Claude API Context Window Token Limits Explained
Claude API Context Window Token Limits Explained
The Claude API context window determines how much text — prompt, conversation history, and system instructions combined — a model can process in a single request. For current Claude models (Claude 3.5 and Claude 4 families), the standard context window is 200,000 tokens. Some models offer an extended 1,000,000-token context window in beta for specific use cases like large codebase analysis or multi-document review, though this requires explicit opt-in and may carry different pricing.
Output is capped separately from input. Most Claude models support up to 8,192 output tokens per response, with some newer models allowing up to 64,000 output tokens when extended thinking or long-form generation is enabled. Understanding the difference between input context and output limits is the key to avoiding truncated responses and unexpected max_tokens errors.
What Counts Toward the Context Window
The context window is shared across everything in a request:
- The system prompt
- All prior user and assistant turns in a multi-turn conversation
- Tool definitions and tool call results
- Any documents, images, or file content included in the message
- The model's own response (output tokens count against the same budget in some billing calculations, though not against the input context limit itself)
If your cumulative input exceeds the model's context window, the API returns an error rather than silently truncating your prompt. This is different from some other providers that quietly drop the oldest messages.
How Tokens Are Counted
Claude uses a tokenizer that doesn't map 1:1 to words. As a rough estimate:
- 1 token ≈ 4 characters of English text
- 100 tokens ≈ 75 words
- A typical page of text (500 words) ≈ 650–700 tokens
Code, non-English text, and structured data (JSON, XML) often tokenize less efficiently than plain English prose, so a 200K-token window can fill up faster than expected when working with large codebases or heavily formatted documents.
curl https://api.subtoapi.app/v1/messages \
-H "Authorization: Bearer $SUBTOAPI_KEY" \
-H "Content-Type: application/json" \
-d '{
"model": "claude-sonnet-4",
"max_tokens": 1024,
"messages": [
{"role": "user", "content": "Summarize this 50-page report..."}
]
}'
If this request fails with a context-length error, the fix usually isn't a different model — it's trimming the input.
Why Context Limits Matter in Practice
Hitting the context window ceiling shows up in a few common scenarios:
- Long-running chat sessions — a support bot or coding assistant that keeps the full conversation history in every request will eventually exceed 200K tokens, especially with verbose tool outputs.
- Large document ingestion — feeding entire PDFs, log files, or repositories directly into the prompt instead of chunking or retrieving relevant sections.
- Agent loops with tool use — each tool call and its result adds tokens to the running context, so autonomous agents that call tools dozens of times per task can balloon quickly.
The practical fix in all three cases is the same: don't send more than the model needs.
Managing Context Window Limits
Truncate or summarize history
For long conversations, keep only the most recent N turns verbatim and replace older turns with a compressed summary. This keeps the system prompt and recent context intact while discarding stale detail.
Use retrieval instead of raw dumping
Rather than pasting an entire knowledge base into the prompt, retrieve only the relevant chunks (via embeddings or keyword search) and include those. This keeps requests well under the context ceiling and usually improves answer quality, since the model isn't searching for a needle in a haystack of irrelevant text.
Monitor token usage per request
Every Claude API response includes usage metadata showing input and output token counts. Tracking this lets you catch creeping context growth before it causes failures in production. If you're calling the Claude API through SubToAPI, every response includes this usage data out of the box, and you can review per-key and per-team consumption in the dashboard without instrumenting your own token counter. See the Messages docs for the exact response shape.
Cache what doesn't change
If your system prompt or reference documents are static across many requests, prompt caching reduces both the effective cost and latency of re-sending that content, though it doesn't change the hard context window ceiling itself — it just makes staying under it cheaper.
Stream for long outputs
When you expect a long response near the output token limit, use streaming so you can start processing (and displaying) tokens as they arrive rather than waiting for the full generation to complete. This matters more for user experience than for the limit itself, but it's worth pairing with output-heavy requests. Details are in the streaming docs.
Choosing the Right Model for Your Context Needs
Not every task needs the maximum context window. If you're summarizing a single email or answering a short question, a smaller context budget is fine and often cheaper. Reserve the full 200K (or extended 1M) context for genuinely large inputs — full codebases, long transcripts, multi-document comparisons — where splitting the input would lose important cross-references.
If you're building this on top of the Claude API through an API gateway like SubToAPI, you get the same model context limits as calling Anthropic directly, plus application-level API keys and usage metadata per key, which makes it easier to see which part of your product is consuming the most context. Check the pricing page for plan details, or start with the quickstart guide to get your first request running in minutes.
questions
Does the context window include both my prompt and Claude's response? The context window limit applies to the input (system prompt, messages, tool results). Output is governed by a separate max_tokens parameter, though total conversation length across multiple turns still accumulates toward the input limit on subsequent requests.
What happens if I exceed the context window? The API returns an error indicating the request exceeds the model's context length — it does not silently truncate. You need to reduce input size, summarize history, or switch to a model with a larger context window before retrying.
Is a 1-million-token context window available for all Claude models? No. Extended context windows are available only for specific models and typically require opt-in; the standard context window across most current Claude models is 200,000 tokens. Check current model documentation before building around the larger limit.