Skip to content

OpenRouter

OpenRouter is a model routing service providing unified access to multiple AI models, supporting model switching and load balancing.

Supported Models

OpenAI Models

  • openai/gpt-4o - Latest GPT-4 optimized version
  • openai/gpt-4-turbo - Fast GPT-4 version
  • openai/gpt-3.5-turbo - Cost-effective model

Anthropic Models

  • anthropic/claude-3-opus - Most powerful Claude model
  • anthropic/claude-3-sonnet - Balance performance and speed
  • anthropic/claude-3-haiku - Fast response

Google Models

  • google/gemini-pro - Gemini Pro model
  • google/gemini-pro-vision - Supports image input

Meta Models

  • meta-llama/llama-3-70b-instruct - LLaMA 3 large model
  • meta-llama/llama-3-8b-instruct - LLaMA 3 lightweight

Mistral Models

  • mistralai/mistral-7b-instruct - Mistral 7B model
  • mistralai/mixtral-8x7b-instruct - Mixtral MoE model

Other Models

  • cohere/command-r-plus - Cohere command model
  • perplexity/pplx-70b-online - Perplexity online model
  • qwen/qwen-2-72b-instruct - Tongyi Qianwen model

Configuration

Basic Configuration

Configure in config.yaml or ~/.bytebuddy/config.yaml:

yaml
models:
  - name: "openrouter-gpt4"
    provider: "openrouter"
    model: "openai/gpt-4o"
    apiKey: "${OPENROUTER_API_KEY}"
    roles: ["chat", "edit"]
    defaultCompletionOptions:
      temperature: 0.7
      maxTokens: 4096

Multi-Model Configuration

yaml
models:
  - name: "openrouter-claude"
    provider: "openrouter"
    model: "anthropic/claude-3-sonnet"
    apiKey: "${OPENROUTER_API_KEY}"
    roles: ["chat"]
    defaultCompletionOptions:
      temperature: 0.5
      maxTokens: 4096

  - name: "openrouter-gemini"
    provider: "openrouter"
    model: "google/gemini-pro"
    apiKey: "${OPENROUTER_API_KEY}"
    roles: ["chat"]
    defaultCompletionOptions:
      temperature: 0.7
      maxTokens: 4096

  - name: "openrouter-llama"
    provider: "openrouter"
    model: "meta-llama/llama-3-70b-instruct"
    apiKey: "${OPENROUTER_API_KEY}"
    roles: ["chat"]
    defaultCompletionOptions:
      temperature: 0.6
      maxTokens: 4096

Configuration Fields

Required Fields

  • name: Unique identifier for the model configuration
  • provider: Set to "openrouter"
  • model: Model identifier (format: provider/model-name)
  • apiKey: OpenRouter API key

Optional Fields

  • roles: Model roles [chat, edit, apply, autocomplete]
  • defaultCompletionOptions:
    • temperature: Control randomness (0-2)
    • maxTokens: Maximum tokens
    • topP: Nucleus sampling parameter
    • topK: Sampling candidates count
    • frequencyPenalty: Frequency penalty
    • presencePenalty: Presence penalty

Environment Variables

bash
# ~/.bashrc or ~/.zshrc
export OPENROUTER_API_KEY="your-openrouter-api-key"

Getting API Key

  1. Visit OpenRouter Website
  2. Register and log in to account
  3. Navigate to API Keys page
  4. Create new API key
  5. Save the key to environment variable

Use Case Configurations

High-Quality Chat

yaml
models:
  - name: "quality-chat"
    provider: "openrouter"
    model: "anthropic/claude-3-opus"
    apiKey: "${OPENROUTER_API_KEY}"
    roles: ["chat"]
    defaultCompletionOptions:
      temperature: 0.7
      maxTokens: 8000

Fast Response

yaml
models:
  - name: "fast-response"
    provider: "openrouter"
    model: "anthropic/claude-3-haiku"
    apiKey: "${OPENROUTER_API_KEY}"
    roles: ["chat", "autocomplete"]
    defaultCompletionOptions:
      temperature: 0.5
      maxTokens: 2000

Code Generation

yaml
models:
  - name: "code-gen"
    provider: "openrouter"
    model: "openai/gpt-4o"
    apiKey: "${OPENROUTER_API_KEY}"
    roles: ["chat", "edit"]
    defaultCompletionOptions:
      temperature: 0.2
      maxTokens: 4000

Advanced Configuration

Complete Configuration Example

yaml
models:
  - name: "openrouter-advanced"
    provider: "openrouter"
    model: "openai/gpt-4o"
    apiKey: "${OPENROUTER_API_KEY}"
    roles: ["chat", "edit", "apply"]
    capabilities: ["tool_use"]
    defaultCompletionOptions:
      temperature: 0.7
      maxTokens: 4096
      topP: 0.9
      frequencyPenalty: 0.1
      presencePenalty: 0.1
      stream: true
    requestOptions:
      timeout: 60000
      verifySsl: true

Troubleshooting

Common Errors

  1. 401 Unauthorized: Check if API key is correct
  2. 404 Model Not Found: Verify model identifier format
  3. 429 Too Many Requests: Rate limit reached, wait and retry
  4. 503 Service Unavailable: Model temporarily unavailable, try another model

Debugging Steps

  1. Verify API key format and validity
  2. Confirm model identifier is correct
  3. Check account balance and quotas
  4. View OpenRouter status page

Best Practices

1. Model Selection

  • Complex Tasks: Use GPT-4 or Claude 3 Opus
  • General Chat: Use Claude 3 Sonnet or Gemini Pro
  • Fast Response: Use Claude 3 Haiku
  • Cost Sensitive: Use GPT-3.5 Turbo

2. Security

  • Use environment variables to store API keys
  • Rotate keys regularly
  • Monitor unusual usage

3. Performance Optimization

  • Enable streaming responses for better UX
  • Choose appropriate model for the task
  • Set reasonable timeout values

4. Cost Control

  • Monitor API usage
  • Choose models based on task complexity
  • Set reasonable maxTokens limits

Model Comparison

Model CategoryRecommended ModelUse CaseRelative Cost
Highest PerformanceGPT-4, Claude 3 OpusComplex tasksHigh
Balanced PerformanceClaude 3 Sonnet, Gemini ProGeneral tasksMedium
Fast ResponseClaude 3 Haiku, GPT-3.5Simple tasksLow
Open SourceLlama 3, MixtralSpecific purposesLow