Skip to main content

Tokens, Context & Pricing

Beginner
What you'll learn
  • 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.

Pro tip
  • 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.

LimitWhat it capsWhen to change it
max_tokensThe length of the reply (output only)Raise it if output gets cut off
Context windowTotal budget for input + outputBig inputs leave less room for output
Watch out
  • 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
  1. Start with Sonnet; reserve Opus for the hard parts. See Choosing a Model (/docs/api/choosing-a-model).

More strategy in Cost & Latency Tradeoffs.

Check yourself

0/4
  1. You're planning a request and want to know its token count before sending. What should you use?
  2. Your reply keeps getting cut off mid-sentence. Which setting do you change?
  3. How are you billed on the API?
  4. Which is the best first move to cut cost without losing quality?
Key takeaways
  • 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.

Next