APIs·5 min read

How to route between models with OpenRouter

OpenRouter is a unified API that gives access to 200+ LLM models through a single endpoint. This guide covers setup, model selection, cost optimisation, and fallback routing.

What OpenRouter solves

Managing multiple LLM providers means multiple API keys, multiple SDKs, different rate limits, and different pricing. OpenRouter unifies all of them behind a single OpenAI-compatible API endpoint. One key, one SDK, access to GPT-4o, Claude, Gemini, Llama, Mistral, DeepSeek, and hundreds more.

OpenRouter also adds a routing layer: you can set fallbacks so if Claude is unavailable, requests automatically route to GPT-4o; or use cost routing to automatically select the cheapest model that meets your quality requirement.

Set up OpenRouter

Create an account at openrouter.ai. Add credits (minimum $5) and generate an API key. Set it as `OPENROUTER_API_KEY` in your environment.

OpenRouter is fully OpenAI API-compatible. Use the OpenAI SDK with a custom base URL: Python: `client = OpenAI(base_url='https://openrouter.ai/api/v1', api_key=os.environ['OPENROUTER_API_KEY'])`. Node: `const client = new OpenAI({baseURL: 'https://openrouter.ai/api/v1', apiKey: process.env.OPENROUTER_API_KEY})`.

Select and compare models

Model IDs on OpenRouter follow the format `provider/model-name`. Examples: `anthropic/claude-sonnet-4-5`, `openai/gpt-4o`, `meta-llama/llama-3.3-70b-instruct`, `deepseek/deepseek-r1`, `google/gemini-2.5-flash`.

The OpenRouter model list at openrouter.ai/models shows pricing, context length, and benchmarks for every available model. Filter by price, context window, or provider to find the right model for your use case.

Cost optimisation with auto-routing

Use `openai/auto` as the model to let OpenRouter automatically select the cheapest model that can handle your request. Add a `route` header to specify preferences: `{'HTTP-Referer': 'your-app', 'X-Title': 'your-app', 'route': 'cheapest'}`.

For tasks where quality varies a lot by model, use the model ranking feature: pass a list of models in order of preference with a fallback option. OpenRouter tries the first model; if it is unavailable, it tries the next.