Add `sticky` to `ModelFallback` to keep a conversation on the fallback model that answered
- Related: #3023 (circuit breaker, across runs; not closed by this)
- Stacked on #11807 (
ModelResponse.model_id), which this PR targets. Once #11807 merges, this PR is retargeted tomain.
What
ModelFallback(..., sticky=True) keeps a conversation on the fallback model that last answered, instead of starting every request step from the primary again. It's off by default, and with it off the behavior is unchanged.
Why
Without it, every step of a conversation whose primary is down or rate-limited:
- waits on the primary to fail first;
- spends retries on it;
- switches providers back and forth, which loses the prompt-cache prefix each time.
Stickiness is per conversation, not per run, because the cache prefix belongs to the conversation. It survives agent.run(message_history=...), so it also works across processes and in durable execution. Temporal and DBOS tests cover it.
How
#11807 has the agent record each response's model as ModelResponse.model_id, and the attempts that failed before it in failed_attempts. At a step's first attempt, if the latest answers all came from one of models other than the step's own, and one of them was reached by falling back (its failed_attempts aren't empty), the request moves to that candidate before anything is sent (RetryModelRequest). Later sticky answers have no failed attempts of their own; they continue the streak the fallback started. Nothing is written to message metadata, and the capability holds no state across steps or runs. ModelFallback.from_spec accepts sticky too.
Decisions for review
- When the sticky model fails, the chain wraps around. It tries the models after the sticky one, then the step's own model, then the models before it, each at most once. Otherwise
Agent(A, capabilities=[ModelFallback(B, sticky=True)])would fail when B fails without ever retrying A. Suspended continuations keep #9348's rule of not starting over, because of batching. - No expiry. The conversation goes back to the primary only when the primary answers again, for example on that wrap-around, since its answer records the primary's
model_id. A time-based return policy could be added later. - Only the agent's default model is replaced.
run(model=...),override(model=...)andSelectModelstill go first. Limitation: an explicit choice that picks the same model as the default counts as the default. Fixing that would mean tracking in core where the step's model came from. - Only a fallback makes a conversation sticky (decided by Douwe). An answer from a candidate picked with an explicit
run(model=B), or any answer with no failed attempts before it, doesn't change what later runs start from. A fallback reached from an explicitrun(model=X)does stick, as does a candidate another capability retried after the fallback. This is read from the answering response'sfailed_attempts, so nothing new is stamped. - A
model_idnaming a model that's no longer a candidate is ignored, with no error, and the step starts normally. - Not a
get_model()selector. That design would get this precedence for free, but resuming a suspended response with a dynamic model raises an error (_check_dynamic_model_resume).
Out of scope: a circuit breaker across runs (#3023), meaning shared cooldowns taken from retry_after.
Tests
tests/test_capability_model_fallback.py covers:
- stickiness across steps in one run, and across two runs;
- without
sticky, the chain restarts from the step's model; - a conversation the step's model answered stays on it;
- an explicit
run(model=B)answer with no failed attempts doesn't stick; - after a sticky period, a failing candidate wraps around to the primary, whose answer takes the conversation back;
- a later answer from another model ends the streak;
- the variants of the chain after the sticky model fails;
run(model=)andSelectModelwinning;- ignored
model_ids; - a candidate another capability retried;
- interaction with resumed suspended responses.
The Temporal and DBOS tests cover the durable wrapper: across steps and runs, and wrapping around to the wrapped primary. There's also a docs example.
Checklist
- No breaking changes in accordance with the version policy.
- Any permitted compatibility impact has a label, warning, migration, release note, and exact API-check waiver. (none)
- PR title is fit for the release changelog.
š¤ Generated with Claude Code