The short answer
#The context window is the total number of tokens a model can work with in a single request: everything you send (system prompt, tool definitions, conversation history, the new message) plus everything it generates in reply. Current Claude models have a 1M-token window. If a request does not fit, the API rejects it with an HTTP 400 before the model runs. Nothing is silently dropped, so you must keep requests inside the window yourself.
_10{_10 "type": "error",_10 "error": {_10 "type": "invalid_request_error",_10 "message": "prompt is too long: 1048830 tokens > 1000000 maximum"_10 }_10}
With the TypeScript SDK that arrives as an Anthropic.BadRequestError with status 400. Catch it, do not retry it: the same request will fail the same way.
What it means
#Input and output share the window. A model generates one token at a time and each new token is appended to the context before the next is produced, so the answer has to fit in the same space as the question. On current Claude models, if your input tokens plus max_tokens would exceed the window, the request fails validation. Other providers may instead shrink the output budget quietly, so check your provider's docs. Either way, a huge input leaves less room for the reply.
The API is stateless. There is no server-side memory of a chat. Every request resends the full history, so a conversation grows toward the limit turn by turn, and so does its cost. A chat app that never trims will eventually hit the wall, usually in production on the user who talks the most.
A long window is capacity, not comprehension. A million tokens of room does not mean the model weighs every token equally. Models attend most reliably to material near the start and the end of a prompt, and recall of a detail buried in the middle of a very long context is weaker than recall of the same detail in a short, focused prompt. Longer prompts are also slower and dearer on every turn. Treat the window as a ceiling you stay well under, not a target to fill.
How to stay inside it
#Measure before you send
countTokens gives you the exact input count for the model you are using. Check it against your own budget, not the hard limit, and leave room for max_tokens.
_10const { input_tokens } = await client.messages.countTokens({_10 model: 'claude-opus-5-5',_10 system,_10 messages,_10});_10_10const MAX_OUTPUT = 4096;_10if (input_tokens + MAX_OUTPUT > 200_000) {_10 messages = trimHistory(messages);_10}
Trim the history
The simplest strategy is a sliding window: keep the system prompt, keep the most recent N turns, and drop the oldest. Drop whole user and assistant pairs together so the sequence stays well formed, and if a turn contains a tool call, keep its tool result with it.
_10function trimHistory(messages: Anthropic.MessageParam[], keepTurns = 10) {_10 // Each turn is a user message followed by an assistant message_10 const keep = keepTurns * 2;_10 return messages.length <= keep ? messages : messages.slice(-keep);_10}
Summarise older turns
When the early part of a conversation matters (requirements, decisions, the user's name), dropping it loses information. Instead, ask the model to summarise the older turns into a few hundred tokens and replace them with that summary as the first user message. Run the summary step with a cheaper model and low effort; it does not need deep reasoning.
_10const summary = await client.messages.create({_10 model: 'claude-haiku-5-5',_10 max_tokens: 600,_10 output_config: { effort: 'low' },_10 messages: [_10 ...oldTurns,_10 { role: 'user', content: 'Summarise the conversation so far in under 300 words, keeping all decisions and constraints.' },_10 ],_10});
Retrieve only what is needed
Do not paste an entire knowledge base into every request. Split documents into chunks, store them, and pull in only the handful that are relevant to the current question. That is retrieval-augmented generation, and it keeps prompts small, cheap and focused even when the underlying data is enormous. A few relevant pages beat a thousand irrelevant ones.
Put the instructions near the end
When you must include a long document, place it first and put the question and any instructions after it. The model attends well to the end of the prompt, so this puts your actual request in the strongest position and the bulk material where the model can look it up.
_10const content = `<document>\n${longDocument}\n</document>\n\nUsing only the document above, answer: ${question}`;
Common pitfalls
#- Treating 1M tokens as "paste everything". You pay for every token on every turn, latency grows, and attention to the middle gets weaker. Send what is relevant.
- Forgetting that
max_tokenscounts. A request that fits on input alone can still fail if you ask for a 128K-token reply on top. - Measuring in characters. Four characters per token is a prose average. Code, JSON and non-English text run higher. Use
countTokens. - Trimming half a tool exchange. A
tool_useblock without its matchingtool_result, or the reverse, is an invalid conversation and returns a 400 of its own. - Summarising away the constraints. A summary that drops "the user is on a free plan" or "never suggest library X" changes the model's behaviour. Tell the summariser what to keep.
- Retrying the error. The SDK retries 429s and 5xx responses for you. A 400 for an oversized prompt is deterministic; fix the request instead.
Related reading
#quick question
Was this resource helpful?
by Danny