Skip to content

Instantly share code, notes, and snippets.

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

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

Select an option

Save wangjohn/fd5cdb777ad1ca48db45a0eeb53bf3d5 to your computer and use it in GitHub Desktop.
LLM fallback article
func (agent *LLMAgent) StreamCompletion(ctx context.Context, request *CompletionRequest, tokens chan<- Token) error {
modelsToTry := agent.getModelsToTry()
for i, model := range modelsToTry {
hasReceivedFirstToken, err := agent.tryStreamSingleModel(ctx, request, model, tokens, agent.getModelTimeout())
if err == nil {
return nil // Success
}
// Only retry if we haven't started streaming and have more models to try
if !hasReceivedFirstToken && i < len(modelsToTry)-1 {
continue // Try next model
}
return err
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment