LLM Inference Optimization & Serving · 1 of 5
The Inference Cost Model
Inference cost is the price of turning a prompt into an answer, shaped by tokens, model choice, latency, context, and tooling.
A model does not cost money because it is clever. It costs money because every answer asks real machines to do real work. Inference is the name for that work: taking an input, running it through a trained model, and producing an output. If you understand what makes that work larger, slower, or more repeated, you can understand the bill without needing to be a machine learning specialist.
Inference starts when a question becomes compute
A trained model is like a very large set of learned relationships. It is not searching a fixed database for a prewritten answer. It is calculating, step by step, which pieces of text are likely to come next given the input it received.
The cost model begins with a simple chain. A user sends a prompt. The service prepares that prompt for the model. The model reads the prompt, generates an answer, and returns it. Each stage uses infrastructure, but the model run is usually the expensive part because it requires specialized processors and memory.
Inference is different from training. Training is the process that creates or adapts a model by learning from data. Inference is the use of that model after training. Most organizations do far more inference than training, so small choices in day-to-day usage can matter more than a rare training run.
The basic unit is the token
Most language model pricing starts with tokens. A token is a small chunk of text, often a word, part of a word, punctuation mark, or space pattern. The sentence a person sees as ten words may become more or fewer tokens depending on the language and wording.
There are two token counts to care about. Input tokens are what the model reads: the user question, system instructions, conversation history, retrieved documents, and any hidden formatting added by the application. Output tokens are what the model writes back.
Output tokens often cost more time than input tokens because the model generates them one after another. It cannot produce the final word until it has produced the words before it. This is why a long answer can feel slow and cost more, even if the original prompt was short.
- Input tokens measure what the model must read.
- Output tokens measure what the model must generate.
- Total tokens are a useful first estimate, but they do not explain every cost.
- Different models may charge different rates for the same number of tokens.
The bill is not only tokens
A simple formula says inference cost equals input tokens plus output tokens, multiplied by the model's price. That formula is a good starting point, but it is incomplete. Real systems also pay for capacity, orchestration, network movement, storage lookups, monitoring, and the cost of failed or repeated attempts.
Model choice is the largest lever after volume. A smaller model may answer routine classification, summarization, or extraction tasks well enough at a lower price. A larger model may be needed for complex reasoning, long context, better instruction following, or higher accuracy. The cheapest model is not the one with the lowest price per token; it is the one that reaches the required quality with the least total work.
The cost model should include waste. If users ask the same question repeatedly, if prompts include unnecessary history, if the system retries too aggressively, or if an application generates answers that users reject, the metered bill may look normal while the business value is poor.
Latency and throughput turn price into capacity
Latency is the time a user waits for one response. Throughput is how much work the system can complete in a period of time. A system can have a low price per request and still fail if it cannot serve enough requests at the required speed.
Fast responses may require more reserved capacity, less batching, shorter outputs, or a smaller model. Slower background jobs can often be batched, scheduled outside peak hours, or sent to a less expensive model. This is why the same model can have different economics in a chat assistant, a nightly document processor, and a real-time customer service workflow.
There is also a human side to latency. If an answer takes too long, users may click again, abandon the workflow, or open another tool. Those behaviors create hidden cost: duplicate requests, lost productivity, or lower trust.
Context is useful, but it is not free
The context window is the amount of information a model can consider in one request. A larger context window lets an application include longer documents, more conversation history, richer instructions, or retrieved evidence. That can improve quality, but every included token must be processed.
A common mistake is to treat context as free memory. It is not. If an application sends a full policy manual when only three paragraphs matter, the model must still read the full manual. Long context also increases the chance that important details compete with irrelevant ones.
Good cost control often means being selective. Keep system instructions short and stable. Trim old conversation turns. Retrieve only the passages that are likely to matter. Summarize when precision is not required, and include exact source text when it is.
Tools, retrieval, and agents add extra meters
Many useful applications do more than call a model once. They retrieve documents, call business systems, search the web, run code, classify the result, and ask the model to revise its answer. Each step can add cost and latency.
Retrieval-augmented generation, often shortened to RAG, is a pattern where the application retrieves relevant information and places it into the prompt so the model can answer with current or private context. RAG can reduce hallucination and avoid fine-tuning for every knowledge update, but it adds search, storage, ranking, and additional input tokens.
Agentic systems go further. An agent is a model-driven loop that chooses actions, observes results, and decides what to do next. This can solve more open-ended tasks, but it also makes cost less predictable because one user request may become many model calls and tool calls. Platforms such as Microsoft Foundry can help assemble and observe these systems, but the cost model still belongs to the application owner.
- A retrieval step may add search and storage charges before the model is called.
- A tool call may trigger another application programming interface, or API, with its own limits and costs.
- An agent loop may make several model calls for one visible user request.
- A safety or quality check may add another model pass after the first answer.
The model informs real decisions
A leader does not need to inspect every prompt, but does need to decide what the system is allowed to spend to create a useful outcome. That means connecting technical measures to business measures: cost per answer, cost per resolved case, cost per document processed, or cost per employee hour saved.
A practitioner needs a similar but more detailed view. Which model should serve each task? How long should answers be? How much conversation history should be retained? When should the system retry? Which requests need the strongest model, and which can be routed to a smaller one?
The most important decision is not simply budget approval. It is setting a quality threshold. Below that threshold, the system creates rework or risk. Above it, extra quality may not be worth the marginal cost. A good inference cost model makes that trade visible.
- Choose a quality target before choosing the cheapest model.
- Measure cost per useful outcome, not only cost per token.
- Route simple tasks to smaller models when quality is sufficient.
- Set output length limits where long answers do not add value.
- Budget separately for experiments, production traffic, and peak demand.
Manage inference as a living system
Inference economics change as users learn the system, prompts evolve, documents grow, and new models become available. A cost model should therefore be monitored, not written once and filed away.
Start with measurement. Log token counts, model names, latency, retries, tool calls, and user-visible outcomes. Then improve the highest-cost paths first. Often the easiest wins are prompt trimming, response length limits, caching repeated answers, and routing routine work away from the largest model.
Caching deserves special attention. A cache stores a previous result or reusable part of a request so the system can avoid doing the same work again. It is powerful when many users ask similar questions, but it must be designed carefully so users do not receive stale, unauthorized, or inappropriate answers.
Key takeaways
- Inference is the runtime work of turning an input into a model output.
- Token counts are the starting point, but model choice, latency, tools, retries, and capacity also shape cost.
- Output tokens often affect both price and user wait time because they are generated step by step.
- Long context can improve answers, but irrelevant context wastes money and may reduce focus.
- Measure cost per useful outcome, not only cost per request or token.
- Inference cost control is an ongoing operating practice, not a one-time architecture choice.
Glossary
- Inference
- The process of using a trained model to produce an output from a new input.
- Token
- A small unit of text, such as a word, part of a word, punctuation mark, or spacing pattern, that a language model processes.
- Input tokens
- The tokens a model reads, including the user prompt, instructions, history, and any retrieved context.
- Output tokens
- The tokens a model generates as its answer.
- Context window
- The maximum amount of information a model can consider in a single request.
- Latency
- The time between sending a request and receiving the response.
- Throughput
- The amount of work a system can complete in a given period of time.
- Retrieval-augmented generation
- A design pattern that retrieves relevant information and supplies it to a model so the answer can use current or private context.
- Cache
- A stored result or reusable computation that helps avoid repeating the same work.