Tokens, Context & Pricing
- Count tokens correctly with Anthropic's own tooling (not another model's tokenizer)
- Tell max_tokens apart from the context window — and why the difference matters
- Estimate API cost from input + output tokens at per-model rates
- Cut cost without losing quality: right-size, cache, trim, batch
Cost and limits on the API are all measured in tokens (~¾ of a word). Three things to get right.
1. Count tokens correctly
Don't guess, and don't use another model's tokenizer (e.g. tiktoken) — token counts differ per model family. Use Anthropic's token counting endpoint/SDK helper to measure a request before sending it.
- Rough planning rule: ~750 words ≈ ~1,000 tokens (a token is roughly ¾ of a word).
2. max_tokens ≠ context window
These two limits are easy to confuse, but they govern different things.
| Limit | What it caps | When to change it |
|---|---|---|
max_tokens | The length of the reply (output only) | Raise it if output gets cut off |
| Context window | Total budget for input + output | Big inputs leave less room for output |
- Set max_tokens to what the task needs. Too low truncates the reply. Needlessly high doesn't cost more — you pay only for tokens actually generated — but it can let replies ramble.
3. Estimate cost
You're billed for input tokens + output tokens, at per-model rates (Opus > Sonnet > Haiku). A quick estimate:
cost ≈ (input_tokens × input_rate) + (output_tokens × output_rate)
Get the current rates from the official pricing page — we don't hard-code them here on purpose.
Cutting cost (without losing quality)
Guided walkthrough1 of 4
- Start with Sonnet; reserve Opus for the hard parts. See Choosing a Model (/docs/api/choosing-a-model).
- Reuse a stable prompt prefix across calls. See Prompt Caching (/docs/api/prompt-caching).
- Send only the context that matters. This is also where RAG (/docs/foundations/rag) helps.
- Batch jobs where latency doesn't matter.
More strategy in Cost & Latency Tradeoffs.
Check yourself
0/4- Everything is measured in tokens (~¾ of a word); ~750 words ≈ ~1,000 tokens.
- Count tokens with Anthropic's own tooling — never another model's tokenizer.
- max_tokens caps the reply; the context window is the total input + output budget.
- Billing = input tokens + output tokens at per-model rates (Opus > Sonnet > Haiku).
- Cut cost by right-sizing the model, caching prefixes, trimming inputs, and batching.