Skip to main content
OpenRouter provides access to multiple LLM providers through a single API, with automatic fallback, cost tracking, and model routing.

Configuration

string
required
Your OpenRouter API key from OpenRouter Dashboard
string
required
Model identifier in OpenRouter format. Examples:
  • anthropic/claude-4.5-sonnet
  • openai/gpt-4o
  • google/gemini-3.0-flash-thinking
  • deepseek/deepseek-reasoner
  • xai/grok-4
ModelInfo
Model metadata. OpenRouter fetches this automatically if not provided.
string
Provider preference order. Examples:
  • "anthropic,openai" - Prefer Anthropic, fallback to OpenAI
  • "google" - Use only Google
string
For reasoning models. Options: low, medium, high, xhigh
number
Token budget for extended thinking (model-dependent)

Basic Setup

Provider Routing

Control which providers handle your requests:

Reasoning Models

OpenRouter supports various reasoning models:

DeepSeek Reasoner

Gemini with Thinking

Anthropic Extended Thinking

Reasoning Details Preservation

OpenRouter passes reasoning_details for models to preserve reasoning traces:
From openrouter.ts:133-146:
Reasoning block preservationOpenRouter returns reasoning_details in delta responses that should be preserved and sent back in subsequent API requests to maintain reasoning traces. See OpenRouter docs.

Tool Calling

Cost Tracking

OpenRouter provides detailed cost information:

Generation Endpoint Fallback

If usage data isn’t in the stream, the handler fetches it from the generation endpoint:

Error Handling

OpenRouter returns errors in two ways:

Direct Error Response

Mid-Stream Error

Error Types

Streaming Response

Text Chunks

Reasoning Chunks

Tool Calls

Usage Data

Model Information

OpenRouter caches model info:
Model info is cached in StateManager to avoid repeated API calls.

Skipping Reasoning for Specific Models

Some models (like Grok 4) output placeholder reasoning:

Advanced Usage

Multiple Provider Fallback

Custom Model Info

Retry Configuration

Generation Details API

Access detailed generation info:
From openrouter.ts:171-193, the generation endpoint:
  • Returns native token counts
  • Includes cache statistics
  • Provides exact cost calculations
  • Has built-in retry logic (4 attempts)

Implementation Reference

Source: ~/workspace/source/src/core/api/providers/openrouter.ts Key features:
  • Unified access to 100+ models
  • Automatic provider fallback
  • Cost tracking with generation endpoint
  • Reasoning details preservation
  • Error handling for stream and response
  • Model info caching

Common Patterns

Cost Monitoring

Provider Preference

Multi-Model Strategy

Next Steps

Anthropic Provider

Direct Anthropic integration

OpenAI Provider

Direct OpenAI integration

Provider Overview

View all providers

Custom Provider

Build your own