What you’ll be able to do
- Reduce token usage through context trimming, output limits, caching and batching without breaching the quality bar
- Reduce latency, both perceived and total, with streaming, parallelism, shorter outputs and model routing
- Choose configurations on the cost-quality frontier using evaluation data and cost per successful task
- Design per-request logging, dashboards and alerts that detect quality, cost and latency drift in production
- Run online evaluation on sampled production traffic and feed findings back into the offline dataset
What you need to know
Optimize under the quality bar
Every optimization here is a trade, and the eval suite (4.2) is what keeps it honest. The working rule: a change that cuts cost or latency ships only if the primary metric holds and no guardrail breaks (4.1). Measure the effect in cost per successful task, since a cheaper configuration that fails more often can cost more once retries and escalations are counted.
Token levers, roughly in order of effort
- Send less context. Retrieve fewer, better-ranked chunks instead of padding with marginal ones; drop stale conversation turns or summarize them; remove unused tool definitions, which are sent on every call. Often this improves quality as well, because relevant material is less diluted.
- Ask for less output. Output tokens usually cost more than input tokens and generation time grows with output length. Request a compact structured format instead of prose, set a sensible
max_tokens, and don't ask for explanations nobody reads. - Reuse a stable prefix. When the same system prompt, tools or reference documents lead every request, prompt caching reduces both cost and time to first token. Structuring prompts for cache hits is covered in 2.4; here, track cache read and write tokens from the
usageblock so you know it's working. - Batch what can wait. Overnight document processing, bulk classification and offline eval runs don't need real-time answers. The Message Batches API processes them asynchronously at lower cost; keep the real-time API for interactive paths.
- Route by difficulty. Send routine requests to a smaller, faster model and escalate hard ones (by classifier, by rule, or on a failed validation) to a more capable one. Prove the routing with eval data per segment.
- Cut round trips. In agentic flows, each extra model turn re-sends the conversation. Better tool design (fewer, more complete tools; results that contain what the next step needs) cuts turns and tokens together.
Latency levers
Separate perceived latency from total latency. Streaming doesn't make the full response faster, but it gets the first token to the user sooner, which is what a chat user feels. Total time falls with shorter outputs, a faster model where quality allows, caching long stable prefixes, and parallelism: run independent retrieval and tool calls concurrently instead of in sequence, and split independent subtasks across parallel calls. Measure by stage, because in many RAG and agent systems the slowest part isn't the model. Accuracy-latency configuration decisions, such as how much reasoning effort to allow for a given request type, are covered in 3.3.
Choosing on the frontier
Plot candidate configurations (model, context size, reasoning settings, routing thresholds) by quality score against cost per successful task, and latency where it matters. Configurations that are both worse and more expensive than another option drop out. Among what remains, choose the cheapest one that clears the quality bar and latency SLA for each segment. Present the remaining trade-off to stakeholders with numbers, not adjectives (domain P6).
What to log per request
Monitoring an LLM system means watching quality, not just uptime. A useful per-request record:
- Request and trace IDs, user or tenant segment, timestamp.
- Versions: model snapshot, prompt template version, index or knowledge-base version, config version. These make diagnosis (4.4) possible.
- Usage: input, output, cache-read and cache-write tokens, and computed cost.
- Latency by stage: retrieval, time to first token, total, each tool call.
- Outcome signals:
stop_reason, tool calls and their errors, retries, validation failures, refusals, escalations, user feedback. - For RAG, the IDs of retrieved chunks, so a bad answer can be traced to its context.
Prompts and outputs often contain personal or confidential data. Log them under the same governance as the source data: redaction where possible, access controls, defined retention, and data residency where it applies (domain P5). Correlating these records across services and agents with distributed tracing is covered in 3.4.
Dashboards, alerts and online evaluation
- Alert on rates and percentiles, by segment: p95 latency, cost per task, error and retry rates, the share of responses ending in
"max_tokens", refusal rate, cache hit rate, escalation rate. A drop in cache hits after a deploy, for example, usually means something started changing the prefix. - Online evaluation. Sample production traffic and run model-graded checks (groundedness, policy compliance, format) asynchronously, with a human-reviewed subsample. This yields a quality trend, not just an infrastructure one.
- Close the loop. Failures found in production become new cases in the offline dataset, so the next change is tested against them.
Key concept
Optimization is only real if the eval suite says quality held: measure cost and latency per successful task, pull the biggest lever first, and log versions, usage, stage latency and outcomes per request so drift is visible.
In questions, the wrong answers usually optimize one dimension blindly (smallest model, aggressive truncation) or monitor only infrastructure. The right answer pairs an optimization with an eval check, or adds the quality signal and versioning the monitoring was missing.
Practice scenario
Work it through, then open this
Start with measurement: add per-request usage, cost and version logging, and run the extraction eval set to get a quality baseline per contract type. Then pull the biggest levers that don’t touch quality. Move the nightly job to the Message Batches API, since it isn’t latency-sensitive. Have extraction return a compact structured schema instead of free text, which shrinks output tokens and simplifies validation. Test whether fewer, better-ranked chunks hold quality, and cache the stable instruction prefix. Only then evaluate a smaller model for extraction, per contract type, keeping the large model for segments where it measurably matters. Add online evaluation on a sample of extractions so legal ops can see quality holding, not just uptime.
Build exercise — Cost and quality dashboard for one workload
Advanced · 50 min
What you’ll learn
- Computing cost per successful task from usage data
- Testing optimizations against the eval suite
- Defining alerts that catch quality and cost drift
-
For one workload, log per-request usage (input, output, cache tokens), versions, stage latencies and
stop_reason, and compute cost per successful task using your eval’s pass/fail.- Why: Without a per-outcome cost and a quality baseline, every optimization claim is unverifiable.
- You should see: A baseline table: quality score, p95 latency and cost per successful task, by segment.
-
Apply two levers one at a time (for example, a structured output format and fewer retrieved chunks), rerun the eval after each, and plot the results against the baseline.
- Why: Single changes are attributable, and the plot shows which levers sit on the frontier.
- You should see: At least one change that cuts cost with no quality loss, and possibly one that cuts cost but fails the bar.
-
Define five alerts with thresholds (for example p95 latency, cost per task, max-token truncation rate, cache hit rate, online groundedness score) and the first diagnostic step for each.
- Why: An alert without a next step just creates noise; linking each to a diagnosis speeds the response.
- You should see: An alert table where every row names the metric, segment, threshold and first thing to check.
Exam traps
Cutting cost by switching to the smallest model without re-running the eval suite
Downsizing without evidence risks the quality bar. Test the smaller model on the suite, per segment, and consider routing instead.
Optimizing input tokens when output length is what drives latency and cost
Output tokens are typically priced higher and drive generation time. Check the usage split before choosing a lever.
Sending latency-tolerant bulk work through the real-time API
Batch-friendly work belongs on the Message Batches API, which is designed for asynchronous processing at lower cost.
Monitoring only infrastructure metrics (uptime, error rate) and assuming quality holds
A system can be up and fast while answers degrade. Add online evaluation and outcome signals.
Logging full prompts and outputs with personal data and no retention or access controls
Logs inherit the sensitivity of the data. Apply redaction, access control and retention as you would to the source.
Alerting on averages, so a drift in one segment or the tail never triggers
Averages absorb concentrated problems. Alert on percentiles and per-segment rates.
Sources
- Claude Developer Platform Docs
- Anthropic
- Anthropic, Claude Certified Architect – Professional Exam Guide v1.0 (July 2026)