Skip to content
NewFree 30-minute AI strategy call for new clients.Book yours
SYF Tech
LLMEngineering

How an LLM produces an answer, and what that means in production

Tokens, next-token prediction, the context window and sampling explain most LLM behaviour in production, from the bill to the hallucinations.

In short

An LLM turns text into tokens, then predicts one token at a time from what is in its context window. It optimises for a plausible continuation, not a verified fact, so production systems need grounding, evaluation sets, structured outputs and per-token monitoring.

Most surprises with LLMs in production come from four mechanics: tokens, next-token prediction, the context window and sampling. Once you see how an LLM produces an answer, the bill, the latency and the occasional confident wrong answer stop being mysterious. They become engineering problems with known fixes.

Text becomes tokens

A model never sees characters or words. A tokenizer splits text into tokens: whole words, pieces of words, punctuation or bytes. Most tokenizers learn subword units with methods such as byte-pair encoding, so common words stay whole and rare ones split into pieces.

Animation: the sentence "Unbelievably, tokens cost money." is split into coloured chips one by one: "Un", "believ", "ably", ",", " tokens", " cost", " money", "." A counter beneath them climbs to 8 tokens. A note says the split is illustrative and real splits depend on the tokenizer.

An illustrative split: one short sentence, eight tokens, and rare words cost more than common ones.

Everything is counted in tokens: context limits, rate limits and price. The ratio to text is tokenizer-dependent. OpenAI's rule of thumb is about four English characters per token; Anthropic's docs put earlier Claude models at about 3.5, with newer models producing roughly 30% more tokens for the same text. Measure with the provider's token counting tool, not a word count.

One token at a time

An LLM is autoregressive. Given the tokens so far, it outputs a probability for every token in its vocabulary. One token is chosen, appended to the input, and the loop runs again until a stop condition or max_tokens.

Choosing is called decoding. Greedy decoding always takes the top token. Sampling draws from the distribution, and temperature reshapes it first: low temperature sharpens it towards the top choice, high temperature flattens it so less likely tokens get picked more often.

Animation: the prompt "The invoice is" feeds a model box that outputs a probability bar chart for the next token: "due" highest, then "paid", "late" and "blue" lowest. On the left, low temperature: the sharpened bars always select "due". On the right, high temperature: flatter bars, and the pick alternates between "paid" on one loop and "due" on the next. The chosen token is appended to the prompt and the loop repeats. Probabilities are illustrative.

Same prompt, same model: low temperature keeps picking the top token, high temperature spreads the picks.

That is why the same prompt can give different answers. Even temperature 0 was never fully deterministic on Claude's API, and some current models, such as Claude Opus 5.5, have removed temperature, top_p and top_k: the migration guide says to omit them. Design for variation instead of trying to switch it off.

The context window is the only working memory

The model keeps nothing between requests. Everything it can use (system prompt, history, retrieved documents, tool results and its own output) has to fit in the context window, and all of it is sent and billed again on every turn. Prompt caching lowers the price of a repeated prefix, not its size.

Animation: blocks of conversation tokens (user, reply, tool, result) stream in from the right while a fixed-size box labelled "context window" holds only the newest four. The oldest tokens slide out of the left edge, fade and are labelled "dropped by your app". A note says the API does not do this for you: it returns an error or stops, so the application must trim, summarise or compact.

A long conversation outgrows the window, and something has to decide what leaves it.

The Claude API does not slide the window for you. An input that is too long is rejected, and a response that hits the limit stops. Your application decides what to drop, summarise or compact. More context is not automatically better either: Anthropic's docs note that recall degrades as token count grows.

Why models hallucinate

Pretraining teaches a model to predict the next token, not to look up facts. When the context lacks the answer, the most plausible continuation can still be fluent, specific and wrong. A 2025 paper by Kalai and colleagues argues that training and benchmark scoring reward guessing over saying "I don't know", which keeps the behaviour alive.

What this means in production

  • Grounding. Put the source material in the context and tell the model to answer only from it, with quotes or citations. Choosing how is its own decision: see RAG vs CAG.
  • Evaluation sets. Because outputs vary, one good demo proves little. Keep a versioned set of real questions and edge cases, grade it automatically where you can, and rerun it on every prompt or model change.
  • Structured outputs. When code consumes the answer, constrain it to a JSON schema (output_config.format on the Claude API) instead of parsing prose. Still handle refusals and max_tokens cut-offs.
  • Per-token monitoring. Log input, output and cached tokens from the usage field, plus time to first token and total latency, per feature. On Claude's price list, output tokens cost more than input tokens, so long answers drive both cost and wait time.

In practice

  1. Count tokens with the provider's API before you set limits or budgets.
  2. Set max_tokens deliberately and handle every stop reason.
  3. Trim or compact history before it reaches the window, not after an error.
  4. Ground factual answers in supplied sources and allow "I don't know".
  5. Gate prompt and model changes on an evaluation set.
  6. Dashboard cost and latency per token, per feature.

Building something that has to work every time, not just in the demo? See how we build AI systems.

Sources

LLMEngineering
Share

SYF Tech

AI, automation and app development from Pakistan, for teams worldwide.

All insights

Have a project in mind?

Tell us what you're building. A senior engineer replies within 24 hours.

Start a project