Home / The Claude API for builders
Counting before sending: the context window
A dedicated counting endpoint returns the number of tokens in a request before it is sent, and the context window to compare against this figure is no longer one shared value across a whole model range, it now varies sharply from one family to another.
A request sent to the Messages API is rejected if it exceeds the context window of the model called, the sum of input tokens, output tokens and any cached content. A dedicated counting endpoint makes it possible to know this size before even sending the full request.
The dedicated counting endpoint
The API exposes an entry point distinct from the Messages API itself, POST /v1/messages/count_tokens, which accepts the same request structure, the same messages array, the same declared tools, and returns only the number of tokens that request would consume, without generating a response. Comparing this figure to the target model's context window before sending detects an overrun without waiting for a server error, and estimates the cost of the request in advance.
A window that now varies sharply from one model to another
The context window is no longer one value shared across a whole model range: it depends on the exact family called, and a more recent model does not necessarily carry the widest window in its own catalogue. The figure in this lesson gives the exact detail by model, with its source and its verification date, since a technical limit of this kind becomes outdated as soon as a new generation of models ships.
Not confusing the API with the chat application
The same model name can carry a different context window depending on whether it is called from the Messages API or from the chat application, where a paid plan applies its own display limit independent of the protocol. The figure in this lesson only documents the value that applies to a request sent through the API, the only one relevant to code you write yourself. The support article that details the tiers by paid plan only documents the chat application, never the Messages API: the figure in this lesson only carries the value that applies on the API side. The same stable prefix that fills cached memory counts in full toward this total, marking a block as reusable does not remove it from the window calculation.
import anthropic
client = anthropic.Anthropic()
messages = [{"role": "user", "content": "Resume ce texte : " + "phrase de test. " * 200}]
compte = client.messages.count_tokens(
model="claude-sonnet-5",
messages=messages,
)
fenetre_du_modele = 1_000_000 # confirmed for this model in the figure of this lesson
max_tokens_prevus = 1024 # output tokens the response can produce at most
if compte.input_tokens + max_tokens_prevus > fenetre_du_modele:
print("depassement de la fenetre de contexte")
else:
print("jetons dans la requete :", compte.input_tokens)
The script counts the tokens of a request built on the spot, without reading any project file, then compares this total to the target model's window before sending anything. The same structure works to check a longer batch of messages, or a request that includes declared tools.
The context window by model, on the API side
A developer calls the dedicated counting endpoint on a request built by a script for the Claude Sonnet 5 model. The counter returns a total of 1,240,000 tokens for this request.
Write, in one sentence, what this situation establishes, and in one sentence what it does not establish.
What this establishes: This count establishes that the request as built requires at least 1,240,000 input tokens for this specific model, before any actual sending.
What this does not establish: It does not establish that this request will exceed the model's context window, since the comparison to the exact figure for that window was not made in the statement.
The three most common miscalibrations
- Too broad This count establishes that any request built by this same script will always stay under the context window of any model in the range.
- Too narrow This count establishes nothing at all, since a single call to the counting endpoint never informs on the real size of a request.
- Off target This count shows that the dedicated counting endpoint bills output tokens at the same rate as the Messages API.
- The dedicated counting endpoint, POST /v1/messages/count_tokens, returns the number of tokens in a request without generating a response.
- The API's context window is no longer one value shared across a whole model range, it now varies sharply from one family to another.
- The context window shown by the chat application for a given model is not necessarily the one that applies to that same model called from the API.
- Comparing the count result to the target model's window before sending detects an overrun without waiting for a server error.
Write a script that builds a test request with content you generate yourself, call the dedicated counting endpoint for the model of your choice, and compare the result to the context window shown in this lesson's figure.
Every datable claim in this lesson links here to the public text behind it. A source that does not open proves nothing.
- Anthropic, pricing and long context window consultée le 2026-09-02
- Anthropic, context window size by model and plan, chat application consultée le 2026-09-02