A token is the unit models read text in — a word fragment, roughly speaking. Every request you send to Claude Platform on AWS is measured in tokens three ways at once: billing (dollars per million tokens), rate limits (input and output tokens per minute), and the context window (the maximum a model can hold in one request). Token counting via POST /v1/messages/count_tokens gives you the input-side number before committing to a full inference request. On the IAM side it is its own action, aws-external-anthropic:CountTokens, separate from CreateInference — included in the AnthropicInferenceAccess managed policy.
Calling it
The request shape mirrors messages.create — same model, system prompt, messages, and tools — so the count reflects everything the real request would contain:
from anthropic import AnthropicAWS
client = AnthropicAWS() # AWS_REGION + ANTHROPIC_AWS_WORKSPACE_ID set
count = client.messages.count_tokens(
model="claude-sonnet-5",
system=SYSTEM_PROMPT,
messages=[{"role": "user", "content": assembled_rag_prompt}],
)
print(count.input_tokens) # input-side tokens for this exact request
Counting the pieces separately and adding them up is less reliable than counting the assembled request, because everything counts toward the window together: the system prompt, all messages including tool results and images, tool definitions, and — on the output side — whatever the model generates, including any thinking tokens.
Use 1: cost estimation
Multiply the count by the model's input price to know what a request costs before sending it. At list prices, 40,000 input tokens is $0.20 on Claude Opus 4.8 ($5/MTok), $0.04 on Claude Haiku 4.5 ($1/MTok). Do this across a representative sample of production traffic and you have a defensible per-request cost model — the input for routing decisions ("send short queries to Haiku") and for the traffic model your quota planning needs. One re-baselining warning: Claude Opus 4.7 and later Opus models, Fable 5, and Sonnet 5 use a newer tokenizer that produces roughly 30% more tokens for the same text than Sonnet 4.6 and earlier. Never reuse token counts measured on one tokenizer generation to budget for the other — recount with the target model in the model field.
Use 2: overflow detection for RAG pipelines
Retrieval-augmented generation (RAG) — stuffing retrieved documents into the prompt — is where context overflow actually happens, because prompt size depends on whatever retrieval returned today. If input alone exceeds the model's context window, the request fails with a 400 invalid_request_error ("prompt is too long") after you have already assembled and shipped the whole payload. Counting first turns that failure into a decision point: drop the lowest-ranked chunks, summarize, or split the request.
max_tokens exceeds the window is accepted — and generation simply stops early with stop_reason: "model_context_window_exceeded". Check for that stop reason if truncated answers would silently corrupt your pipeline. So the practical check is: input_tokens + max_tokens ≤ window, not just input_tokens ≤ window.Window sizes vary by model: most current models (Fable 5, Opus 4.8, Sonnet 5, and others) have a 1M-token window — the default, with no long-context price premium — while Claude Haiku 4.5 has 200K. Context windows on Claude Platform on AWS are identical to the first-party API, and you can query each model's max_input_tokens and max_tokens programmatically via the Models API, which this platform supports.
What token counting does not tell you
It returns input tokens only — output length is the model's choice, bounded by max_tokens, so cost estimates need an assumed output size from your own historical data. It also doesn't predict cache behavior: cached prefixes change what you pay and what counts against cache-aware rate limits, but they still occupy the context window. For teams with Zero Data Retention arrangements, note that the Token Counting API is covered by ZDR.
Where to go next
Feed the numbers into quota management and prompt caching; for model selection economics, see the feature matrix.