Glossary · C

Context Window (LLM)

Maximum number of tokens a language model can take into account in a single request, covering the prompt, history and generated output.

By Obseria TeamUpdated

Definition

The context window of a large language model is the maximum amount of text, measured in tokens, that the model can process in a single request. It includes system instructions, conversation history, retrieved documents, tool results and usually the generated output. Anything outside the window is invisible to the model.

What is a context window?

A language model does not remember earlier requests. Everything it should take into account must be sent with every call, and the context window defines how much that can be. It is measured in tokens, the units into which a tokenizer splits text. A token is often a word fragment; as a rough rule of thumb, an English word corresponds to slightly more than one token, while other languages and source code can need more tokens per word.

The window contains several parts that compete for space: the system prompt, the conversation history, documents retrieved via RAG, tool definitions and tool results, and the answer being generated. Many APIs specify the maximum output length separately, but input and output together still have to fit into the model's limit.

How large are context windows?

Context windows have grown considerably. Early widely used models handled a few thousand tokens. Several current models advertise windows of hundreds of thousands of tokens, and some one million tokens or more. Exact limits differ per model and version, can depend on the API tier and change frequently, so always check the provider's current documentation instead of relying on fixed numbers.

A large window does not mean the model uses all of its content equally well. Research such as "Lost in the Middle" (Liu et al., 2023) showed that models often use information at the beginning and end of a long context better than information in the middle. Long contexts also increase the chance that irrelevant or contradictory content distracts the model.

Effects on cost and latency

  • Cost: providers usually bill per input and output token, so every token placed in the window is paid for in every request. Long conversation histories make each new turn more expensive, which is visible in token usage.
  • Latency: the model must process the entire input before it generates the first token. Long prompts therefore increase time to first token.
  • Prompt caching: several providers can cache a stable prompt prefix, such as system instructions or a large document, which reduces cost and latency for repeated requests.

Managing the context window

Applications rarely fail because the window is too small; they fail because it is filled carelessly. Common techniques, often summarized as context engineering:

  1. Retrieve instead of stuffing: use a vector database or search to include only the most relevant chunks.
  2. Summarize history: compress older turns of a conversation into a summary.
  3. Trim tool output: return only the fields an agent needs, not complete API responses or log dumps.
  4. Order deliberately: put stable instructions first (cache-friendly) and the most important facts where the model attends to them reliably.
  5. Handle overflow explicitly: count tokens before sending and decide what to drop, rather than letting the API reject the request or truncate silently.

Context windows and agents

AI agents are particularly affected. Each loop iteration adds tool calls and results to the context, so a long task can approach the limit even with a large window. Agent frameworks therefore compact or summarize the history, store intermediate results externally, or delegate subtasks to sub-agents with their own fresh context.

What to monitor

Useful signals are input and output tokens per request, the share of the window used, the number of requests that hit the limit or were truncated, cache hit rates and the correlation between prompt length, latency and answer quality. The OpenTelemetry GenAI semantic conventions define attributes such as gen_ai.usage.input_tokens and gen_ai.usage.output_tokens for this.

Counting tokens correctly

Token counts depend on the tokenizer, and tokenizers differ between model families. The same prompt can therefore be a different number of tokens for two models, and a character-based estimate can be far off for code, tables or non-English text. Use the tokenizer or token-counting endpoint of the provider when precision matters, and rely on the usage figures returned in the API response for billing and monitoring. Also remember hidden contributors: tool definitions, structured output schemas, images and, for some reasoning models, internal reasoning tokens can count against limits or be billed even though they are not visible in the final answer.

How Obseria helps

With AI observability, Obseria records token usage, latency and errors of every LLM call as OpenTelemetry spans. You can see which prompts grow over time, which agents run close to their limit and how context length affects cost and time to first token.

Frequently asked questions

Depending on the API, the request is rejected with an error, or the application or framework truncates or drops older content. Silent truncation is risky because the model then answers without information it was supposed to see.

No. It allows more input, but every token costs money and latency, and models do not use very long contexts equally well. Relevant, well-selected context usually beats a full window.

Usually yes: input and generated output together must fit the model's limit. Many APIs also define a separate maximum for output tokens. Check the documentation of the model you use.

The context window is what the model sees in a single request. Memory in AI applications is implemented outside the model, for example in a database, and relevant parts are inserted into the context when needed.

Put these concepts into practice

Try Obseria free for 14 days, with OpenTelemetry and no proprietary agents.