Cost, Latency & Token Optimization Techniques
CoreOptimize cost, latency, and token usage while verifying against the eval · Difficulty 3/5
Explanation
Optimization is a tradeoff against measured quality -- never blind. There are five standard levers for reducing cost, latency, and token usage in a Claude system.
The Five Levers
- **Prompt Caching** -- cut latency and cost on repeated stable prefixes with no accuracy loss (order stable content first, so the cacheable portion is contiguous)
- Right-size the model -- route steps to the cheapest tier that still passes the eval; use Opus/extended thinking only where the task actually needs that capability
- Trim the context -- prune stale tool output and over-fetched chunks; fewer tokens means lower cost and less context rot
- Batch -- batch latency-tolerant bulk jobs to cut cost; the Batches API is async and cheaper, but it does not reduce per-request latency
- **Cap `max_tokens`** -- bound output cost by setting an appropriate ceiling
Why These Five, and Not Others
Each lever targets a different cost driver: caching targets repeated input tokens, right-sizing targets per-token pricing, context trimming targets total input volume, batching targets throughput economics (not latency), and capping max_tokens targets output volume. Applying the wrong lever to the wrong driver -- e.g., expecting batching to speed up a single latency-sensitive request -- doesn't produce the expected saving.
Key Takeaways
- Prompt caching cuts cost/latency on stable repeated prefixes with no accuracy loss -- order stable content first
- Right-size the model: route each step to the cheapest tier that still passes the eval
- Trimming context reduces both cost and context rot at the same time
- The Batches API cuts cost for latency-tolerant bulk jobs but does not reduce per-request latency
- Capping max_tokens bounds output cost
Glossary Terms
The practice of directing requests to different Claude model tiers based on assessed complexity and requirements. A common pattern uses a fast, cheap model (Haiku) to classify task complexity, then routes to Sonnet or Opus accordingly.
API parameter that sets the maximum number of tokens Claude will generate in a single response. If generation would exceed this limit it is truncated and `stop_reason` is set to `"max_tokens"`. This is a required parameter — omitting it returns a 400 error.
An asynchronous Claude API for processing multiple requests in a batch with 50% cost savings versus synchronous requests. Processing takes up to 24 hours with no guaranteed latency SLA. Does not support iterative tool use, streaming, or prompt caching. Best for scheduled, non-blocking analysis.
A Claude API feature that caches frequently-used prompt content (system prompts, large documents, tool definitions) to reduce cost and latency on repeated API calls. Cached tokens are billed at a discounted rate. Cache has a TTL that resets on each use. Must be enabled by marking content with cache_control.
Related Concepts