Skip to content

AI & automation · Guide

LLM cost in production: what drives the bill and how to control it

The cost of an LLM in production comes mostly from tokens: input tokens (instructions, context, history and tool definitions) and output tokens, multiplied by the number of calls each task makes. The model you choose, the context size, agent loops and caching decide how much each task consumes.

Master TimmUpdated on 6 min read

How the bill is built: tokens, calls and model

LLM providers charge per token, the unit of text a model reads and writes, with different prices for input and output and for each model. The cost of a task is the sum of the calls it makes; the monthly cost is that figure times the volume.

  • Input tokens: Everything sent to the model on each call, such as system instructions, tool definitions, retrieved documents and the conversation history.
  • Output tokens: The generated text; they usually cost more than input tokens, and the reasoning of models that think before answering is billed as output too.
  • Calls per task: A chatbot makes one call per message; an agent makes several, and each step sends the accumulated context again.
  • Model: Larger models cost more per token than smaller ones from the same provider, and price lists change often.

Beyond tokens, the estimate also covers embeddings, the vector store, infrastructure and observability.

What weighs most on the bill

In practice, the bill goes off forecast less because of the token price and more because of how the system is designed. These are the usual culprits:

  • Context that only grows: The full history, too many documents and long instructions sent again on every call.
  • Agent loops: Tasks that take many steps, retries after an error and tools called more than once for the same information.
  • A model larger than the task needs: Using the most capable model to classify, extract or summarize, tasks a smaller model handles.
  • Long output: Lengthy answers, verbose formats and long reasoning on simple tasks.
  • Wasted cache: Prompts that change right at the start on every call, which prevents the prefix from being reused.

How to cut cost without losing quality

Every cost cut goes through the same evals as quality: a cut that lowers the success rate ends up costing more, in rework and human review.

  • Prompt caching: The provider reuses an identical prefix (instructions, tools, fixed documents) and charges less for tokens read from the cache, so stable content goes at the start of the prompt and variable content at the end.
  • Semantic caching: A question similar to one already answered gets the stored answer, with no model call; it needs a similarity threshold and expiry rules.
  • Routing by task: A smaller model classifies, extracts and summarizes, and the larger model takes the hard steps.
  • A lean context: Compact the history, retrieve fewer and more relevant passages (reranking) and cap the output length.
  • Batch processing: Work that does not need an immediate answer goes to the providers' batch APIs, which charge less in exchange for delivering later.

How to forecast the cost before you build

A good estimate comes from measurements, not assumptions: first measure the tokens of a real task, then apply the provider's current price list.

  1. Define the task and the volume: How many conversations, documents or runs per day, and with what peaks.
  2. Measure on a prototype: Run real samples and record the input, output and cache tokens the API reports in every response.
  3. Count the calls per task: The average and the worst case, with retries and extra agent steps.
  4. Apply today's prices: Multiply by the current prices of each candidate model; since they change, the spreadsheet keeps the date they were checked.
  5. Compare scenarios: A larger or smaller model, with and without caching, always by cost per completed task, not per call.
  6. Keep a margin: Usage growth, context that grows over time and prompt changes.

How to track cost in production

In production, the bill has to be read per task, per flow and per customer, not only as a monthly total. That means recording the tokens of each call as it happens.

  • Traces: Every run becomes a trace with the model calls, the tools, the tokens and the cost of each step; OpenTelemetry's generative AI conventions standardize those fields.
  • Cost per completed task: It combines cost with eval results and shows when a cheaper model ends up costing more because it fails more often.
  • Cache hit rate: It shows whether prompt caching and semantic caching are working.
  • Limits and alerts: A budget per flow or per customer, a step limit per task and an alert when cost drifts from the average.

In Master Timm's own products, Zorbit audits the cost and the tokens of every agent run, and the Contas Casal assistant tracks cost per model and uses a semantic cache.

Related service

AI & LLM Engineering

AI agents, RAG and MCP in production, not in demos.

Free first conversation · Reply within 1 business day

See also: AI Customer Service Agents.

Frequently asked questions

What is a token in an LLM?

A token is the piece of text a model reads and writes: a short word, part of a word or a punctuation mark. Each model has its own tokenizer, so the same text can produce different counts on different models, and text in languages other than English usually takes more tokens for the same content. APIs report the exact count in every response.

Why does the LLM bill change so much from month to month?

Because it follows usage and system design, not a fixed fee. More users, longer conversations, a prompt that grew, a model switch, an agent that now takes more steps or a cache that stopped hitting all change the bill without warning. Traces with cost per task show which of these changed.

Does switching to a cheaper model always cut the cost?

Not always. A smaller model can fail more often, need more steps and retries and create more human rework, and then the cost per completed task goes up. The right comparison runs both models on the same evaluation dataset and measures cost and success rate together.

Are prompt caching and semantic caching the same thing?

No. Prompt caching happens at the provider: an identical prompt prefix is reused, and the model still generates the answer. Semantic caching lives in your application: a question similar to one already answered gets the stored answer, with no model call. The two add up, and each has its limit: the first works only on an identical prefix, and the second can return an outdated answer.

Sources

Master Timm · Updated on

Tell us what you're building

Tell us the context: the problem, the timeline and what already exists. You talk directly to the engineer who will do the work.

Request a first call

Free first conversation · Reply within 1 business day