OpenRouter: one API key for 300+ AI models (account, API, OpenCode, VSCode)
| Introduction | |
| Create an account | |
| Create an API key | |
| Try models on the website | |
| Call models from code | |
| Add OpenRouter to OpenCode | |
| Add OpenRouter to VS Code | |
| Keys, limits and privacy | |
| Troubleshooting |
Introduction
OpenRouter is a gateway to 300+ AI models behind one API key and
one bill. Instead of registering with every provider, you register once,
top up credits, and call any model through an OpenAI-compatible API:
flagship commercial models, cheap fast models, open-weight models, and
free models for experiments.
Two typical uses on this site: as a model source for the
OpenCode coding agent (one OpenRouter key
replaces a pocketful of provider keys), and as a cloud alternative to
running Qwen locally when you need more quality
than your hardware can serve.
Create an account
- Open openrouter.ai and sign up (Google or GitHub login works, email works too).
- Free models work with just the account — no payment needed to start.
- Paid models need topped-up credits: open the credits/billing page and add funds with a card. Usage is pay-as-you-go, billed per million tokens; exact prices sit next to every model on the models page and do change, so check them there, not in blog posts.
Create an API key
- Open openrouter.ai/settings/keys and create a key. It starts with sk-or- — copy it immediately, it is shown once.
- Put the key in an environment variable instead of pasting it into files:
export OPENROUTER_API_KEY="sk-or-your-key-here"
Treat the key like a password: never commit it to git, never paste it into chats or screenshots. If a key leaks, delete it on the keys page and create a new one.
Try models on the website
Before wiring anything, try a model in the browser:
- Open openrouter.ai/models: every model shows price per million tokens, context length, and whether a free variant exists (free variants end with :free).
- Open a model and use the chat view to ask a few questions from your own domain — this is the cheapest way to compare quality before spending credits in an agent loop.
- Copy the exact model id (format provider/model, for example openai/gpt-4o) — you will need it verbatim in code and configs below.
Call models from code
The API endpoint is https://openrouter.ai/api/v1 and speaks the OpenAI chat format. Minimal request with curl:
curl https://openrouter.ai/api/v1/chat/completions -H "Content-Type: application/json" -H "Authorization: Bearer $OPENROUTER_API_KEY" -d '{"model": "openai/gpt-4o", "messages": [{"role": "user", "content": "Hi"}]}'
Same call from Python (needs openai package):
pip install openai
import os from openai import OpenAI client = OpenAI( base_url="https://openrouter.ai/api/v1", api_key=os.environ["OPENROUTER_API_KEY"], ) answer = client.chat.completions.create( model="openai/gpt-4o", messages=[{"role": "user", "content": "Say hi"}], ) print(answer.choices[0].message.content)
Add OpenRouter to OpenCode
Full agent setup lives in OpenCode: download, install and use — here is only the OpenRouter part:
- Start OpenCode (opencode) and type /connect.
- Pick the OpenRouter provider and paste your key (the sk-or- one from above).
- Type /models to list the model ids available to you, then pin your default in opencode.json with the exact id from that list.
Add OpenRouter to VS Code
Any VS Code AI extension that accepts a custom provider works; three common ones:
- Cline. Install Cline from the Marketplace, open its settings, set the API provider to OpenRouter, paste the key, pick a model from the dropdown.
- Roo Code. Same flow: install, choose the OpenRouter provider, paste the key, pick a model.
- Continue. Install Continue and add a model block to config.yaml (replace the id and the key):
models: - name: OpenRouter GPT-4o provider: openrouter model: openai/gpt-4o apiKey: YOUR_OPENROUTER_KEY
Prefer an environment variable over a hardcoded key where the extension supports it, and check the extension docs for the current field names.
Keys, limits and privacy
- One key per purpose. Create separate keys for experiments, coding agents, and apps — a leaked agent key then costs you one revocation, not a full reset. Name keys so you know what to delete.
- Spend limits. Set a credit limit on the billing page before pointing an autonomous agent at a paid model; agent loops burn tokens fast.
- Free vs paid privacy. Paid requests are not used for model training by default; free-tier prompts may be used to improve models. Never send secrets or customer data to a free endpoint — re-check the current privacy policy, it changes.
- Free-tier rate limits. Free models have daily request limits, so a sudden 429 in the middle of a session usually means the quota, not your key.
Troubleshooting
- 401 Unauthorized: the key is wrong (extra spaces or a newline from copy-paste are the usual cause — the key must start with sk-or-), revoked, or the variable is empty in this shell. Print the first characters to check what the code actually sees.
- Model not found: the id must match openrouter.ai/models exactly, including the provider prefix and the :free suffix for free variants.
- No credits / payment required: paid models stop when the balance hits zero — top up on the billing page and retry.
- Empty or cut answers: you hit the model context or output limit; shorten the input or switch to a model with a larger context window (shown on the models page).
- Slow first token on free models: normal — free endpoints are queued behind paid traffic.
Official sources:
- openrouter.ai
- openrouter.ai/docs
- openrouter.ai/models
Article author: Andrei Olegovich
| AI | |
| OpenRouter: one API key for 300+ AI models (account, API, OpenCode, VSCode) |