Overview
TheLlmRegistry is the central system that manages all LLM providers in ADK. It uses regex pattern matching to automatically route model names to the correct provider implementation, enabling the seamless multi-provider experience.
Source: packages/adk/src/models/llm-registry.ts:23
How It Works
Automatic Registration
Providers are automatically registered when the models module is imported:Pattern Matching
Each provider class defines supported model patterns:Model Resolution
When you specify a model, the registry resolves it:Core API
Creating LLM Instances
- Via AgentBuilder
- Via Registry
- Direct Instantiation
Registry Methods
newLLM
Create a new LLM instance by model name:resolve
Get the LLM class for a model name:registerLLM
Register a custom provider:Named Model Instances
For shared or preconfigured model instances:Register Named Instance
Retrieve Named Instance
Check Named Instance
Get or Create
Get a named instance or create a new one:Custom Providers
Create Custom Provider
Register Custom Provider
Use Custom Provider
Pattern Precedence
Registration Order
Providers are matched in registration order:Overlapping Patterns
If patterns overlap, the first registered provider wins:Debugging
Log Registered Models
Test Model Resolution
Registry Management
Clear All
Clear Model Instances Only
Clear Class Registrations Only
Unregister Named Instance
Best Practices
Pattern Design
Pattern Design
- Use specific patterns first (e.g.,
"gpt-4o.*"before"gpt-4.*") - Include version suffixes in patterns (e.g.,
"model-.*-v[0-9]+") - Test patterns thoroughly with regex testers
- Document supported models in your provider
Custom Providers
Custom Providers
- Extend
BaseLlmfor full ADK integration - Implement both streaming and non-streaming
- Handle errors properly (use
RateLimitErrorfor rate limits) - Add usage metadata to responses
Named Instances
Named Instances
- Use for shared configurations (API keys, base URLs)
- Use for environment-specific models (dev, staging, prod)
- Use for A/B testing different models
- Clear instances in tests to avoid state leakage
Registration
Registration
- Register custom providers early (at app startup)
- Register specific patterns before broad patterns
- Use automatic registration (via registry.ts pattern)
- Test registration with
logRegisteredModels()
Advanced Patterns
Provider Factory
Create providers dynamically:Multi-Provider Fallback
Fallback to alternative providers:Environment-Based Provider
Next Steps
OpenAI Provider
Learn about GPT model configuration
Custom Providers
Build your own LLM provider
AgentBuilder
Use providers with AgentBuilder
Error Handling
Handle provider errors