ReqLLM.Providers.OpenRouter (ReqLLM v1.0.0)
View SourceOpenRouter provider – OpenAI Chat Completions compatible with OpenRouter's unified API.
Implementation
Uses built-in OpenAI-style encoding/decoding defaults. No custom wrapper modules – leverages the standard OpenAI-compatible implementations.
OpenRouter-Specific Extensions
Beyond standard OpenAI parameters, OpenRouter supports:
openrouter_models- Array of model IDs for routing/fallback preferencesopenrouter_route- Routing strategy (e.g., "fallback")openrouter_provider- Provider preferences object for routing decisionsopenrouter_transforms- Array of prompt transforms to applyopenrouter_top_k- Top-k sampling (not available for OpenAI models)openrouter_repetition_penalty- Repetition penalty for reducing repetitive textopenrouter_min_p- Minimum probability threshold for samplingopenrouter_top_a- Top-a sampling parameterapp_referer- HTTP-Referer header for app identificationapp_title- X-Title header for app title in rankings
App Attribution Headers
OpenRouter supports optional headers for app discoverability:
- Set
HTTP-Refererheader for app identification - Set
X-Titleheader for app title in rankings
See provider_schema/0 for the complete OpenRouter-specific schema and
ReqLLM.Provider.Options for inherited OpenAI parameters.
Configuration
# Add to .env file (automatically loaded)
OPENROUTER_API_KEY=sk-or-...
Summary
Functions
Default implementation of attach/3.
Default implementation of attach_stream/4.
Default implementation of decode_response/1.
Default implementation of decode_stream_event/2.
Callback implementation for ReqLLM.Provider.default_env_key/0.
Custom body encoding that adds OpenRouter-specific extensions to the default OpenAI-compatible format.
Default implementation of extract_usage/2.
Custom prepare_request for :object operations to maintain OpenRouter-specific max_tokens handling.
Default implementation of translate_options/3.
Functions
Default implementation of attach/3.
Sets up Bearer token authentication and standard pipeline steps.
Default implementation of attach_stream/4.
Builds complete streaming requests using OpenAI-compatible format.
Default implementation of decode_response/1.
Handles success/error responses with standard ReqLLM.Response creation.
Default implementation of decode_stream_event/2.
Decodes SSE events using OpenAI-compatible format.
Callback implementation for ReqLLM.Provider.default_env_key/0.
Custom body encoding that adds OpenRouter-specific extensions to the default OpenAI-compatible format.
Adds support for OpenRouter routing and sampling parameters:
- models (routing preferences)
- route (routing strategy)
- provider (provider preferences)
- transforms (prompt transforms)
- top_k, repetition_penalty, min_p, top_a (sampling parameters)
- top_logprobs (log probabilities)
Also handles OpenRouter-specific app attribution headers:
- HTTP-Referer header for app identification
- X-Title header for app title in rankings
Default implementation of extract_usage/2.
Extracts usage data from standard usage field in response body.
Custom prepare_request for :object operations to maintain OpenRouter-specific max_tokens handling.
Ensures that structured output requests have adequate token limits while delegating other operations to the default implementation.
Default implementation of translate_options/3.
Pass-through implementation that returns options unchanged.