Overview
OpenRouterLLMService provides access to OpenRouter’s language models through an OpenAI-compatible interface. It inherits from OpenAILLMService and supports streaming responses, function calling, and context management with access to multiple model providers through a single API.
OpenRouter LLM API Reference
Pipecat’s API methods for OpenRouter integration
Example Implementation
Complete example with function calling
OpenRouter Documentation
Official OpenRouter API documentation and features
OpenRouter Platform
Access multiple model providers and manage API keys
Installation
To use OpenRouter services, install the required dependencies:Prerequisites
OpenRouter Account Setup
Before using OpenRouter LLM services, you need:- OpenRouter Account: Sign up at OpenRouter
- API Key: Generate an API key from your account dashboard
- Model Selection: Choose from hundreds of available models from different providers
- Credits: Add credits to your account for model usage
Required Environment Variables
OPENROUTER_API_KEY: Your OpenRouter API key for authentication
Configuration
str
required
OpenRouter API key for authentication. Raises
ValueError when empty.str
default:"None"
deprecated
Deprecated since 0.0.105, removed in 2.0.0. Use
settings=OpenRouterLLMService.Settings(model=...) instead.str
default:"https://openrouter.ai/api/v1"
Base URL for OpenRouter API endpoint.
Settings
Runtime-configurable settings passed via thesettings constructor argument using OpenRouterLLMService.Settings(...). These can be updated mid-conversation with LLMUpdateSettingsFrame. See Service Settings for details.
This service extends OpenAILLMService.Settings with OpenRouter-specific parameters:
OpenRouterProviderPreferences | dict | None
default:"None"
Which upstream providers may serve the request. Controls OpenRouter’s provider
routing preferences — which providers may serve a request, in what order, and
under what latency, throughput, price, quantization, and data-retention
constraints. Accepts an
OpenRouterProviderPreferences instance or a plain
dict (coerced to OpenRouterProviderPreferences). When unset or None,
OpenRouter routes by its default order. See Provider
Routing below for the full field reference.Usage
Basic Setup
With a Different Provider Model
With Provider Routing Preferences
Provider Routing
OpenRouter can route requests to multiple upstream providers for each model. Theprovider parameter in Settings controls these routing preferences.
OpenRouterProviderPreferences Fields
For provider slugs and full routing semantics, see OpenRouter’s provider selection guide.
Notes
- OpenRouter model identifiers use the
provider/modelformat (e.g.,openai/gpt-4o,anthropic/claude-sonnet-4,google/gemini-2.5-pro). - The default model is
openai/gpt-4.1. - Developer message conversion: OpenRouter requests convert
developermessages tousermessages by default. To use models that support thedeveloperrole, setllm.supports_developer_role = Trueafter instantiation or subclassOpenRouterLLMServiceto override this behavior. - When using Gemini models through OpenRouter, the service automatically handles the constraint that only one system message is allowed by converting additional system messages to user messages.