What is included in the context window?
According to Anthropic's Claude Platform documentation, the context window includes the text a language model can draw on when generating a response. The generated response is also included in the window. This window is separate from the full body of data used to train the model. Anthropic describes the window as the model's working memory.
In a standard API flow, user messages and assistant responses accumulate in the window as the conversation progresses. The input for each turn includes the previous conversation history and the user message for that turn. The text response generated in that turn also becomes part of the input for the next turn.
In a Claude API request, the system prompt, every message in messages, and tool definitions count toward the window. Tool results and images in messages are also included in this calculation. The output Claude generates in that turn and extended thinking content also use the window.
The usage field in every Claude response reports how much the request used. When prompt caching is used, the input count is split among the input_tokens, cache_read_input_tokens, and cache_creation_input_tokens fields. According to Anthropic, all three count toward the window. Cached prompt prefixes also take up space in the window.
Anthropic describes using the token counting API to estimate token usage before sending messages to Claude. It presents server-side compaction for long-running conversations and agentic workflows. This method summarizes earlier parts of the conversation on the server, allowing the conversation to continue beyond the window limit.
What information enters the context?
In Anthropic's explanation of context engineering, context is defined as the set of tokens included when sampling from a large language model. Context engineering covers strategies for selecting and maintaining this set of tokens, including information that comes from outside the prompt during inference.
As an agent works in a loop, it generates more data that may be relevant to the next inference turn. According to Anthropic, this information is filtered again throughout the loop. Deciding what to pass to the model each time is a recurring part of the process. The managed state can include components such as system instructions, tools, MCP, external data, and message history.
More context does not always mean better results. Anthropic calls the decline in the ability to accurately recall information from context as the token count increases context rot. In long contexts, the accuracy of information retrieval and reasoning across distant sections can decline compared with shorter contexts. This change is described as a gradual difference in performance rather than a sharp break.
Anthropic explains that in the transformer architecture, each token can attend to other tokens throughout the context. As the context grows, the model's capacity to capture these relationships is strained. Anthropic also notes that short sequences are generally more common than long sequences in the training distributions in which models learn attention patterns.
Organizing prompt and tool content
Anthropic recommends clear and direct language for the system prompt. The balance it describes lies between rigid, complex hardcoded logic at one extreme and vague guidance that gives no concrete indication of the expected output at the other. Complex logic can increase the maintenance burden; overly general guidance can assume shared context.
Anthropic recommends organizing the prompt into separate sections and using XML tags or Markdown headings to separate them. Example section names include <background_information>, <instructions>, Tool guidance, and Output description. According to Anthropic, keeping the set of information needed to describe the expected behavior small does not necessarily mean it must be brief.
The testing process Anthropic recommends starts by trying a minimal prompt on the task. Explicit instructions and examples are added based on the types of errors seen in the initial tests. For examples, it recommends varied, representative examples that show the expected agent behavior rather than listing every possible edge case.
Tools allow the agent to work with its environment and receive additional context during execution. Anthropic recommends that tool outputs provide token-efficient information and that the purposes of tools be clear. It also specifies that input parameters should be defined descriptively and without room for interpretation. A tool set that is too broad or has overlapping functions can create uncertainty about which tool to use.
Retrieving information during execution
In the just-in-time approach described by Anthropic, the agent keeps lightweight references such as file paths, saved queries, and web links instead of processing all relevant data upfront. It then uses tools to load the data into the context during execution.
In this approach, the metadata of references can also provide guidance. Anthropic gives folder hierarchies and naming conventions as examples. As the agent explores, each interaction can provide context that affects the next decision. File size can offer a clue about complexity, while naming can offer a clue about purpose.