AI API token cost calculator: the formula behind every bill

Updated 2026-07-15

Every AI API bill reduces to one formula, applied once per call: (input tokens / 1,000,000 x input price) + (output tokens / 1,000,000 x output price), using whatever rate table the model you called actually publishes. Output tokens run anywhere from about twice to eight times the input rate across the catalog behind this page, so the mix of prompt length versus reply length moves a bill more than most people expect.

Quick answer: the formula behind every token bill

Every AI API bill reduces to one formula, applied per call: (input tokens / 1,000,000 x input price) + (output tokens / 1,000,000 x output price). The two halves rarely move together. A model can be cheap on input and expensive on output, or the other way around, so pricing a task means checking both numbers on that model's own rate table rather than assuming one blended per-token price covers it. Tokens are not words and not characters. English text runs roughly four characters, or about three-quarters of a word, per token as a loose rule of thumb, but the exact split depends on the tokenizer the provider runs internally, and it shifts for code, non-English text, and anything with unusual punctuation or whitespace. Treat a word-count estimate as a rough bound, not the number that ends up on an invoice. The only token counts worth trusting are the ones a real response hands back. Every OpenAI-compatible completion returns a usage object with the exact prompt and completion token counts the provider billed, and the worked table below turns those counts into real dollars using actual catalog prices instead of guesses.

Where the token count actually comes from

Only one of these five is meaningfully discountable. Providers that support prompt caching bill the stable, repeated part of a system prompt or reference document at a fraction of the normal input rate on every call after the first, which softens the cost of large tool schemas and instructions. Nothing softens the other four: history keeps growing, output keeps costing whatever it costs, non-text tokens keep converting on their own terms, and a retried call keeps the tokens it already spent. Build an estimate around what a call actually contains, not around the message a user typed, and it stops running low.

  • System instructions and tool schemas ride along on every single call. A one-line user question can still bill several hundred to several thousand input tokens before the visible prompt even starts, because the framework or SDK around it resends its own setup text each time.
  • Conversation history compounds. Each new turn in a multi-turn chat resends every earlier message as input, so a fifty-message thread costs far more per message than the first message did, even though nothing about that single message changed.
  • Output is priced on its own scale. The reply a model writes bills at the output rate, which sits anywhere from about two to eight times the input rate across the catalog behind this page, so a short prompt with a long answer can outcost a long prompt with a short one.
  • Non-text input gets its own token math. Images, audio, and some document uploads are converted into tokens through provider-specific rules that rarely line up with a plain byte or character count, so a rough guess here is closer to a placeholder than an estimate.
  • Retries still cost something. A call that gets cut off by a length limit and gets resent, or a request that errors out after the provider already processed the prompt, can still be billed for whatever it consumed before the failure.

Worked example: pricing three task sizes across three model tiers

Numbers land better against real tasks than against an abstract per-token rate, so here are three profiles built around how much a call typically carries: a quick lookup, a standard task such as summarizing a document or reviewing one file, and a heavy task such as a long-context or multi-file job. Each is priced at three points in the catalog, a budget model, a mid-tier model, and a premium model, using the formula from the quick answer above.

Calculated with the formula above at each model’s catalog price. Treat as a ceiling before any prompt caching discount.
Task profileTokens (in / out)deepseek-v4-flashglm-5claude-sonnet-4-6
Quick lookup2,000 / 300$0.0003$0.0017$0.0084
Standard task20,000 / 2,000$0.0030$0.0149$0.0720
Heavy task150,000 / 10,000$0.0214$0.1003$0.4800

Why the estimate is usually wrong, and in which direction

Every one of these mistakes points the same direction: low. Treat a pre-call estimate as a starting number, correct it against the usage object a real response returns, and re-check it whenever the shape of the task changes, whether that means a longer thread, a bigger attachment, or a model swap.

  • Counting only the visible prompt. The message a user typed is a fraction of what actually gets billed once system instructions, tool definitions, and prior turns are added back in, so an estimate built from that message alone is a floor, not the real number.
  • Treating output as a rounding error. On several catalog models the output rate runs several times the input rate, so a model that tends to write long, thorough answers can rack up more cost in output than a much larger prompt racks up in input.
  • Pricing today's thread length for tomorrow's. A support bot or coding agent that runs many turns bills the full accumulated history on every turn, so the fifth message and the fiftieth message on the same thread are not the same cost, even though both are just one message.
  • Confusing context window size with token cost. A one-million-token context window is a ceiling on how much a single call can hold, not a discount on what it costs; a call that fills 900,000 tokens of that window is billed for all 900,000.
  • Comparing a sticker price to a discounted one. Two providers can quote very different headline numbers for what is functionally the same model tier once you check whether the number is an official list price or a rate that already reflects a routed discount.

Catalog price comparison: input and output rates side by side

A calculator is only as useful as the price table behind it. The rates below span the catalog from its cheapest models to its flagship tier, alongside the official list price each provider publishes for the same model, so any discount is visible model by model instead of asserted as one flat number.

