feat: add runtime model fallback on retry exhaustion#11739
Closed
vaab wants to merge 1 commit intoanomalyco:devfrom
Closed
feat: add runtime model fallback on retry exhaustion#11739vaab wants to merge 1 commit intoanomalyco:devfrom
vaab wants to merge 1 commit intoanomalyco:devfrom
Conversation
When an LLM model is overloaded or rate-limited, OpenCode retries the
same model indefinitely with exponential backoff. This adds a
``fallback_models`` config field on agents so that after
``max_retries_before_fallback`` (default 3) consecutive retryable
failures, the processor automatically switches to the next model in
the fallback chain.
Changes across 4 files:
- ``config/config.ts``: accept ``fallback_models`` and
``max_retries_before_fallback`` in ``Config.Agent`` schema
- ``agent/agent.ts``: parse ``"provider/model"`` strings into
``{ providerID, modelID }`` on ``Agent.Info``
- ``session/status.ts``: new ``"fallback"`` status type with
``from``/``to`` fields
- ``session/processor.ts``: fallback logic in the retry catch block
with per-model backoff reset, assistant message metadata update,
and greedy resolution loop across remaining fallbacks
Config example:
{ "agent": { "build": {
"model": "moonshotai/kimi-k2",
"fallback_models": ["anthropic/claude-sonnet-4-5", "openai/gpt-4o"],
"max_retries_before_fallback": 3
}}}
Contributor
|
Thanks for your contribution! This PR doesn't have a linked issue. All PRs must reference an existing issue. Please:
See CONTRIBUTING.md for details. |
Contributor
|
The following comment was made by an LLM, it may be inaccurate: Based on my search, I found one potentially related PR: Related PR Found:
Conclusion: No duplicate PRs found for the current feature (PR #11739). The only tangentially related PR (#8669) appears to be a previous fix related to model fallback tracking, but it's not a duplicate of the new fallback-on-retry-exhaustion feature being introduced in PR #11739. |
6 tasks
dzianisv
pushed a commit
to dzianisv/opencode
that referenced
this pull request
Apr 1, 2026
Allow agent markdown frontmatter and config files to specify model as a YAML array: model: [primary, fallback1, fallback2...] The first element is the primary model; subsequent elements become fallback_models. This is a convenience shorthand on top of the separately cherry-picked fallback_models/max_retries_before_fallback fields from upstream PR anomalyco#11739. Also fixes a TS branded-type cast in processor.ts for Provider.getModel.
dzianisv
pushed a commit
to dzianisv/opencode
that referenced
this pull request
Apr 2, 2026
Allow agent markdown frontmatter and config files to specify model as a YAML array: model: [primary, fallback1, fallback2...] The first element is the primary model; subsequent elements become fallback_models. This is a convenience shorthand on top of the separately cherry-picked fallback_models/max_retries_before_fallback fields from upstream PR anomalyco#11739. Also fixes a TS branded-type cast in processor.ts for Provider.getModel.
Contributor
|
Closing this pull request because it has had no updates for more than 60 days. If you plan to continue working on it, feel free to reopen or open a new PR. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
fallback_modelsandmax_retries_before_fallbackconfig fields to agent configuration"fallback"session status type surfaces the model transition in the UIMotivation
When an LLM model is overloaded or rate-limited, OpenCode retries the same model indefinitely with exponential backoff (capped at 30s). Users with multiple provider accounts (e.g. Anthropic, OpenAI, GitHub Copilot) have no way to automatically fail over — they're stuck waiting on a down model.
Config example
{ "agent": { "build": { "model": "moonshotai/kimi-k2", "fallback_models": [ "anthropic/claude-sonnet-4-5", "openai/gpt-4o" ], "max_retries_before_fallback": 3 } } }Changes
config/config.tsfallback_models(string array) andmax_retries_before_fallback(positive int) inConfig.Agentschema; added toknownKeysso they don't leak tooptionsvia.catchall()agent/agent.ts"provider/model"strings into{ providerID, modelID }objects onAgent.Info; propagate from configsession/status.ts"fallback"discriminated union member withfrom/tostring fieldssession/processor.tsProcessor behavior
Design decisions
attemptsOnCurrentModel, not totalattempt, so a fresh fallback model starts at 2s instead of inheriting the escalated delaymodelID/providerIDon the persisted message reflect the actual model that generated the responsecreate()closure, reset naturally whenprocess()returns and a new processor is created for the next turnBackward compatibility
Fully backward-compatible. Both new config fields are optional with sensible defaults. Agents without
fallback_modelsconfigured behave identically to before.