Skip to main content

Claude API Context Windows and Tokens: Limits and Management

What counts against the context window, what happens past the limit, and how to manage tokens with counting, max_tokens, and compaction.

ByUpdated

In the Claude API, the context window is the total capacity a model can handle in one request. Your input — system prompt, conversation history, attached documents, tool results — and the model’s generated output all count against the same limit. This guide covers what goes into the context window, what happens when you exceed it, and how to plan and manage tokens, based on the official docs. (Per-model limits and behavior may change — see the official context windows docs.)

🟢 Model references match the current lineup · model notice · Fable subscription
🟢 Model references match the current lineup · Claude Opus 5 / Claude Sonnet 5 / Claude Haiku 4.5 (higher tier: Claude Fable 5.1). This notice changes only when Anthropic ships a new model.

Fable 5 and 5.1 subscription (updated September 7, 2026): Claude Fable 5.1, released September 1, 2026, is the current Fable model and Fable 5 is now legacy. Plan terms are the same for both — Max and Team Premium plans include Fable at up to 50% of the weekly usage limit; Pro and Team Standard use usage credits (

🟢 Model references match the current lineup · Claude Opus 5 / Claude Sonnet 5 / Claude Haiku 4.5 (higher tier: Claude Fable 5.1). This notice changes only when Anthropic ships a new model.
0/M input, $50/M output tokens). The one-time
🟢 Model references match the current lineup · Claude Opus 5 / Claude Sonnet 5 / Claude Haiku 4.5 (higher tier: Claude Fable 5.1). This notice changes only when Anthropic ships a new model.
00 credit applied only to the Fable 5 transition and is not offered for 5.1. Some coding and debugging requests may be answered by an Opus model due to a security classifier (both models). See the Fable 5.1 guide and the Fable 5 availability guide for details.

Context window — input and output share one capacity Input tokens system prompt · history · documents · tool results Output tokens the generated response Total = context window (200K–1M tokens by model) - As a conversation grows, every prior turn stacks up as input, shrinking what remains. - Previous thinking blocks: kept by default on current models (count as input); stripped on older/Haiku. - Exact per-model limits: see the official model comparison table (may change).

What counts against the window

Tokens in a request are not just the reply text. The system prompt, the accumulated conversation history (every prior user and assistant turn), attached documents and images, tool definitions, and tool results all count as input tokens — and the output the model generates must fit inside the same limit. The context window varies by model — current top models (Opus 5, Sonnet 5, Fable 5.1) support 1M tokens, while Haiku 4.5 supports 200K tokens (subject to conditions — check the official model comparison table). In multi-turn conversations and agentic workflows, tool results accumulate at every step, so the real thing to manage is usually cumulative growth rather than a single request.

Thinking blocks depend on the model. With extended thinking, thinking tokens are billed as output within max_tokens, but whether previous turns’ thinking blocks stay in the context varies by model as of the September 2026 docs: current models — Opus 4.5 and later Opus, Sonnet 4.6 and later Sonnet, Fable 5.1 — keep them by default and they count toward the context window like any other input tokens, while earlier Opus/Sonnet models and all Haiku models have them stripped automatically by the API. To drop them on current models, use thinking block clearing in context editing.

What happens when you exceed the limit

Per the official docs, behavior differs by model generation. On Claude 4.5 models and newer, the API accepts a request even if input + max_tokens exceeds the window; if generation actually reaches the limit, it stops with stop_reason: "model_context_window_exceeded". Earlier models returned a validation error instead (a beta header opts into the newer behavior). If the response is cut off by max_tokens, you get stop_reason: "max_tokens". In both cases, check stop_reason in your code and handle it — warn the user, continue generation, and so on. For error handling in general, see the errors and rate limits guide.

Three tools for planning and managing tokens 1. Token counting API Count input tokens before sending the request Same format incl. system prompt, tools, images, and PDFs Prevents overflows and surprise costs 2. max_tokens Caps the output length When reached, response ends with stop_reason: "max_tokens" Output rate limits (OTPM) count actual generation — no penalty for a high cap 3. Compaction & editing The primary strategy for long conversations and agents Server-side compaction summarizes history; context editing clears tool results and thinking blocks Count before sending -> cap the output -> manage accumulation

Token counting API — count before you send

The starting point for limit management is the token counting API. It accepts the same structured input as message creation (including system prompts, tools, images, and PDFs) and returns the total input token count. Call it before the real request to avoid overflows and unintended long-context costs. Since tokens are cost, pair this with the cost optimization guide (prompt caching and batches).

max_tokens and rate limits

max_tokens caps output length. Per the official rate limits docs, output-tokens-per-minute (OTPM) limits are evaluated in real time on actually generated tokens; the max_tokens value itself does not factor into OTPM. So setting it generously to avoid truncation carries no rate-limit downside.

Long conversations: use compaction

As a conversation approaches the limit, the official docs recommend server-side compaction (summarizing the history) as the primary strategy, with context editing for finer control such as clearing tool results and thinking blocks. For app-side tips on long chats in claude.ai, see keeping context in long conversations. For choosing and pinning model IDs, see model IDs and versioning.

The limits here (200K, 1M) and behaviors reflect the official documentation as of September 2026 (re-checked against the context-windows and models overview docs); they vary by model and plan and may change. For exact per-model context sizes, see the official docs and the model comparison table. This site is not an official Anthropic site.

See also: Claude Model Comparison Guide

Curious what your usage would cost? Try the API cost calculator — pick a model and token counts for an instant estimate.

Was this helpful?

Keep reading