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: 4096Multi-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: 4096Configuration 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 tokenstopP: Nucleus sampling parametertopK: Sampling candidates countfrequencyPenalty: Frequency penaltypresencePenalty: Presence penalty
Environment Variables
bash
# ~/.bashrc or ~/.zshrc
export OPENROUTER_API_KEY="your-openrouter-api-key"Getting API Key
- Visit OpenRouter Website
- Register and log in to account
- Navigate to API Keys page
- Create new API key
- 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: 8000Fast 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: 2000Code Generation
yaml
models:
- name: "code-gen"
provider: "openrouter"
model: "openai/gpt-4o"
apiKey: "${OPENROUTER_API_KEY}"
roles: ["chat", "edit"]
defaultCompletionOptions:
temperature: 0.2
maxTokens: 4000Advanced 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: trueTroubleshooting
Common Errors
- 401 Unauthorized: Check if API key is correct
- 404 Model Not Found: Verify model identifier format
- 429 Too Many Requests: Rate limit reached, wait and retry
- 503 Service Unavailable: Model temporarily unavailable, try another model
Debugging Steps
- Verify API key format and validity
- Confirm model identifier is correct
- Check account balance and quotas
- 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 Category | Recommended Model | Use Case | Relative Cost |
|---|---|---|---|
| Highest Performance | GPT-4, Claude 3 Opus | Complex tasks | High |
| Balanced Performance | Claude 3 Sonnet, Gemini Pro | General tasks | Medium |
| Fast Response | Claude 3 Haiku, GPT-3.5 | Simple tasks | Low |
| Open Source | Llama 3, Mixtral | Specific purposes | Low |