Context Window
The maximum number of tokens a large language model can process and attend to simultaneously within a single inference call
Table of Contents
Structure Diagram
What is Context Window?
The maximum number of tokens a large language model can process and attend to simultaneously within a single inference call. It is a core concept in LLM Fundamentals within AI / ML.
Core Principles
The Context Window is an LLM's working memory. Everything the model needs to see in a single call—system instructions, conversation history, retrieved RAG chunks, tool schemas, tool outputs, and the generated response itself—must fit inside this shared token budget.
Exceeding the limit forces older messages to be truncated or triggers an API error. Even below the hard ceiling, longer contexts expand KV-cache memory and compute cost, raising both latency (Time to First Token) and per-request spend.
Practical Takeaways
- Account for "Lost in the Middle" degradation: Even with 128K–1M token windows, models recall instructions placed at the very beginning and very end more reliably than facts buried in the middle. Place critical rules and top-ranked evidence near the boundaries.
- Compact history and prune noisy tool outputs: In long-running chat or agent loops, periodically summarize older turns and trim verbose logs before appending them—keeping signal-to-noise ratio high directly improves reasoning accuracy.
- Structure prompts for Prompt Caching: Put static prefixes (system prompt, tool definitions, shared docs) at the top and dynamic user input at the bottom so providers can reuse cached KV states, cutting input token costs and latency dramatically.
Notable Examples
- AI coding assistants: Loading multiple source files, test errors, and git diffs simultaneously to perform cross-file refactors.
- Long-document legal & financial review: Ingesting entire contracts or quarterly reports in one pass to cross-check clauses and numbers.
- Structured multi-turn chatbots: Budgeting fixed slices for system policy, recent raw turns, and a rolling summary of earlier dialogue.