To use OpenRouterModel, you need to either install pydantic-ai, or install pydantic-ai-slim with the openrouter optional group:
pip/uv-add "pydantic-ai-slim[openrouter]"To use OpenRouter, first create an API key at openrouter.ai/keys.
You can set the OPENROUTER_API_KEY environment variable and use [OpenRouterProvider][pydantic_ai.providers.openrouter.OpenRouterProvider] by name:
from pydantic_ai import Agent
agent = Agent('openrouter:anthropic/claude-sonnet-4-5')
...Or initialise the model and provider directly:
from pydantic_ai import Agent
from pydantic_ai.models.openrouter import OpenRouterModel
from pydantic_ai.providers.openrouter import OpenRouterProvider
model = OpenRouterModel(
'anthropic/claude-sonnet-4-5',
provider=OpenRouterProvider(api_key='your-openrouter-api-key'),
)
agent = Agent(model)
...OpenRouter has an app attribution feature to track your application in their public ranking and analytics.
You can pass in an app_url and app_title when initializing the provider to enable app attribution.
from pydantic_ai.providers.openrouter import OpenRouterProvider
provider=OpenRouterProvider(
api_key='your-openrouter-api-key',
app_url='https://your-app.com',
app_title='Your App',
),
...You can customize model behavior using [OpenRouterModelSettings][pydantic_ai.models.openrouter.OpenRouterModelSettings]:
from pydantic_ai import Agent
from pydantic_ai.models.openrouter import OpenRouterModel, OpenRouterModelSettings
settings = OpenRouterModelSettings(
openrouter_reasoning={
'effort': 'high',
},
openrouter_usage={
'include': True,
}
)
model = OpenRouterModel('openai/gpt-5.2')
agent = Agent(model, model_settings=settings)
...OpenRouter supports web search via its plugins. You can enable it using the [WebSearchTool][pydantic_ai.builtin_tools.WebSearchTool].
You can customize the web search behavior using the search_context_size parameter on [WebSearchTool][pydantic_ai.builtin_tools.WebSearchTool]:
from pydantic_ai import Agent
from pydantic_ai.builtin_tools import WebSearchTool
from pydantic_ai.models.openrouter import OpenRouterModel
tool = WebSearchTool(search_context_size='high')
model = OpenRouterModel('openai/gpt-4.1')
agent = Agent(
model,
builtin_tools=[tool]
)
result = agent.run_sync('What is the latest news in AI?')