Context Window

The context window is all the text a language model can reference when generating a response, including the response itself.

Verified

On this page

Definition

The context window refers to all the text a language model can reference when generating a response, including the response itself. The window differs from the large training corpus: it represents the model's working memory. Anthropic states that a larger context window allows the model to handle more complex and lengthy prompts, but more context is not automatically better.

Anthropic calls the decline in accuracy and recall as token count grows context rot. Chat interfaces can also manage the context window on a rolling first in, first out basis.

At a glance

The context window, up to 1M tokens depending on the model, holds the conversation history plus the new output Claude generates. In the document's standard API request diagram, user messages and assistant responses accumulate through turns, and previous turns are preserved completely. The diagram and the thinking-block stripping example below describe separate cases.

Diagram: Input phase, in the standard API request diagram: all previous conversation history plus the current user message. > Output phase: generates a text response that becomes part of the input for the next turn.1Input phase, in the standard API requestdiagram: all previous conversation historyplus the current user message.2Output phase: generates a text response thatbecomes part of the input for the next turn.

How it works

Everything in the request counts toward the context window: the system prompt, every message in messages (including tool results, images, and documents), and the tool definitions. The output Claude generates for the turn, including its extended thinking, counts too. Every response reports what the request consumed in its usage field.

To estimate a request before sending it, use the token counting API. For models with a 1M-token context window, 1M is the default: a beta header is not needed, and long-context requests are billed at standard pricing, except on Claude Haiku 5.5, where prompts over 100,000 tokens cost more. Other Claude models, including Claude Sonnet 4.5 (deprecated), have a 200k-token context window.

A single request can include up to 600 images or PDF pages, and up to 100 for models with a 200k-token context window. With thinking, input and output tokens, including thinking tokens, count toward the context window limit, with a few nuances in multi-turn situations. At a new user turn after a completed tool-use cycle, on models that strip previous thinking blocks, the API automatically drops that cycle's thinking block when it is passed back.

Thinking tokens are billed as output tokens once, when they are generated. Cached prompt prefixes still occupy the context window; prompt caching changes how you pay for those tokens and does not stop them from counting. Compaction automatically summarizes earlier parts of the conversation on the server, so the conversation can continue past the context window limit.

Compaction is available in beta for Claude 4.6 or later models and Claude Mythos Preview. If the input alone already exceeds the model's context window, the API returns a 400 error with the message prompt is too long. On Claude 4.5 models or newer, the API accepts a request when input tokens plus max_tokens exceeds the window. If the input alone already exceeds it, the API returns a 400 error.

On Claude 4.5 models or newer, if generation then reaches the context window limit, it stops with stop_reason model_context_window_exceeded. On earlier models, the API returns a validation error instead. Claude Sonnet 5, Claude Sonnet 4.6, Claude Sonnet 4.5 (deprecated), and Claude Haiku 4.5 have context awareness: they track their remaining context window, their token budget, throughout a conversation.

For these models, the API gives Claude its total context window in the system prompt of every request. For the context-awareness models listed above, the API updates Claude on its remaining capacity after each tool call.

Example

The documented example combines thinking with tool use on a model that strips previous thinking blocks. In the example's first turn architecture, the input components are the tools configuration and the user message. The output components of that turn are thinking, a text response and a tool use request.

In the second turn, which handles the tool result, the input components are every block in the first turn and the tool_result. In this example with a model that strips previous thinking blocks, Claude responds with only text after the tool results are passed back, with no additional thinking until the next user message unless interleaved thinking is enabled. In the third turn, a new user turn, Claude generates a new thinking block and continues from there, because the new user turn is outside the tool use cycle.

At the new user turn after the completed tool-use cycle, the previous thinking tokens no longer count toward the context window on models that strip previous thinking blocks. The other previous blocks still count toward the context window, as does the thinking block in the current assistant turn.

Sources

This page is written from the official sources below. Open them to check the original text.

Verified ·

This page is written by AI from the official sources above and checked automatically. This site has not tested it. Found a mistake? Tell us.