Skip to content
Beytullah Toprak

Blog

How to Calculate and Reduce LLM API Costs

What a token is, how to calculate monthly LLM API cost, and how caching, batching and the right model bring it down, with a worked example.

Reading time
6 min read
API cost illustration with a coin stack, bar chart and clock icons
On this page
  1. What Is a Token, and Why Not Words?
  2. What Makes Up the Cost
  3. How to Calculate Monthly Cost
  4. Ways to Reduce Cost
  5. 1. Cache Repeated Text
  6. 2. Batch Work That Can Wait
  7. 3. Match the Model to the Job
  8. 4. Shorten Input and Output
  9. 5. Consider Flexible Processing for Work That Isn't Urgent
  10. 6. Measure and Set Limits
  11. Frequently Asked Questions
  12. Is Non-English Text More Expensive?
  13. Does Caching Always Save Money?
  14. How Do I Estimate Cost at the Start of a Project?

LLM API cost comes from the number of tokens you send to the model (input) and the tokens it generates (output), multiplied by the model's token prices. Three numbers are enough for a monthly estimate: average input tokens per request, average output tokens per request and requests per month. The most effective ways to cut that cost are caching repeated text, sending work that can wait as a batch and not using a bigger model than each job needs.

What Is a Token, and Why Not Words?

Language models process text as pieces called tokens, not word by word. A token is sometimes a whole word, sometimes part of a word and sometimes a punctuation mark. OpenAI's guide to counting tokens (opens in a new tab) says that for English text, 1 token is roughly 4 characters and 100 tokens are about 75 words. The same page notes that other languages can have a different relationship between characters, words and tokens.

That's an important caveat for projects in other languages. Turkish, for example, builds long words from suffixes, and one word can split into several tokens. An estimate based on the English ratio may not hold. The most reliable approach is to measure samples of your own text with your provider's token counter. Most major providers offer a tool or API for this.

What Makes Up the Cost

A request's bill has several parts. First come input tokens: the system prompt, the user's message, the conversation history and any document passages added through RAG. Second are the output tokens that make up the model's answer. An output token usually costs several times more than an input token.

Some models, known as reasoning models, also run an invisible reasoning step before answering. According to OpenAI's guide, these reasoning tokens don't appear in the answer text but are billed as output. If you use caching, input tokens written to and read from the cache are priced separately too. In short, two requests with answers of the same length can use very different numbers of tokens behind the scenes.

The part people tend to miss is the input side. A long system prompt is sent again with every request. As a conversation grows, so does its history, and it's billed again with each new message.

How to Calculate Monthly Cost

The formula is simple:

Monthly cost = monthly requests × (average input tokens × input price + average output tokens × output price)

Prices are usually quoted per million tokens. To show the math, the example below uses a hypothetical model priced at $3 per million input tokens and $15 per million output tokens. Real prices vary by model and change often, so check the provider's current pricing page when you do your own calculation.

Picture a support assistant handling 10,000 requests a month. Each request carries a fixed 1,500-token system prompt and a user message averaging 500 tokens, for 2,000 input tokens. Answers average 300 tokens.

Item Calculation Result
Input 10,000 × 2,000 = 20 million tokens × $3 $60
Output 10,000 × 300 = 3 million tokens × $15 $45
Total $105 / month

I'd recommend running this calculation at the start of a project with real examples. Measuring average input and output tokens across 50 to 100 real requests turns a guess into an estimate.

Ways to Reduce Cost

1. Cache Repeated Text

A system prompt, examples or a document that stays the same on every request can be cached. According to Anthropic's pricing page (opens in a new tab), a cache read costs 0.1x the base input price on most models. A cache write costs 1.25x for the 5-minute cache and 2x for the 1-hour cache, so the 5-minute cache pays for itself after one read and the 1-hour cache after two. At OpenAI, prompt caching (opens in a new tab) is on by default for supported models, with discounts of up to 95% on reused tokens. On OpenAI's newer models, cache writes are also billed at 1.25x.

In the example above, reading the 1,500-token system prompt from the cache changes input cost like this: 15 million tokens at 0.1x come to $4.50, and the remaining 5 million at full price come to $15. Input drops from $60 to about $20, plus a small extra for cache writes. For caching to work, the fixed part has to come first in the request and be exactly the same every time. Putting something that changes, like a date or a user's name, at the start of the prompt breaks the cache.

2. Batch Work That Can Wait

Jobs that don't need an immediate answer, such as overnight reports, bulk product descriptions or classifying an archive of old documents, can go through a batch API. The OpenAI Batch API (opens in a new tab) offers a 50% discount over synchronous requests with a 24-hour completion window. Anthropic's Message Batches API (opens in a new tab) also cuts cost by 50%, with most batches finishing in under an hour.

3. Match the Model to the Job

Not every job needs the largest model. Sorting requests into categories or pulling data out of a form can often be done with a small, cheap model. OpenAI's cost optimization guide (opens in a new tab) also lists choosing a smaller model that keeps accuracy among its main options. Use your own test set to decide: if the small model is accurate enough on your examples, there's no need to pay for the big one.

4. Shorten Input and Output

  • Review the system prompt and remove rules that repeat or that the model already follows.
  • In long conversations, summarize older messages or send only the last few.
  • If you use RAG, tune the number of passages sent to the model by measuring. Every unnecessary passage is billed on every request.
  • Cap output length, and ask for structured output that contains only the fields you need.

5. Consider Flexible Processing for Work That Isn't Urgent

Alongside batching, OpenAI's cost guide lists Flex processing as an option for work that can wait. It trades a lower price for slower responses. It's worth considering for background jobs no user is waiting on.

6. Measure and Set Limits

Log usage by request, user and feature. Without knowing which feature drives most of the cost, optimization is guesswork. Setting spend limits and alerts in the provider's console also keeps a runaway loop or abuse from inflating the bill.

Frequently Asked Questions

Is Non-English Text More Expensive?

The same content can map to a different number of tokens depending on the language and the model's tokenizer. OpenAI's guide also notes the ratio can differ in other languages. For a firm answer, measure your own text with the token counter of the model you use.

Does Caching Always Save Money?

No. At Anthropic and on OpenAI's newer models, cache writes cost more than regular input, so if cached text isn't read often enough, cost can go up. Caching pays off when the same long text is reused at short intervals.

How Do I Estimate Cost at the Start of a Project?

Build a set of real usage examples, measure input and output tokens for each and multiply the averages by your expected monthly requests. I cover the general framework in the AI integration guide and how RAG affects cost in RAG vs fine-tuning.

If you'd like a cost estimate for your project, or to bring down the cost of an existing integration, see the AI and LLM Integration service or write to me through the contact page.

Share