mattrguerra/cortexmainPrivate on GitHub, published here in full

cost-accounting.md

Language Markdown2.9 KB67 lines

docs/cost-accounting.md

  1. # How Cortex counts cost
  2. Cortex exists to make the price of each reply visible, so the arithmetic is
  3. simple and written down here.
  4. ## Prices
  5. DeepInfra publishes, for every model, a price per million tokens:
  6. `input_tokens`, `output_tokens` and, for models with prompt caching,
  7. `cache_read_tokens`. They come with the public model list
  8. (`GET https://api.deepinfra.com/v1/openai/models`, no key needed). Cortex
  9. keeps only models tagged `chat`, refreshes the list at startup when it is more
  10. than a day old, and stores it locally.
  11. When the list cannot be fetched (offline, or DeepInfra changes the format),
  12. Cortex falls back to `crates/cortex-core/data/pricing-snapshot.json`, a dated
  13. copy made by `node scripts/snapshot-pricing.mjs`. The model picker then says
  14. "Prices as of DATE (offline copy)".
  15. A price above $1,000 per million, or below zero, is treated as a parsing
  16. mistake and the model is dropped rather than shown with a wrong price.
  17. Local servers (Ollama, LM Studio, llama.cpp) cost nothing.
  18. ## One reply
  19. ```
  20. cost = (prompt - cached) x input price
  21. + cached x cache price (the input price if the model has none)
  22. + completion x output price
  23. ```
  24. with prices per million tokens. Money is stored as whole **nano-dollars**
  25. (10^-9 USD) in a 64-bit integer, so a month of sums never drifts the way
  26. floating point does. Displayed values round to four decimal places below a
  27. dollar and two above; anything under $0.0001 shows as "<$0.0001".
  28. Each reply stores the prices it was billed at. A later price change never
  29. rewrites what an old reply cost.
  30. ## Where the token counts come from
  31. | `usage_source` | When | Shown as |
  32. |---|---|---|
  33. | `reported` | The provider sent a usage block (Cortex asks for one with `stream_options.include_usage`). | the numbers |
  34. | `estimated` | No usage block: most local servers, a stream you stopped, an error part way. | the numbers with an **est.** badge |
  35. | `none` | Refused before a single token (bad key, no credit, rate limit). | nothing; costs $0 |
  36. The estimate is about four characters per token for Latin text and one per
  37. CJK character, plus four tokens per message for the chat template and 765 per
  38. image. It is deliberately rough and always labeled.
  39. If DeepInfra reports its own cost for a reply (`usage.estimated_cost`), Cortex
  40. stores it beside its own figure for comparison, but shows its own arithmetic.
  41. ## Month to date and the budget
  42. "This month" is the sum since midnight on the first of the current month in
  43. your local time zone. The monthly budget warns at 80% and again at 100%, in
  44. the chat, the sidebar and the menu bar. It never blocks a send by itself; an
  45. optional setting asks for confirmation once the budget is spent.
  46. ## Before you send
  47. The composer shows roughly what the prompt will cost: the conversation so far
  48. plus what you have typed and attached, at the model's input price. The reply's
  49. length is unknown until it arrives, so this is the floor, not the total.