This will help you get started with OpenRouter chat models. OpenRouter is a unified API that provides access to models from multiple providers (OpenAI, Anthropic, Google, Meta, and more) through a single endpoint. For a full list of available models, visit the OpenRouter models page.Documentation Index
Fetch the complete documentation index at: https://langchain-5e9cc07a-preview-mdrxyo-1777658790-7be347c.mintlify.app/llms.txt
Use this file to discover all available pages before exploring further.
Overview
Integration details
| Class | Package | Serializable | JS/TS Support | Downloads | Latest Version |
|---|---|---|---|---|---|
ChatOpenRouter | langchain-openrouter | beta | ❌ |
Model features
| Tool calling | Structured output | Image input | Audio input | Video input | Token-level streaming | Native async | Token usage | Logprobs |
|---|---|---|---|---|---|---|---|---|
| ✅ | ✅ | ✅ | ✅ | ✅ | ✅ | ✅ | ✅ | ✅ |
Setup
To access models via OpenRouter you’ll need to create an OpenRouter account, get an API key, and install thelangchain-openrouter integration package.
Installation
The LangChain OpenRouter integration lives in thelangchain-openrouter package:
Credentials
Head to the OpenRouter keys page to sign up and generate an API key. Once you’ve done this set theOPENROUTER_API_KEY environment variable:
Instantiation
Now we can instantiate our model object and generate chat completions:Invocation
Streaming
Tool calling
OpenRouter uses the OpenAI-compatible tool calling format. You can describe tools and their arguments, and have the model return a JSON object with a tool to invoke and the inputs to that tool.Bind tools
WithChatOpenRouter.bind_tools, you can pass in Pydantic classes, dict schemas, LangChain tools, or functions as tools to the model. Under the hood these are converted to OpenAI tool schemas and passed in every model invocation.
Tool calls
The AIMessage has atool_calls attribute. This contains tool calls in a standardized format that is model-provider agnostic.
Strict mode
Passstrict=True to guarantee that model output exactly matches the JSON Schema provided in the tool definition:
Structured output
ChatOpenRouter supports structured output via the with_structured_output method. Two methods are available: function_calling (default) and json_schema.
Individual model calls
Individual model calls
Use
with_structured_output to generate a structured model response. Specify method="json_schema" to use JSON Schema-based structured output; otherwise the method defaults to function calling.Agent response format
Agent response format
Specify
response_format with ProviderStrategy to engage structured output when generating the agent’s final response.strict=True with the function_calling and json_schema methods to enforce exact schema adherence. The strict parameter is not supported with json_mode.
Reasoning output
For models that support reasoning (e.g.,anthropic/claude-sonnet-4.5, deepseek/deepseek-r1), you can enable reasoning tokens via the reasoning parameter. See the OpenRouter reasoning docs for details:
reasoning dict supports two 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".
usage_metadata:
The effort-to-budget mapping is model-dependent. For example, Google Gemini models map effort to an internal
thinkingLevel rather than an exact token budget. See the OpenRouter reasoning docs for details.Multimodal inputs
OpenRouter supports multimodal inputs for models that accept them. The available modalities depend on the model you select—check the OpenRouter models page for details.Supported input methods
| Method | Image | Audio | Video | |
|---|---|---|---|---|
| HTTP/HTTPS URLs | ✅ | ❌ | ✅ | ✅ |
| Base64 inline data | ✅ | ✅ | ✅ | ✅ |
Not all models support all modalities. Check the OpenRouter models page for model-specific support.
Image input
Provide image inputs along with text using aHumanMessage with list content format.
Audio input
Provide audio inputs along with text. Audio is passed as base64 inline data.Video input
Video inputs are automatically converted to OpenRouter’svideo_url format.
PDF input
Provide PDF file inputs along with text.Token usage metadata
After an invocation, token usage information is available on theusage_metadata attribute of the response:
Reasoning tokens
output_token_details.reasoning reports the number of tokens the model used for internal chain-of-thought reasoning. This appears when using reasoning models (e.g., deepseek/deepseek-r1, openai/o3) or when reasoning is explicitly enabled:
Cached input tokens
input_token_details.cache_read reports the number of input tokens served from the provider’s prompt cache, and input_token_details.cache_creation reports tokens written to the cache on the first call.
Prompt caching requires explicit cache_control breakpoints in message content blocks. Pass {"cache_control": {"type": "ephemeral"}} on the content block you want cached:
Without
cache_control on message content blocks, the provider will not cache the prompt and these fields will not appear.Response metadata
After an invocation, provider and model metadata is available on theresponse_metadata attribute:
native_finish_reason field, if present, contains the underlying provider’s original finish reason, which may differ from the normalized finish_reason.
Provider routing
Many models on OpenRouter are served by multiple providers. Theopenrouter_provider parameter gives you control over which providers handle your requests and how they’re selected.
Ordering and filtering providers
Useorder to set a preferred provider sequence. OpenRouter tries each provider in order and falls back to the next if one is unavailable:
only. To exclude certain providers, use ignore:
Sorting by cost, speed, or latency
By default, OpenRouter load-balances across providers with a preference for lower cost. Usesort to change the priority:
Data collection policy
If your use case requires that providers do not store or train on your data, setdata_collection to "deny":
Filtering by quantization
For open-weight models, you can restrict routing to specific precision levels:Route parameter
Theroute parameter controls high-level routing behavior:
"fallback": enable automatic failover across providers (default behavior)."sort": route based on the sorting strategy configured inopenrouter_provider.
Combining options
Provider options can be composed together:App attribution
OpenRouter supports app attribution via HTTP headers. You can set these through init params or environment variables:Observability and tracing
OpenRouter can broadcast request data to configured observability destinations.ChatOpenRouter exposes two related parameters: session_id for grouping related requests under a single logical workflow, and trace for per-request trace metadata. See the OpenRouter broadcast docs for details.
Grouping requests with session_id
Pass a session_id to associate multiple requests with the same workflow (a conversation, an agent run, a batch job, a CI run, and so on). Maximum 256 characters.
OPENROUTER_SESSION_ID environment variable is read at instantiation when no explicit session_id is provided, which lets a process tag every request without threading the value through application code.
You can also override the value per call:
Adding trace metadata with trace
Pass trace to attach per-request metadata that OpenRouter forwards to broadcast destinations. Recognized keys are trace_id, trace_name, span_name, generation_name, and parent_span_id; additional keys are passed through as custom metadata.
session_id and trace are independent — session_id groups requests into a logical workflow on the OpenRouter side, while trace annotates the individual request.
API reference
For detailed documentation of allChatOpenRouter features and configurations, head to the ChatOpenRouter API reference.
For more information about OpenRouter’s platform, models, and features, see the OpenRouter documentation.
Connect these docs to Claude, VSCode, and more via MCP for real-time answers.

