Review the prerequisites
Suitable for: Backend developers learning Agent applications.
- Capability and status contracts for fallback models
- Switching the model changes the decision maker and cannot reset executed facts, authorizations and budgets. Only when the backup model meets the required capabilities and message semantics of the task can it be an executable downgrade path.
How does the mechanism work?
Step-by-step explanation
- Should the model be automatically changed after the main model returns a rejection?
Reasons for switching models must distinguish temporary failures from prohibited behavior.
- How to handle the tool call ID when switching?
Changing the message format must preserve correlations with actual actions.
- What should I do if the backup model is also limited?
When fallback also fails, a global stopping rule is needed.
Debugging · Diagnose failures
Diagnose failures · Capability and status contracts for fallback models
Objectives of this level: Capability contracts and executed events can be maintained to avoid repeated side effects.
Visual guide · Fallback preserves business facts
This routing fixture performs no writes. Consider fallback only after preserving business receipts, identity, required capabilities and shared limits. Select the figure to open it at full size.
Public repair exercise: identity reset and replay after a 429
Save this faulty implementation as fallback_bad.py. It disregards write state and failure type, generates a retry business ID, discards tool facts, and submits another write. The cause is disclosed; label this guided_repair. A reviewer must separately select complete new inputs/contracts for unseen diagnosis.
def route(runtime, request, state, event, capabilities):
retry_id = request["business_id"] + "-retry"
runtime.submit_write(retry_id)
response = runtime.call_fallback(retry_id, [])
return {"business_id": retry_id, "status": "draft_ready", "receipt": None,
"facts": [], "reason": "fallback_draft", "draft": response.get("text")}
Execute the fixed incident first. A ledger containing biz-77/receipt-1 gains biz-77-retry/receipt-2; one_effect, same_receipt, and no_new_model_or_write fail, with exit 1. Grade the actual faulty module and save before; it also exits 1. cp creates your repair copy in fallback_debug.py.
python3 fallback_lab.py incident --out fallback-incident.json
python3 fallback_lab.py grade --module fallback_bad.py --out fallback-before.json
cp fallback_bad.py fallback_debug.py
Save unchanged assertions as fallback_probe.py. They cover committed state and unknown state both with/without a matching receipt. Repair the module while preserving the probes, grader, inputs, and original business identity.
from copy import deepcopy
from fallback_lab import REQUEST, STATE, EVENT, RECEIPT, RoutingRuntime
from fallback_debug import route
req, state, event = deepcopy(REQUEST), deepcopy(STATE), deepcopy(EVENT)
state.update(write_state="committed", receipt=deepcopy(RECEIPT))
rt = RoutingRuntime(ledger=[RECEIPT])
result = route(rt, req, state, event, ["tools", "json"])
assert result["status"] == "committed" and result["receipt"] == RECEIPT
assert result["business_id"] == "biz-77" and result["facts"] == STATE["facts"]
assert rt.calls == [] and rt.ledger == [RECEIPT] and rt.now == 0
state.update(write_state="unknown", receipt=None)
rt = RoutingRuntime(ledger=[RECEIPT])
result = route(rt, req, state, event, ["tools", "json"])
assert result["status"] == "committed" and result["receipt"] == RECEIPT
assert rt.calls == [{"action": "lookup", "business_id": "biz-77"}]
assert rt.ledger == [RECEIPT]
rt = RoutingRuntime()
result = route(rt, req, state, event, ["tools", "json"])
assert result["status"] == "reconcile_required"
assert rt.calls == [{"action": "lookup", "business_id": "biz-77"}]
assert rt.ledger == []
print("committed_and_unknown_write_contract_passed")
Rerun: expect committed_and_unknown_write_contract_passed and exit 0 from the probe, then 17/17 and exit 0 from the grader. Preserve actual calls/ledger and module_sha256 across before/after. Author calibration establishes behavior on these fixtures; it cannot prove a learner independently authored a solution.
python3 fallback_probe.py
python3 fallback_lab.py grade --module fallback_debug.py --out fallback-after.json
Optional explanation: the fixed committed case
After recording your public repair, run explain. With fixed write_state=committed, return existing_receipt first, retaining facts and biz-77. Calls remain empty and the ledger contains only receipt-1. A general implementation must also handle unknown-result lookup, permanent parameters, permission/refusal, capabilities, shared budgets, and an unavailable backup. This one branch cannot be used for every input.
python3 fallback_lab.py explain --out fallback-explained.json
Bounded backoff and idempotence; Function calling associations; lab source.
Worked trace: unknown is a reconciliation state
Visual guide · An unknown write keeps its original identity
Actual local authored fixture calls. Lookup uses business_id=biz-77; operation_id=read-op-1 remains evidence. A missing receipt does not authorize rewrite, and changing models cannot undo a committed effect. Select the figure to open it at full size.
A 429 changes model availability; it does not reveal whether a previous business write committed. In this executed narrow trace, retain request.business_id=state.business_id='biz-77', facts[0].operation_id='read-op-1', write_state='unknown', and receipt=None. Before considering backoff, capability or call budgets, the only allowed runtime action is lookup('biz-77'). This same-ID lookup is about the original action, not a new request with a retry suffix.
With fixture ledger [{business_id: biz-77, receipt_id: receipt-1, status: committed}], lookup returns that receipt. Result is status=committed, reason=reconciled_receipt, original facts and receipt, draft None; calls are exactly [{action: lookup, business_id: biz-77}], now0 and ledger still one row. With empty fixture ledger, lookup returns None. Result is reconcile_required/receipt_not_found_is_not_proof_of_no_write, receipt None and draft None; calls still contain only lookup and the ledger stays empty. That empty fixture establishes this one local response, not absence of an effect in an external service. The business needs an authoritative reconciliation/idempotence contract before any resubmission can be considered.
Concrete counterexample executed by the existing guided incident: start already committed with biz-77/receipt-1, then submit_write('biz-77-retry') and call_fallback('biz-77-retry', []). Audit now has two ledger rows, including receipt-2; one_effect, same_receipt, and no_new_model_or_write fail. Dropping facts also drops the association to read-op-1. A new model cannot cancel receipt-1 or its effect. The paired fixed committed trace instead returns committed/existing_receipt with calls [], the original receipt/facts, and one ledger row. It proves only that known-commit branch; unknown must still lookup and stop as above.
Captured original failure and fixed branch, executable narrow source. Fields existing_guided_broken and existing_guided_repaired retain both audits; observed retains both unknown outcomes. This is author calibration over non-idempotent local fixture writes, with zero provider calls. It is not an independently authored learner route, authentication test, real payment write, or proof of cross-service atomicity.
Find the first fallback condition that was skipped
Use the fallback exercise. Check receipt/write state first, then failure classification, required capabilities and shared allowance/deadline. Compare backup input with the persisted original facts; a backup that gets a clean empty state has already crossed the wrong boundary. A draft becoming available still does not mean a return exists.
The connected lost-response case distinguishes a retryable decision failure from uncertain business completion. Its stop/verify/resume route must preserve one operation key and one effect. Evidence and exact commands are guided contract checks, not measurements of current provider rate limits, latency or model accuracy.
Continue the connected project article and inspect the executable entrypoint.
code example
Replay failure and repair
The fixed incident wrongly adds receipt-2 to receipt-1 and exits 1. The fixed repaired branch retains the original receipt; a general repair still needs unknown-state and budget coverage across 17 cases.
Acceptance task for this level
Public repair exercise: keep the replay failure, repair fallback_debug, and rerun unchanged three-write-state probes/grader; submit a diff and before/after SHA.
Reading this browser’s record…
Hide the answer and check your understanding
Why does no receipt for unknown state not authorize a safe replay?
Expand reference derivation
Visibility or in-flight commit can explain absence. This exercise provides no external proof of nonexecution, so return reconcile_required and stop new writes.
Further explanations and practice
When encountering unfamiliar principles, first read the implementation, continuous questioning and migration cases, and then independently explain the premise and boundaries. Answers and notes are saved to the original account record.
All linked explanations and exercises (1 )
Sources and verification scope
The principles are based on public information; the numbers, cases and tasks are the teaching design of this website. Offline experiments verify the range noted on this page, and the learning effect still needs to be judged through independent tasks and feedback.
- AWS: Bounded backoff and idempotent retries
- MCP 2025-11-25: Tools and error types
- OpenAI: Function calling messages are associated with results
- OpenAI: Structural guarantees and boundaries for Structured outputs
- Harness Journey: one connected practical project
- Bilingual project guide and fault exercises
- Harness Journey source ZIP