Skip to content

Instantly share code, notes, and snippets.

@wangjohn
Created June 13, 2025 02:27
Show Gist options
  • Select an option

  • Save wangjohn/8b34ab00f2ad6077ba671969ca073e2f to your computer and use it in GitHub Desktop.

Select an option

Save wangjohn/8b34ab00f2ad6077ba671969ca073e2f to your computer and use it in GitHub Desktop.
LLM fallback article
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