Skip to content

Using OpenRouter

Griffen Fargo edited this page Jan 29, 2026 · 1 revision

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

  1. Sign up at openrouter.ai
  2. Go to Keys and create a new API key
  3. Copy your key (starts with sk-or-v1-...)

2. Configure Coco

Option A: Project Config (.coco.config.json)

{
  "$schema": "https://coco.griffen.codes/schema.json",
  "mode": "interactive",
  "service": {
    "provider": "openai",
    "model": "anthropic/claude-3.5-sonnet",
    "baseURL": "https://openrouter.ai/api/v1",
    "authentication": {
      "type": "APIKey",
      "credentials": {
        "apiKey": "sk-or-v1-your-key-here"
      }
    }
  }
}

Option B: Git Config (~/.gitconfig)

git config --global coco.serviceProvider openai
git config --global coco.serviceModel "anthropic/claude-3.5-sonnet"
git config --global coco.serviceBaseURL "https://openrouter.ai/api/v1"
git config --global coco.serviceApiKey "sk-or-v1-your-key-here"

Option C: Environment Variables

export COCO_SERVICE_PROVIDER=openai
export COCO_SERVICE_MODEL="anthropic/claude-3.5-sonnet"
export COCO_SERVICE_BASE_URL="https://openrouter.ai/api/v1"
export COCO_SERVICE_API_KEY="sk-or-v1-your-key-here"

Option D: XDG Config (~/.config/coco/config.json)

{
  "service": {
    "provider": "openai",
    "model": "anthropic/claude-3.5-sonnet",
    "baseURL": "https://openrouter.ai/api/v1",
    "apiKey": "sk-or-v1-your-key-here"
  }
}

3. Use Coco

git add .
coco -i

Advanced Configuration

With Performance Tuning

{
  "service": {
    "provider": "openai",
    "model": "anthropic/claude-3.5-sonnet",
    "baseURL": "https://openrouter.ai/api/v1",
    "tokenLimit": 4096,
    "temperature": 0.32,
    "maxConcurrent": 12,
    "minTokensForSummary": 800,
    "maxFileTokens": 2000,
    "authentication": {
      "type": "APIKey",
      "credentials": {
        "apiKey": "sk-or-v1-your-key-here"
      }
    }
  }
}

With Custom Headers (Site/App Name)

OpenRouter allows you to set custom headers for tracking:

{
  "service": {
    "provider": "openai",
    "model": "anthropic/claude-3.5-sonnet",
    "baseURL": "https://openrouter.ai/api/v1",
    "fields": {
      "defaultHeaders": {
        "HTTP-Referer": "https://github.com/your-org/your-repo",
        "X-Title": "Coco Git Assistant"
      }
    },
    "authentication": {
      "type": "APIKey",
      "credentials": {
        "apiKey": "sk-or-v1-your-key-here"
      }
    }
  }
}

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-4o not just gpt-4o)

Rate Limiting

OpenRouter has rate limits per model. If you hit limits:

  1. Check your OpenRouter dashboard
  2. Consider upgrading your plan
  3. Use a different model temporarily

Cost Tracking

Monitor your usage:

  1. Visit OpenRouter Activity
  2. Set up budget alerts in your account settings
  3. Use cheaper models for development, premium for production

Switching Between Models

You can easily switch models without changing your config structure:

# Try different models
coco --service.model="google/gemini-pro-1.5"
coco --service.model="meta-llama/llama-3.1-70b-instruct"
coco --service.model="anthropic/claude-3.5-sonnet"

Benefits Over Direct Provider APIs

  1. Single API Key - One key for all providers
  2. Unified Billing - One invoice for all usage
  3. Easy Switching - Change models without reconfiguring
  4. Cost Comparison - See which models are most cost-effective
  5. Fallback Options - Automatically retry with different models
  6. Access to Exclusive Models - Some models only available through OpenRouter

See Also

Clone this wiki locally