OpenRouter chat model integration.
OpenRouter is a unified API that provides access to hundreds of models from multiple providers (OpenAI, Anthropic, Google, Meta, etc.).
Install langchain-openrouter and set environment variable
OPENROUTER_API_KEY.
pip install -U langchain-openrouter
export OPENROUTER_API_KEY="your-api-key"| Param | Type | Description |
|---|---|---|
model |
str |
Model name |
temperature |
float | None |
Sampling temperature. |
max_tokens |
int | None |
Max tokens to generate. |
| Param | Type | Description |
|---|---|---|
api_key |
str | None |
OpenRouter API key. |
base_url |
str | None |
Base URL for API requests. |
timeout |
int | None |
Timeout in milliseconds. |
app_url |
str | None |
App URL for attribution. |
app_title |
str | None |
App title for attribution. |
app_categories |
list[str] | None |
Marketplace attribution categories. |
session_id |
str | None |
Group related requests for observability. |
trace |
dict[str, Any] | None |
Trace metadata for broadcasts. |
max_retries |
int |
Max retries (default 2). Set to 0 to disable. |
from langchain_openrouter import ChatOpenRouter
model = ChatOpenRouter(
model="anthropic/claude-sonnet-4-5",
temperature=0,
# api_key="...",
# openrouter_provider={"order": ["Anthropic"]},
)See https://openrouter.ai/docs for platform documentation.
Underlying SDK client (openrouter.OpenRouter).
OpenRouter API key.
OpenRouter API base URL. Maps to SDK server_url.
Application URL for OpenRouter attribution.
Maps to HTTP-Referer header.
Defaults to LangChain docs URL. Set this to your app's URL to get attribution for API usage in the OpenRouter dashboard.
See https://openrouter.ai/docs/app-attribution for details.
Application title for OpenRouter attribution.
Maps to X-Title header.
Defaults to 'LangChain'. Set this to your app's name to get attribution
for API usage in the OpenRouter dashboard.
See https://openrouter.ai/docs/app-attribution for details.
Marketplace categories for OpenRouter attribution.
Maps to X-OpenRouter-Categories header. Pass a list of lowercase,
hyphen-separated category strings (max 30 characters each),
e.g. ['cli-agent', 'programming-app'].
Only recognized categories are accepted (unrecognized values are silently dropped by OpenRouter).
See https://openrouter.ai/docs/app-attribution for recognized categories.
Timeout for requests in milliseconds. Maps to SDK timeout_ms.
Maximum number of retries.
Each unit adds ~150 seconds to the backoff window via the SDK's
max_elapsed_time (e.g. max_retries=2 allows up to ~300 s).
Set to 0 to disable retries.
The name of the model, e.g. 'anthropic/claude-sonnet-4-5'.
Same as model_name.
Sampling temperature.
Maximum number of tokens to generate.
Maximum number of completion tokens to generate.
Nucleus sampling parameter.
Frequency penalty for generation.
Presence penalty for generation.
Random seed for reproducibility.
Default stop sequences.
Number of chat completions to generate for each prompt.
Whether to stream the results or not.
Whether to include usage metadata in streaming output.
If True, additional message chunks will be generated during the stream including
usage metadata.
Any extra model parameters for the OpenRouter API.
Reasoning settings to pass to OpenRouter.
Controls how many tokens the model allocates for internal chain-of-thought reasoning.
Accepts an openrouter.components.OpenResponsesReasoningConfig or an
equivalent dict.
Supported keys:
effort: Controls reasoning token budget.
Values: 'xhigh', 'high', 'medium', 'low', 'minimal', 'none'.
summary: Controls verbosity of the reasoning summary returned in the
response.
Values: 'auto', 'concise', 'detailed'.
Example: {"effort": "high", "summary": "auto"}
See https://openrouter.ai/docs/guides/best-practices/reasoning-tokens
Provider preferences to pass to OpenRouter.
Example: {"order": ["Anthropic", "OpenAI"]}
Route preference for OpenRouter, e.g. 'fallback'.
Plugins configuration for OpenRouter.
Identifier used by OpenRouter to group related requests together.
Useful any time multiple requests should share an observability
grouping (e.g. a conversation, an agent workflow, a batch job, or a CI
run). Equivalent to setting the x-session-id HTTP header on the
underlying request. OpenRouter rejects values longer than 128
characters.
Falls back to the OPENROUTER_SESSION_ID environment variable when
unset, so callers can group all requests from a process without
threading the value through application code. Empty strings are
treated as unset.
Example: "conv-2026-04-30-abc"
See https://openrouter.ai/docs/guides/features/broadcast/overview
Trace metadata for observability tools (e.g. Langfuse, LangSmith).
Forwarded by OpenRouter to configured broadcast destinations. Common
keys include trace_id, trace_name, span_name, generation_name,
and parent_span_id; see the OpenRouter broadcast docs for the
current full set. Unknown keys are forwarded as custom metadata.
No environment-variable fallback — set per-call or on the constructor.
Example: {"trace_id": "abc-123", "span_name": "summarize"}
See https://openrouter.ai/docs/guides/features/broadcast/overview
A map of constructor argument names to secret ids.
Build extra kwargs from additional params that were passed in.
Validate configuration and build the SDK client.
Return whether this model can be serialized by LangChain.
Bind tool-like objects to this chat model.
Model wrapper that returns outputs formatted to match the given schema.