11. Caching, tokens, cost

7 cards

Review this deck
All cards in this deck

Prompt caching is prefix matching

Render order `tools` → `system` → `messages`; changing one byte in the prefix loses the cache for everything after it.

Common cache breakers

Timestamps or IDs in the system prompt, JSON with unstable key order, changing tool lists, prefix below the minimum threshold, switching models.

Verifying a cache hit

`usage.cache_read_input_tokens` greater than 0 on repeated requests with the same prefix.

Cache pricing

5-minute write 1.25x, 1-hour write 2x input price; reads 0.1x on most models, but 0.05x on Opus 5.5 and Sonnet 5.5.

Batch API

50% off both input and output for work that does not need an immediate answer; stacks with cache discounts.

Counting tokens

The `count_tokens` endpoint (`client.messages.countTokens` / `count_tokens`) with the exact model ID; do not use `tiktoken`.

Order of cost levers

Cache → trim excess input → cap output → Batch → lower effort → switch to a smaller model (the last two must be measured with evals).

11. Caching, tokens, cost – Flashcards – Claude | Học Dev