feat: smart model routing with priority selection and provider fallback#96
Open
hippoley wants to merge 1 commit intoYuan-lab-LLM:mainfrom
Open
feat: smart model routing with priority selection and provider fallback#96hippoley wants to merge 1 commit intoYuan-lab-LLM:mainfrom
hippoley wants to merge 1 commit intoYuan-lab-LLM:mainfrom
Conversation
Addresses Issue Yuan-lab-LLM#68 (problems 1, 2, 3): 1. ListAvailableModels now returns Auto + all active models so users can see and select specific models instead of only 'Auto'. 2. selectAutoModel uses a priority field (INT, higher = preferred) with random tie-breaking among the highest-priority group for simple load balancing. Falls back to secure models when no non-secure candidates remain. 3. Provider fallback on failure: - Non-streaming: retries with alternate models on connection error or 5xx response (max 2 retries via callWithFallback). - Streaming: retries only on connection-level failure before any response headers are written (max 2 retries via streamWithFallback). - Each fallback attempt records an audit event for observability. Schema: adds 'priority' column to llm_models (INT NOT NULL DEFAULT 0) with idempotent ALTER TABLE guarded by duplicate-column-name check. New errProviderConnection sentinel type distinguishes retriable connection failures from committed-response errors in the streaming path. Tests: 9 new tests covering model listing, priority selection, load distribution, exclusion logic, and error type detection.
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
Addresses Issue #68 (problems 1, 2, and 3) — the AI Gateway's model routing was effectively a stub: users could only see "Auto", auto-selection picked the first model blindly, and provider failures returned errors with no retry.
This PR delivers a complete Smart Model Routing layer:
Problem 1: Users can now see and select specific models
ListAvailableModels()previously hardcoded a single "Auto" entry. It now returns Auto + all active models with their real provider types, so the frontend can offer a model picker.Problem 2: Priority-based auto selection with load balancing
priority INT NOT NULL DEFAULT 0column tollm_models(idempotentALTER TABLEwith duplicate-column guard).selectAutoModel()now filters non-secure models, sorts by priority (descending, from DB ordering), and randomly picks among the highest-priority group for simple load balancing.ListActive()andList()ordering updated to-priority, -is_secure, display_name.Problem 3: Provider fallback on failure
Non-streaming path:
dispatchCall()extracts provider routing fromChatCompletions().callWithFallback()retries with alternate models on connection error or 5xx response, up to 2 retries.gateway.request.fallback) for observability.Streaming path:
dispatchStream()extracts provider routing fromStreamChatCompletions().streamWithFallback()retries only on connection-level failure (before any response headers are written to the client), up to 2 retries.errProviderConnectionsentinel type distinguishes retriable connection failures from committed-response errors.streamOpenAICompatible()andstreamAnthropic()now returnerrProviderConnectiononhttpClient.Do()failure.Design decisions
Files changed
models/llm_model.goPriority intfieldrepository/llm_model_repository.goaigateway/service.goaigateway/service_test.goTests
9 new tests covering:
errProviderConnectionviaerrors.As)All existing tests continue to pass (zero regression).
Closes #68 (problems 1, 2, 3). Problems 4-8 (instance-level routing, cost-aware routing, request-feature routing) are deferred to future PRs.