Skip to content
Mastering Claude

Home / The Claude API for builders

The Claude API for builders6 minApplication

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.

Figure 1

The context window by model, on the API side

1,000,000tokens
context window at standard pricing for Claude 4.6 and later generations, including Fable 5.1, Fable 5, Opus 5, Opus 4.8, Opus 4.7, Opus 4.6, Sonnet 5 and Sonnet 4.6
platform.claude.com, 2026-09-02
200,000tokens
context window of models before the 4.6 generation, including Haiku 4.5
platform.claude.com, 2026-09-02
Two window tiers depending on the model, valid for the Messages API, recorded on 2 September 2026. The values shown in the chat application on paid plans are documented separately and do not apply here. Address at the end of the lesson.
Calibrate it yourself

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 to remember
  • 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.
Do this now

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.

Check the source

Every datable claim in this lesson links here to the public text behind it. A source that does not open proves nothing.