Thinking and the context window
max_tokens, which includes all thinking Claude generates in the current turn, is enforced as a strict limit. On Claude 4.5 models and newer, if input tokens plus max_tokens exceeds the context window size, the API accepts the request. If generation then reaches the context window limit, it stops with stop_reason: "model_context_window_exceeded" instead of returning an error. On earlier models, the API returns a validation error instead. See Handling stop reasons.
How thinking counts against the window depends on when it was generated:
- Current-turn thinking always counts toward
max_tokens, is billed as output tokens, and occupies context window space for the turn that generated it. - Prior-turn thinking depends on the preservation default. On models that keep all prior turns, previous thinking blocks remain in context, count toward the window, and are billed as input tokens like the rest of the conversation history. On models that keep only the last turn, the API strips older thinking blocks automatically when you pass them back, so they don't consume window space or input tokens.
In practice:
- On keep-all models, budget your context window as if thinking were ordinary conversation history, because it is. Long agentic sessions accumulate thinking in context. Use thinking block clearing if you need to reclaim space.
- On last-turn-only models, thinking is a per-turn cost only: each turn's thinking counts against that turn's
max_tokensand then drops out of the window.
The following diagrams illustrate the last-turn-only (stripping) regime. The first shows a multi-turn conversation: each turn's thinking block is generated in the output but not carried into later turns' input.
The second shows the same regime with tool use: thinking stays in context alongside its tool result for the duration of the assistant turn, then drops out on the next user turn.
Use the token counting API to get accurate counts for your specific use case, especially for multi-turn conversations that include thinking.