| Token | The unit a model reads and writes. Roughly ¾ of a word in English; different for code and other languages. |
| Context window | The maximum tokens the model can consider in one call, including your prompt and its answer. |
| Temperature | Sampling randomness. Low values favour the most likely token; high values increase variety and risk. |
| Logit / probability | The score assigned to each possible next token before sampling. |
- 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.
- 01Tokenise the input — text is split into IDs using the model's tokenizer.
- 02Build the context — system instructions, history, retrieved content and the user request are assembled.
- 03Decode — the model predicts one token at a time, applying temperature/top-p sampling.
- 04Stop — generation ends on a stop sequence, length limit or end-of-turn token.
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.
Count tokens before you call
# 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_windowReserve headroom for the answer. If the prompt is tight, compact history or retrieve fewer passages instead of truncating silently.
Pin inference settings
{
"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.
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
| Option | Upside | Cost |
|---|---|---|
| Larger context | More documents and history in one call | Higher cost, slower first token, attention dilution |
| Lower temperature | Deterministic, repeatable outputs | Less creative, can be overly conservative |
| Longer output | Complete answers in one pass | Higher cost and higher chance of drift |
- 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.
| Key | Typical value | Note |
|---|---|---|
| model | pinned version | Avoid floating 'latest' aliases in production. |
| max_tokens | answer-specific | Bound cost and runaway generation. |
| temperature | 0–0.3 for extraction | Raise only where variety is useful. |
| Symptom | Cause | Fix |
|---|---|---|
| Truncated answers | Output budget too small | Raise max_tokens or split the task. |
| Same prompt, different answers | Non-zero sampling or model drift | Pin the version and lower temperature. |
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
Embeddings and vector search
How text becomes vectors, what similarity actually means, and when a vector index is the right tool.
Prompting vs fine-tuning vs RAG
A decision guide: which lever changes behaviour, which one adds knowledge, and what each one costs.
Model selection: capability, cost and latency
A practical framework for choosing a model per task instead of defaulting to the largest one.