Rag.Router (rag v0.3.4)
View SourceMulti-LLM provider router.
The Router manages multiple LLM providers and routes requests to the most appropriate provider based on the configured strategy.
Configuration
{:ok, router} = Router.new(
providers: [:gemini, :codex, :claude],
strategy: :specialist, # or :fallback, :round_robin, :auto
fallback_order: [:gemini, :codex, :claude]
)Strategies
:fallback- Try providers in order until success (default with 1-2 providers):round_robin- Distribute load across providers:specialist- Route by task type to best provider (default with 3 providers):auto- Auto-select strategy based on available providers
Usage
# Route a request
{:ok, provider, router} = Router.route(router, :text, "Hello", [])
# Execute with the provider
{:ok, result, router} = Router.execute(router, :text, "Hello", [])
# Report result manually
router = Router.report_result(router, :gemini, {:ok, "response"})Auto-Detection
# Auto-detect available providers based on loaded modules and credentials
{:ok, router} = Router.new(auto_detect: true)
Summary
Functions
Get list of currently available providers.
Execute a request with automatic provider selection and retry.
Get capabilities for a specific provider.
Create a new router with the given configuration.
Get the next provider after a failure.
Report the result of a provider call.
Route a request to select the best provider.
Types
Functions
Get list of currently available providers.
Execute a request with automatic provider selection and retry.
Routes the request, executes with the selected provider, and handles retries on failure.
Get capabilities for a specific provider.
Create a new router with the given configuration.
Options
:providers- List of provider atoms (required unless auto_detect):strategy- Strategy atom or :auto (default: auto-selected):auto_detect- Auto-detect available providers (default: false):fallback_order- Order for fallback attempts- Strategy-specific options passed through
Returns
{:ok, router}on success{:error, reason}on failure
Get the next provider after a failure.
Report the result of a provider call.
Updates the router's strategy state based on success or failure.
Route a request to select the best provider.
Parameters
router- The router structtype- Request type (:text or :embeddings)prompt- The prompt or list of texts for embeddingsopts- Additional options
Returns
{:ok, provider, updated_router}- Selected provider{:error, reason}- No provider available