Created
June 13, 2025 02:27
-
-
Save wangjohn/8b34ab00f2ad6077ba671969ca073e2f to your computer and use it in GitHub Desktop.
LLM fallback article
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
| func (agent *LLMAgent) CreateCompletion(ctx context.Context, request *CompletionRequest) (*Response, error) { | |
| // Build list of primary model + fallbacks for same category | |
| modelsToTry := []ModelType{agent.primaryModel} | |
| for _, platform := range GlobalFallbackOrder { | |
| if platform != agent.primaryPlatform { | |
| fallbackModel := GetModelForCategory(agent.category, platform) | |
| modelsToTry = append(modelsToTry, fallbackModel) | |
| } | |
| } | |
| // Try each model in order until one succeeds | |
| var lastError error | |
| for i, model := range modelsToTry { | |
| response, err := agent.callSingleModel(ctx, request, model, agent.getModelTimeout()) | |
| if err == nil { | |
| return response, nil | |
| } | |
| lastError = err | |
| } | |
| return nil, fmt.Errorf("all models failed: %w", lastError) | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment