AI Spend LedgerCalculatorGuidesMethodologyAbout

How to Calculate LLM API Cost (Formula and Examples)

Updated November 4, 2026

Every major LLM API bills per token, with separate prices for input and output. The math is simple once you have the right numbers.

The formula

Cost = (input tokens × input price + output tokens × output price) ÷ 1,000,000

Prices are quoted per million tokens, so you divide by one million.

Worked example

You send 100,000 input tokens and get 20,000 output tokens on a model priced at $2 input and $10 output per million:

Scaling to a month

Multiply the per-request cost by requests per day and days per month. At 1,000 requests a day with 1,500 input and 400 output tokens, that is 45 million input and 12 million output tokens a month, or about $210 at the same $2 / $10 prices.

Numbers you need

If you are new to the unit itself, read what a token is.

Step 1: find your token counts

The most accurate source is the usage information returned with each API response, or the usage dashboard of your provider. If you have not built anything yet, estimate from words. As a rule of thumb, one word is about 1.3 tokens in English, so a 1,500-word document is roughly 2,000 tokens. Code and non-English text usually use more tokens per word.

Step 2: separate input from output

Input includes your system prompt, the conversation so far, any documents and tool results. Output is the model's reply, including hidden reasoning on models that bill for it. Keep the two numbers separate, because they have different prices.

Step 3: account for conversation history

Chat apps resend the history on every turn, so input grows with each message. Suppose a system prompt of 500 tokens, a user message of 100 tokens and a reply of 200 tokens per turn. Over a 10-turn conversation, total input is about 19,500 tokens and output is 2,000. At $2 / $10 per million, that costs about $0.06. Without resending history it would be about $0.03. For long chats, history dominates the cost, which is where caching helps.

Step 4: adjust for caching and batch

If part of the input is cached, split it out: cost equals fresh input times the input price, plus cached input times the cache-read price, plus output times the output price. Example on Claude Sonnet 5.5: 90,000 cached tokens at $0.10, 10,000 fresh tokens at $2 and 20,000 output tokens at $10 cost about $0.23, compared with $0.40 without caching, a saving of roughly 43%. For batch jobs, halve the standard rates where the provider offers a batch discount.

A spreadsheet formula

If your tokens are in columns B (input) and D (output), and prices per million are in C and E, use:

Fill it down and sum the column to get your total.

Common mistakes

Frequently asked questions

How do I count tokens before sending a request?

Many providers offer a token-counting endpoint or library. Use it on a sample of your real prompts.

Do all providers count tokens the same way?

No. Each model has its own tokenizer, so the same text can produce different counts and different costs.

How accurate are estimates?

Within 10 to 20 percent if you use real token counts and add a margin for retries and tool calls. See hidden LLM costs.

Skip the spreadsheet

The LLM cost calculator does this for 34 models at once. For ongoing tracking by team and project, use the AI spend tracker template.

Check the source: official pricing pages: Anthropic · OpenAI · Google Gemini · xAI · Mistral · DeepSeek · Amazon Bedrock · Cohere. See our methodology.

Free tool: compare 34 LLM models in the API cost calculator.
Track spend by team and project: the AI Spend Tracker spreadsheet is launching soon.