Model Selection and Optimization
Cost Diagnosis and Optimization
Exam objective: Analyzing usage or cost data to identify the dominant cost driver, selecting the optimization that addresses it, such as prompt caching (single, layered, and rolling breakpoints and checkpoint placement), batch processing, or token budgets, and quantifying the expected impact.
What the exam tests
Whether you can read usage or cost data, name the dominant driver (repeated input, one-off input, output and thinking, or an unbounded agentic loop), pick the optimization that addresses that driver, and estimate the effect.
Key rules
Diagnose first
- Total input =
cache_read_input_tokens+cache_creation_input_tokens+input_tokens. The last one counts only tokens after the last breakpoint. - Price each category separately: uncached input, cache writes, cache reads, output. Thinking is billed as output.
- Organization-wide: Admin API usage and cost reports (group by model, workspace). In the Agent SDK use
modelUsagefor totals that include subagents; dollar fields are estimates. - The token counting endpoint is free (rate limited) and returns an estimate; count with the model you will use.
Prompt caching
- Prefix order is
tools→system→messages. A change invalidates its level and everything after it. - Writes happen only at breakpoints. Put
cache_controlon the last block that is identical across requests. - Single: one breakpoint at the end of the static prefix. Layered: up to 4, separating sections with different change rates. Rolling: top-level automatic caching follows a growing conversation, and combines with explicit breakpoints (it takes one slot).
- Lookback checks 20 positions per breakpoint. If a turn adds more, keep another breakpoint near the previous write.
- Standard multipliers on base input: 5-minute write 1.25x, 1-hour write 2x, read 0.1x (lower on some newer models). Break-even is one read (5m) or two (1h).
- Longer TTLs must come before shorter ones. Below the model's minimum length, nothing is cached and no error is raised.
{"cache_control": {"type": "ephemeral"},
"system": [{"type": "text", "text": "<stable manual>",
"cache_control": {"type": "ephemeral"}}],
"messages": [...]}
Batch processing
- 50% off input and output; most batches finish within 1 hour, and they expire at 24 hours.
- Results arrive in any order: match on
custom_id. Nostream,speed,fallbacksormax_tokens: 0. - Stacks with caching, but hits are best-effort. Use identical prefixes and the 1-hour TTL, ideally written by a one-request batch first.
Task budgets (beta)
output_config.task_budgetgives Claude a countdown for a whole agentic loop. Advisory;max_tokensis the enforced per-request cap. Minimum 20,000; not on every model, and not in Claude Code.- Size from measured spend (start near p99). Set once; use
remainingonly when your code compacts or rewrites history.
Quantify
Example with base input $2/MTok, a 40,000-token prefix, 200 requests within the TTL: uncached $16.00; cached = 1 write ($0.10) + 199 reads ($1.59) = about $1.69.
Traps
- A timestamp or request ID before the breakpoint: a write every time, never a read. Automatic caching does not help if the last block varies.
- Choosing the 1-hour TTL when traffic already refreshes the 5-minute entry.
- Firing parallel requests before the first response has started: the entry is not yet available.
- Changing
tool_choice, images, thinking or effort mid-conversation can invalidate cached content. - Decrementing
task_budget.remainingwhile resending full history: under-reports the budget and breaks the cache. - Optimizing input when output dominates, or batching work a user is waiting for.
- Reading
input_tokensas the whole prompt.
Decide quickly
| Situation | Choice | Why |
|---|---|---|
| Same large prefix, frequent requests | Breakpoint at end of prefix | Reads replace full-price input |
| Sections change at different rates | Layered breakpoints | Earlier layers survive later edits |
| Growing conversation | Automatic caching | Breakpoint advances itself |
| Reuse gaps of 5–60 minutes | 1-hour TTL | Avoids repeated writes |
| Offline bulk work | Message Batches | Half price |
| Output and thinking dominate | Lower effort, verify quality | Targets the driver |
| Long loop must pace itself | Task budget plus max_tokens | Soft target plus hard limit |
| Unexplained cache misses | Cache diagnostics | Names the earliest divergence |