Skip to main content

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:
  1. OpenRouter Account: Sign up at OpenRouter
  2. API Key: Generate an API key from your account dashboard
  3. Model Selection: Choose from hundreds of available models from different providers
  4. 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.
OpenRouterLLMService.Settings
default:"None"
Runtime-configurable settings. See Settings below.
str
default:"https://openrouter.ai/api/v1"
Base URL for OpenRouter API endpoint.

Settings

Runtime-configurable settings passed via the settings 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.
For all other settings (model, temperature, max_completion_tokens, etc.), see OpenAI LLM Settings.

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. The provider 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/model format (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 developer messages to user messages by default. To use models that support the developer role, set llm.supports_developer_role = True after instantiation or subclass OpenRouterLLMService to 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.