openrouter-openai-compat
Installation
SKILL.md
OpenRouter OpenAI Compatibility
Overview
OpenRouter implements the OpenAI Chat Completions API specification (/v1/chat/completions). Existing OpenAI SDK code works with OpenRouter by changing two values: base_url and api_key. This gives you access to 400+ models from all providers through the same SDK interface.
Prerequisites
- An existing OpenAI SDK integration to migrate — Python or TypeScript code calling
chat.completions.create - An OpenRouter API key exported as
OPENROUTER_API_KEY— see theopenrouter-install-authskill for setup - Python 3.8+ with the
openaipackage, or Node.js 18+ with theopenainpm package — the same SDK you already use, no new dependency - Optionally keep
OPENAI_API_KEYexported too, so the Dual-Provider Pattern can switch back to direct OpenAI
Instructions
- Apply The Two-Line Migration: point
base_urlathttps://openrouter.ai/api/v1and swapapi_keytoOPENROUTER_API_KEY; optionally add theHTTP-Referer/X-Titleheaders for app attribution. - Prefix every model string per Model ID Mapping —
gpt-4obecomesopenai/gpt-4o,o1becomesopenai/o1— and try a non-OpenAI model (anthropic/claude-3.5-sonnet) through the same client. - Confirm your feature usage against What Works Identically (streaming,
tools, JSON mode,stop,n) and adjust per What Differs — remove theorganizationparam, plan around limited embeddings, and checklogprobssupport per model via/api/v1/models. - Layer in OpenRouter-Only Features through
extra_body: ordered fallback model lists with"route": "fallback", provider preferences withsort: "price", or theplugins: [{"id": "web"}]web-search plugin. - Keep the migration reversible with the Dual-Provider Pattern —
create_client()switches between direct OpenAI and OpenRouter off theLLM_PROVIDERenvironment variable.