feat(llm): multi-LLM failover & round-robin via indexed config#2365
Merged
Conversation
Configure extra LLMs by index (HINDSIGHT_API_LLM_<n>_*) alongside the
unindexed primary, then route across them with HINDSIGHT_API_LLM_STRATEGY
(JSON): {"mode":"failover"} or {"mode":"round-robin"} with optional
per-member "weights" for unbalanced rotation. Each operation can override
the global chain with a RETAIN_/REFLECT_/CONSOLIDATION_ prefix.
A general, provider-agnostic alternative to the LiteLLM Router and a more
extensible replacement for the single-secondary failover approach.
- config.py: LLMMemberConfig/LLMStrategyConfig dataclasses, indexed-member
+ strategy parsing, new HindsightConfig fields (credential, server-level).
- engine/multi_llm.py: MultiLLMProvider mirrors the LLMProvider surface so it
drops into with_config()/ConfiguredLLMProvider and _provider_impl passthrough;
smooth weighted round-robin; failover passes through OutputTooLongError and
cancellation; strict-primary/soft-secondary verify_connection.
- memory_engine.py: _build_llm wraps each of the 4 LLM slots; no-config path
returns the plain LLMProvider unchanged.
Batch retain runs on the primary member only (documented).
6 tasks
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
Adds a general, extensible multi-LLM layer: configure N LLMs by index and pick a routing strategy. This is a provider-agnostic alternative to the LiteLLM Router and a more flexible replacement for a single hard-coded failover secondary (supersedes the direction of #2332).
The unindexed
HINDSIGHT_API_LLM_*config stays the primary (member 1); extra members are numbered from 1, andHINDSIGHT_API_LLM_STRATEGY(JSON) selects how to route.Strategies
failover— try members in declared order; advance on a member's failure (after its own retries).round-robin— rotate the starting member per request to spread load, then fall through the rest on failure. Optional positive-intweights(one per member, primary first) for an unbalanced rotation (smooth weighted RR).Per-operation chains — each operation can override the global chain with a
RETAIN_/REFLECT_/CONSOLIDATION_prefix (e.g.HINDSIGHT_API_RETAIN_LLM_1_PROVIDER,HINDSIGHT_API_RETAIN_LLM_STRATEGY). A per-op slot with no indexed members (or no strategy) inherits the global chain.Design
MultiLLMProvidermirrors theLLMProviderpublic surface, so it drops into every existing call path —with_config()/ConfiguredLLMProviderand_provider_implattribute passthrough are untouched.OutputTooLongErroris propagated (a different provider won't fit it either);CancelledError/KeyboardInterrupt/SystemExitpropagate unchanged.verify_connection: strict on the primary, soft (warn-only) on the rest, so a down failover member doesn't block startup.LLMProviderunchanged (byte-identical hot path).Files
config.py—LLMMemberConfig/LLMStrategyConfigdataclasses,_parse_llm_members/_parse_llm_strategy, 8 newHindsightConfigfields (credential, server-level static),from_env()wiring.engine/multi_llm.py(new) —MultiLLMProvider,_should_failover, smooth weighted RR scheduler.engine/memory_engine.py—_build_llmresolves + wraps each of the 4 LLM slots.config_resolver.py— restore typed member objects after theasdictround-trip.configuration.md),.env.example(+ embed sync), tests.Notes / non-goals (v1)
call/call_with_toolspaths.max_attempts).Tests
tests/test_multi_llm_provider.py— failover ordering, weighted round-robin distribution, error classification, all-fail re-raise, attribute passthrough, strict/soft verify.tests/test_multi_llm_config.py— indexed-member parsing (contiguity, per-op prefixes, missing-key), strategy JSON validation, per-op inherit/override, plain-vs-wrapped build.ruff+ty+lint.shclean; embed env-template sync test passes.