Cost optimization lab
Define the lab goal and success criteria
Cost optimization lab is production work only when one frozen failure can be reproduced, one measurable gate can stop a release, and one operator can safely reverse it.
1Try it yourself
Playground
Cost optimization lab
Match each workload to batching, caching, or model routing — without killing quality.
Embed 10k docs nightly
Before you start
Why this matters
Read this incident aloud: duplicate shipping questions consume flagship-model tokens even though a cached small-model answer passes quality checks. In two minutes, write the earliest deterministic check that should fail, the telemetry signal you would inspect, and the action that must not happen automatically. Compare your answer with this chapter's boundary: cache keys exclude raw identity and sensitive text; regulated intents always use the approved model path.
2Learn the idea
Read
Explain the system and define success
Start by drawing the boundary around a cost profiler and model router for a high-volume FAQ assistant. The unit under control is not “the model”; it is one repeatable transaction from CostSample with intent, model, input_tokens, output_tokens, cache_hit, latency_ms, quality_pass, and price_version to OptimizationReport with cost_per_success, cache_rate, routed_share, quality_delta, and projected_savings. In this lab, duplicate shipping questions consume flagship-model tokens even though a cached small-model answer passes quality checks. That makes the learning target observable: we can replay the transaction, record the same fields, and decide whether a release is safer than its baseline. The primary decision metric is cost per successful answer. The release rule is reduce cost by 25% while quality drops no more than 1 percentage point. Those values are deliberately paired: a single average cannot hide a critical subgroup or a security escape.
Write the failure before writing implementation code. Our named reproduction is: cost per call falls after routing, but retries increase and cost per successful answer rises. A useful reproduction contains a frozen input, an expected decision, the release identifier, and enough deterministic dependencies to rerun locally. It must not depend on a live customer, changing clock, or unrestricted provider account. The fixture below is synthetic but shaped like production. Keep it in version control and add a case whenever an incident exposes a missing behavior.
Success has three levels. A request-level assertion explains one example. An aggregate metric catches drift over many examples. An operational rule says who acts and how quickly. Here, llm_cost_per_success_usd records the aggregate, while the request artifact carries a correlation ID. The owner should be able to move from an alert to the exact failing fixture without searching raw prompts. The security boundary is equally explicit: cache keys exclude raw identity and sensitive text; regulated intents always use the approved model path.
The deliverable is a one-page acceptance contract plus the first red test. Do not tune a model yet; uncertainty about the contract is the bug to remove on this page.
Read
Focused implementation artifact
{"intent":"shipping_faq","model":"flagship","input_tokens":2100,"output_tokens":180,"cache_hit":false,"latency_ms":2400,"quality_pass":true,"price_version":"2026-07"}
Read
Turn the incident into a lab contract
Use the fixture above as the first row of an acceptance table. Add columns for expected decision, prohibited side effect, severity, policy version, and evidence to retain. The failure statement—cost per call falls after routing, but retries increase and cost per successful answer rises—must be falsifiable: a teammate should know exactly which assertion turns red. Write the baseline result before trying a candidate so improvement is comparative rather than anecdotal.
Now calculate cost per successful answer by hand for a three-case toy set: one pass, one ordinary failure, and one critical failure. Apply the published gate, reduce cost by 25% while quality drops no more than 1 percentage point, without rounding. This exposes whether weighting, critical-case overrides, and empty slices behave as intended. Emit llm_cost_per_success_usd once for the toy run and verify that its labels contain a bounded release and outcome, never fixture text.
The lab is ready only when its fixture is synthetic, its dangerous actions are replaced by recording fakes, and its evidence can be retained safely. The expected incident response is to promote routing by intent, retain a holdout, and automatically disable it if quality or retries breach budget. Assign that decision to a role, not “the team,” and state which artifact proves recovery.
Continue learning · glossary & guides
-
Is the failure falsifiable from one frozen fixture?
-
Does the gate combine a population metric with critical-case handling?
-
Are owner, evidence, and forbidden action explicit?
-
Local references: Glossary: token budget · Glossary: semantic cache · How-to: profile token spend