Quick Start
With Fallback Chain
Add fallback models for reliability — if the primary fails, the agent automatically tries the next:
How It Works
| Phase | What happens |
|---|---|
| 1. Primary | Agent calls the configured model first |
| 2. Fallback | On failure, tries each model in fallback_models in order |
| 3. Response | First successful response is returned |
Configuration Options
LLMConfig SDK Reference
Full parameter reference for LLMConfig
| Option | Type | Default | Description |
|---|---|---|---|
model | str | (required) | Primary model to use |
fallback_models | list[str] | None | None | Ordered fallback models |
base_url | str | None | None | API endpoint override |
api_key | str | None | None | API key (defaults to env vars) |
auth | dict | None | None | Additional auth headers |
Common Patterns
Pattern 1 — Self-hosted model via custom endpoint
Pattern 2 — Multi-provider fallback for resilience
Pattern 3 — Multi-provider fallback for business continuity
Best Practices
Always add at least one fallback model
Always add at least one fallback model
Even one fallback prevents your agent from failing completely during rate limits or outages. Use a smaller, cheaper model as the fallback — it costs less and is often available when the primary isn’t.
Use environment variables for API keys
Use environment variables for API keys
Never hardcode
api_key in your code. Leave it as None and set OPENAI_API_KEY, ANTHROPIC_API_KEY, etc. in your environment instead. LLMConfig picks them up automatically via LiteLLM.Use LiteLLM prefixes for multi-provider setups
Use LiteLLM prefixes for multi-provider setups
Prefix models with the provider name (
anthropic/, openai/, ollama/) to avoid ambiguity when using multiple providers in a fallback chain.Test your fallback chain
Test your fallback chain
Deliberately trigger a failure (e.g., invalid primary model name) and verify your fallback fires correctly before going to production.
Related
LLM Gateways
Route requests through a unified LLM gateway
LLM Endpoint Config
Fine-tune endpoint parameters per model
LLM Error Classification
How errors trigger fallback in the chain
Failover
Automatic failover strategies for reliability