Catalog rates as of this writing. Global models are priced 20% below official list, Chinese-developed models sit below their own official rates, and providers update pricing over time, so treat this as a snapshot rather than a locked quote.
Model IDInput $/1MOutput $/1MOfficial list input $/1M
deepseek-v4-flash$0.126$0.252$0.14
deepseek-v4-pro$0.3915$0.783$0.435
glm-5$0.514$2.314$0.571
kimi-k2.6$0.855$3.60$0.95
gemini-3.5-flash$1.20$7.20$1.50
grok-4.5$1.60$4.80$2.00
claude-sonnet-4-6$2.40$12.00$3.00
claude-opus-4-7$4.00$20.00$5.00
gpt-5.5$4.00$24.00$5.00

Six ways to shrink the number the calculator gives you

None of these six require touching the shape of a request; the chat completion payload looks the same regardless of which one gets applied first. The fastest win is usually whichever line item is largest in your own usage logs this month, whether that is history length, output verbosity, or the per-token rate itself.

  • Trim the system prompt and tool schema to what the task actually needs. Every token sitting in there is billed on every single call it goes out with, not once per session.
  • Cap output length explicitly with a max_tokens value instead of letting a model run long by default. Verbose answers are the fastest way to inflate the output half of a bill, and output is usually the more expensive half per token.
  • Summarize or drop old conversation history instead of replaying a full thread on every turn once a chat passes a useful length. History that no longer matters to the current turn is still full-price input if it stays in the window.
  • Turn on prompt caching wherever a provider supports it, for any system instructions or reference documents that repeat across calls. It is close to free cost reduction once it is configured.
  • Match the model to the task size. A short classification or lookup does not need a flagship-tier model, and the worked table above shows roughly what that mismatch costs across a month of calls.
  • Route price-sensitive traffic through a cheaper OpenAI-compatible endpoint instead of billing everything at one provider’s direct rate. APIsRouter is one option: pay-as-you-go with no subscription, global models priced below official list and Chinese-developed models below their own official rates, plus a balance bonus on the first top-up, so testing a cheaper model against real traffic does not require a monthly commitment.

Verify the estimate against a real call

Everything above is a pre-call estimate, and pre-call estimates are provisional by definition. Every OpenAI-compatible response returns a usage object with the exact prompt_tokens and completion_tokens a provider billed for that specific call, and that number, not the one from a calculator, is what actually shows up on an invoice. Price a real request once, read the usage object back, and the estimate either holds or tells you exactly where it was wrong.

PRICES_USD_PER_1M = {
    # (input, output) in USD per 1,000,000 tokens
    "deepseek-v4-flash": (0.126, 0.252),
    "glm-5": (0.514, 2.314),
    "claude-sonnet-4-6": (2.40, 12.00),
}

def cost_usd(model: str, prompt_tokens: int, completion_tokens: int) -> float:
    input_price, output_price = PRICES_USD_PER_1M[model]
    return (prompt_tokens / 1_000_000 * input_price) + (completion_tokens / 1_000_000 * output_price)

# price a real request from the usage object the response actually returned
usage = response.json()["usage"]
print(cost_usd("glm-5", usage["prompt_tokens"], usage["completion_tokens"]))

FAQ

How do I calculate the cost of an AI API call?

Multiply the input tokens by the model’s input price per million tokens, do the same for output tokens at the output price, and add the two together. Use the prompt_tokens and completion_tokens fields from a real response for the token counts rather than guessing from word count, since the two rarely match exactly.

Are input and output tokens priced the same?

No. Across the catalog behind the worked table on this page, output tokens run anywhere from about twice to eight times the input rate on the same model, so a short prompt with a long answer can cost more than a long prompt with a short answer.

Why did my actual bill come in higher than I estimated?

Almost always because the estimate only counted the visible user message. System instructions, tool schemas, and prior conversation turns are all billed as input on every call, and a request that gets retried after a truncation or an error can still be charged for the tokens it already consumed.

How many tokens is a typical sentence?

English text runs roughly four characters per token, or about three-quarters of a word per token, as a rule of thumb. It is an approximation only; the exact count depends on the tokenizer the model provider uses, so treat any word-count-based estimate as a rough bound rather than the billed figure.

What is the cheapest way to run AI API traffic at scale?

Match cheap models to routine, high-volume calls and save premium-tier models for tasks that actually need them; the worked table above shows a twenty-times-plus gap for the same task. On top of model choice, APIsRouter is a pay-as-you-go, OpenAI-compatible gateway with no subscription: global models are priced 20% below official list, Chinese-developed models sit below their own official rates, the first top-up adds a 100% balance bonus, and the /topup checkout takes payment first and emails the API key with no signup form.

Does a bigger context window mean a bigger bill?

Not by itself. A larger context window is a ceiling on how many tokens a single call can hold, not a price. The bill only grows when a call actually fills more of that window with system content, history, or attachments, so two models with different context limits cost the same for the same token count.