OpenRouter is a unified API that provides access to multiple AI models from different providers (OpenAI, Anthropic, Google, Meta, etc.) through a single endpoint. This is useful for:
- Cost optimization - Compare prices across providers
- Model flexibility - Switch between models without changing code
- Fallback options - Use alternative models if the primary is unavailable
- Access to exclusive models - Some models are only available through OpenRouter
Quick Start
1. Get an OpenRouter API Key
- Sign up at openrouter.ai
- Go to Keys and create a new API key
- Copy your key (starts with
sk-or-v1-...)
2. Configure Coco
Option A: Project Config (.coco.config.json)
json
1{
2 "$schema": "https://coco.griffen.codes/schema.json",
3 "mode": "interactive",
4 "service": {
5 "provider": "openai",
6 "model": "anthropic/claude-3.5-sonnet",
7 "baseURL": "https://openrouter.ai/api/v1",
8 "authentication": {
9 "type": "APIKey",
10 "credentials": {
11 "apiKey": "sk-or-v1-your-key-here"
12 }
13 }
14 }
15}Option B: Git Config (~/.gitconfig)
bash
1git config --global coco.serviceProvider openai
2git config --global coco.serviceModel "anthropic/claude-3.5-sonnet"
3git config --global coco.serviceBaseURL "https://openrouter.ai/api/v1"
4git config --global coco.serviceApiKey "sk-or-v1-your-key-here"Option C: Environment Variables
bash
1export COCO_SERVICE_PROVIDER=openai
2export COCO_SERVICE_MODEL="anthropic/claude-3.5-sonnet"
3export COCO_SERVICE_BASE_URL="https://openrouter.ai/api/v1"
4export COCO_SERVICE_API_KEY="sk-or-v1-your-key-here"Option D: XDG Config (~/.config/coco/config.json)
json
1{
2 "service": {
3 "provider": "openai",
4 "model": "anthropic/claude-3.5-sonnet",
5 "baseURL": "https://openrouter.ai/api/v1",
6 "apiKey": "sk-or-v1-your-key-here"
7 }
8}3. Use Coco
bash
git add .
coco commit -iAdvanced Configuration
With Performance Tuning
json
1{
2 "service": {
3 "provider": "openai",
4 "model": "anthropic/claude-3.5-sonnet",
5 "baseURL": "https://openrouter.ai/api/v1",
6 "tokenLimit": 4096,
7 "temperature": 0.32,
8 "maxConcurrent": 12,
9 "minTokensForSummary": 800,
10 "maxFileTokens": 2000,
11 "authentication": {
12 "type": "APIKey",
13 "credentials": {
14 "apiKey": "sk-or-v1-your-key-here"
15 }
16 }
17 }
18}With Custom Headers (Site/App Name)
OpenRouter allows you to set custom headers for tracking:
json
1{
2 "service": {
3 "provider": "openai",
4 "model": "anthropic/claude-3.5-sonnet",
5 "baseURL": "https://openrouter.ai/api/v1",
6 "fields": {
7 "defaultHeaders": {
8 "HTTP-Referer": "https://github.com/your-org/your-repo",
9 "X-Title": "Coco Git Assistant"
10 }
11 },
12 "authentication": {
13 "type": "APIKey",
14 "credentials": {
15 "apiKey": "sk-or-v1-your-key-here"
16 }
17 }
18 }
19}Troubleshooting
"Invalid API Key" Error
Make sure your API key:
- Starts with
sk-or-v1- - Has sufficient credits in your OpenRouter account
- Is correctly set in your config
"Model Not Found" Error
Check the model name format:
- Use the full model ID from OpenRouter (e.g.,
anthropic/claude-3.5-sonnet) - Don't use OpenAI-specific model names (e.g., use
openai/gpt-4onot justgpt-4o)
Rate Limiting
OpenRouter has rate limits per model. If you hit limits:
- Check your OpenRouter dashboard
- Consider upgrading your plan
- Use a different model temporarily
Cost Tracking
Monitor your usage:
- Visit OpenRouter Activity
- Set up budget alerts in your account settings
- Use cheaper models for development, premium for production
Switching Between Models
You can easily switch models without changing your config structure:
bash
1# Try different models
2coco commit --service.model="google/gemini-pro-1.5"
3coco commit --service.model="meta-llama/llama-3.1-70b-instruct"
4coco commit --service.model="anthropic/claude-3.5-sonnet"Benefits Over Direct Provider APIs
- Single API Key - One key for all providers
- Unified Billing - One invoice for all usage
- Easy Switching - Change models without reconfiguring
- Cost Comparison - See which models are most cost-effective
- Fallback Options - Automatically retry with different models
- Access to Exclusive Models - Some models only available through OpenRouter