Repository navigation
feat: add fallbackModels config for cross-provider LLM failover - #22
Open
omerhochman wants to merge 1 commit into
Open
omerhochman wants to merge 1 commit into
omerhochman wants to merge 1 commit into
Conversation
Adds a `fallbackModels: []` array to the config schema so users can declare ordered backup LLMs (e.g. OpenRouter as a fallback when Groq hits a 429). When the primary model errors out and its `llmRetryAttempts` are exhausted, the agent fails over to the next fallback - each with its own retry budget - via LangChain's native `.withFallbacks()` on the primary Runnable. Each fallback entry supports `model`, `modelProvider`, `baseUrl`, and `apiKey`, so a single fallback can target a different provider account or an OpenAI-compatible gateway (OpenRouter, etc.) without leaking into the primary's env vars. For openai/azure_openai providers, `baseUrl` maps to the SDK's `configuration.baseURL`; other providers receive `baseUrl` directly. Previously, users had to either patch node_modules or run a LiteLLM sidecar to get cross-provider rate-limit failover. This exposes the LangChain primitive that was already available under the hood.
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
Exposes LangChain's native
.withFallbacks()primitive through a newfallbackModels: []config field so users can declare ordered backup LLMs (e.g. OpenRouter as a fallback when Groq hits a 429) without patchingnode_modulesor running a LiteLLM sidecar.How it works
llmRetryAttempts, default3) kicks in.fallbackModelsis tried, with its own retry budget.Each fallback entry supports
model,modelProvider,baseUrl, andapiKey, so a single fallback can target a different provider account or an OpenAI-compatible gateway (OpenRouter, etc.) without leaking into the primary's env vars. Foropenai/azure_openaiproviders,baseUrlmaps to the SDK'sconfiguration.baseURL; other providers receivebaseUrldirectly.Files changed
src/config/schema.ts— newFallbackModelSchema+fallbackModels: []array onConfigSchema.src/config/types.ts— exports the newFallbackModeltype.src/agent/model-factory.ts— refactorscreateChatModelto build the primary + each fallback through a sharedbuildSingleModelhelper, then wraps withprimary.withFallbacks({ fallbacks }). AddsbuildInitOptions(exported, tested) that normalizesbaseUrl/apiKeyper provider.docs/configuration.md+README.md— new “Fallback Models” section with semantics, fields table, and OpenRouter example.tests/unit/config/schema.test.ts— 7 new tests covering defaults, single/multi entries, OpenRouter-style entry, and validation errors.tests/unit/agent/model-factory.test.ts— 14 new tests covering the empty-array no-op, retry composition,baseUrlmapping for openai vs other providers, andapiKeypass-through. NewbuildInitOptionssuite with 6 unit tests.Configliterals updated to includefallbackModels: []so they continue to satisfy the type.Test plan
bun run typecheckcleanbun run test— 17 files / 223 tests passingTradeoff worth flagging
.withFallbacks()falls through on any error from the primary, including401 Unauthorized. This keeps the suite running through a misconfigured primary, but means a stray credential mistake will silently route traffic (and cost) to the fallback. The first-run logs will surface it, but we may want to add afallbackOn: ['transient', 'rate-limit']filter in a follow-up.🤖 Generated with Claude Code
Generated by Claude Code