Skip to content

Language models: tokens, context and inference

The mental model behind an LLM call: tokenisation, context windows, sampling controls and why outputs are probabilistic.

6 MIN READUPDATED 2026-11-18Models & foundations
ORIGINAL DIAGRAM · 4 STEPS1Tokenise the input —2Build the context —3Decode — the model4Stop — generation ends

Original flow diagram from note steps — not a copied image

§ 00Notes & rules
Context is a budget
The context window is shared by system instructions, conversation history, retrieved evidence, tool definitions and the model's answer. Count the full prompt, not just the user's question.
Floating model aliases
Avoid 'latest' model names in production. Pin an explicit model version in configuration so behaviour, cost and evaluation results stay reproducible.
§ 01Key concepts
TokenThe unit a model reads and writes. Roughly ¾ of a word in English; different for code and other languages.
Context windowThe maximum tokens the model can consider in one call, including your prompt and its answer.
TemperatureSampling randomness. Low values favour the most likely token; high values increase variety and risk.
Logit / probabilityThe score assigned to each possible next token before sampling.
§ 02When to use
  • Scoping a new AI feature and estimating cost or latency.
  • Debugging why a prompt behaves differently across models.
  • Deciding how much history or retrieved context to send.
§ 03How it works
  1. 01Tokenise the input — text is split into IDs using the model's tokenizer.
  2. 02Build the context — system instructions, history, retrieved content and the user request are assembled.
  3. 03Decode — the model predicts one token at a time, applying temperature/top-p sampling.
  4. 04Stop — generation ends on a stop sequence, length limit or end-of-turn token.
§ 04Use cases & scenarios

Support chat that stays within budget

Context
A support assistant handles long threads with retrieved policy passages.
Problem
Answers truncate or earlier context disappears as the thread grows.
Approach
Count tokens for the full prompt, reserve headroom for the answer and compact older turns.
Outcome
Predictable cost per conversation and complete, grounded answers.

Repeatable invoice extraction

Context
An accounts team extracts fields from vendor invoices.
Problem
The same invoice produces slightly different values across runs.
Approach
Pin the model version, use temperature 0 with a fixed seed and a strict output schema.
Outcome
Stable structured output that downstream automation can trust.
§ 05Examples

Count tokens before you call

python
# Example: estimate prompt size (tokenizer varies by model)
from tiktoken import encoding_for_model

enc = encoding_for_model("gpt-4o-mini")
messages = system_prompt + history + retrieved_context + user_question
tokens = enc.encode(messages)

print(len(tokens), "prompt tokens")
assert len(tokens) + max_answer_tokens <= context_window

Reserve headroom for the answer. If the prompt is tight, compact history or retrieve fewer passages instead of truncating silently.

Pin inference settings

json
{
  "model": "gpt-4o-mini-2024-07-18",
  "temperature": 0.2,
  "max_tokens": 600,
  "top_p": 1.0,
  "seed": 42
}

Low temperature and a fixed seed improve repeatability for extraction and classification tasks.

§ 06Trade-offs

Larger context

Up: More documents and history in one call

Down: Higher cost, slower first token, attention dilution

Lower temperature

Up: Deterministic, repeatable outputs

Down: Less creative, can be overly conservative

Longer output

Up: Complete answers in one pass

Down: Higher cost and higher chance of drift

§ 07Implementation checklist
  • Count tokens for the full prompt, not just the user question.
  • Reserve headroom for the answer inside the context window.
  • Pin the model version in configuration for reproducible behaviour.
  • Log token usage per request to establish a cost baseline.
§ 08Configuration reference
KeyTypical valueNote
modelpinned versionAvoid floating 'latest' aliases in production.
max_tokensanswer-specificBound cost and runaway generation.
temperature0–0.3 for extractionRaise only where variety is useful.
§ 09Troubleshooting
SymptomCauseFix
Truncated answersOutput budget too smallRaise max_tokens or split the task.
Same prompt, different answersNon-zero sampling or model driftPin the version and lower temperature.
§ 10Frequently asked questions

Is a context window the same as memory?

No. It is the working set for one call. Persistent memory must be stored and re-injected deliberately.

Why do models mishandle long prompts?

Relevant details can be crowded out. Retrieval and concise context usually beat dumping everything in.

More in Models & foundations