Page 5 of 8~112 min topic

Cost optimization lab

Handle failures and retries

When router sends long research prompts to mini — quality collapses, retries inflate spend, the system must degrade on purpose without widening blast radius.

~14 min this pageFailure handling

1Learn the idea

Read

Classify and bound retries

Map failure classes for POST /v1/answer: retryable vs fatal vs needs-human. Retries need budgets, jitter, and idempotency rules aligned to simple FAQs route to mini model; monthly forecast alerts at 80% budget. The chapter’s signature failure — router sends long research prompts to mini — quality collapses, retries inflate spend — must take a deliberate branch, not a generic catch-all.

Read

Containment path

Implement the degrade/rollback/refuse behavior finance partner capping July LLM spend at $12k needs when COST-ROUTER-MISCLASS-14 repeats. Prefer scoped controls (one flag, one weight, one tenant, one secret version) over fleet-wide restarts. Preserve evidence; do not delete logs to “clean the demo.”

Read

Implementation artifact

if decision.class_ == "research" and decision.model == "mini":
    raise AssertionError("misrouted_research")

Read

Verify harm reduction

After containment, check usd_per_successful_answer and groundedness moves in the safe direction and watch for retry amplification. Write the stop condition that ends the incident response for this lab.

Read

Stage depth

Chaos note: inject only one fault class at a time and restore fixtures after. Watch for dual failures — dependency down and retry amplifier — which is how router sends long research prompts to mini — quality collapses, retries inflate spend becomes an outage. Customer communication templates (even if only for the drill) beat silence. If you queue deferred work, define poison-message handling. Budget documents should state the maximum extra spend allowed during retries. Close the loop by linking the containment action to a dashboard panel for usd_per_successful_answer and groundedness.

Read

Field notes for `cost-optimization-lab` / `failure-handling`

Draw a state diagram for degrade modes and put it in the repo as ASCII if needed. Cap concurrent retries across the process, not only per request. Ensure cancellation propagates to downstream HTTP clients. When failing closed, choose a user-visible message that does not leak internals. Practice the single command that flips the kill switch or weight to zero. After recovery, drain or inspect deferred work before declaring green. In this chapter the product is token+router cost controller for mixed FAQ vs complex research traffic, the human stakeholder is finance partner capping July LLM spend at $12k, and the incident id you design against is COST-ROUTER-MISCLASS-14. Re-state the oracle in your notes — replay day shows −32% $ with groundedness drop ≤ 0.01 vs all-frontier baseline — and keep the invariant visible: simple FAQs route to mini model; monthly forecast alerts at 80% budget. Track usd_per_successful_answer and groundedness as the scoreboard. Surface under change control: POST /v1/answer. If you only have forty minutes, finish the fixture for router sends long research prompts to mini — quality collapses, retries inflate spend before polishing UI. Promotion language stays ternary: promote, hold, or roll back based on evidence, not hope.

Go deeper

Before you start

Why this matters

Assume router sends long research prompts to mini — quality collapses, retries inflate spend is happening right now. Write the first safe action, the signal that confirms containment, and the action you will not take (infinite retry, broad restart, deleting evidence). Tie the plan to invariant: simple FAQs route to mini model; monthly forecast alerts at 80% budget.

In the wild

See how this idea shows up as a product and a company — then come back to the lesson. Skills transfer across vendors.

Check your understanding

Page assessment

Answer from memory. Completion is saved from this evidence, not from opening the next page.

1. Are retry budgets explicit?
2. Is containment scoped?
3. Do you preserve evidence for COST-ROUTER-MISCLASS-14?

All responses are required.