OpenRouter Integration
Cync uses OpenRouter as a gateway to access multiple AI models from a single API.
Overview
OpenRouter provides:
- Access to 100+ AI models
- Single API, multiple providers
- Usage-based pricing
- Free tier models available
Setup
1. Get API Key
- Sign up at OpenRouter
- Go to API Keys
- Create a new key
- Copy the key
2. Configure Environment
# .env.local
OPENROUTER_API_KEY=sk-or-v1-xxx
OPENROUTER_MODEL=anthropic/claude-3.5-sonnet
OPENROUTER_BASE_URL=https://openrouter.ai/api/v1
Model Selection
Free Models (Testing)
| Model | Description |
|---|---|
meta-llama/llama-3.2-3b-instruct:free | Fast, basic tasks |
meta-llama/llama-3.2-1b-instruct:free | Smaller, faster |
google/gemma-2-9b-it:free | Google's open model |
Paid Models (Production)
| Model | Description |
|---|---|
anthropic/claude-3.5-sonnet | Best quality |
openai/gpt-4o | Excellent reasoning |
meta-llama/llama-3.1-70b-instruct | Good balance |
Browse all models: https://openrouter.ai/models
API Usage
Chat Completions
const response = await fetch('https://openrouter.ai/api/v1/chat/completions', {
method: 'POST',
headers: {
'Authorization': `Bearer ${OPENROUTER_API_KEY}`,
'Content-Type': 'application/json',
'HTTP-Referer': 'https://cync.aicholdings.com',
'X-Title': 'Cync'
},
body: JSON.stringify({
model: 'anthropic/claude-3.5-sonnet',
messages: [
{ role: 'system', content: 'You are a helpful assistant.' },
{ role: 'user', content: 'Hello!' }
]
})
});
const data = await response.json();
console.log(data.choices[0].message.content);
With Tool Use
const response = await fetch('https://openrouter.ai/api/v1/chat/completions', {
method: 'POST',
headers: { /* ... */ },
body: JSON.stringify({
model: 'anthropic/claude-3.5-sonnet',
messages: [ /* ... */ ],
tools: [
{
type: 'function',
function: {
name: 'get_stickies',
description: 'Retrieve user notes',
parameters: {
type: 'object',
properties: {
tags: {
type: 'array',
items: { type: 'string' },
description: 'Filter by tags'
}
}
}
}
}
]
})
});
Cync Usage
Cync Summaries
Multi-agent pipeline uses OpenRouter for:
- Historian analysis
- Editor composition
- Strategist insights
- Validator scoring
- Polisher refinement
AI Chat
Chat interfaces (/protected/rhea) use OpenRouter for:
- Natural language queries
- Tool-calling decisions
- Response generation
Rate Limits
Rate limits depend on the model and your tier:
- Free models: Generous for testing
- Paid models: Tier-dependent
Check limits: https://openrouter.ai/docs#limits
Handling Rate Limits
if (response.status === 429) {
const retryAfter = response.headers.get('Retry-After');
await sleep(parseInt(retryAfter) * 1000);
// Retry request
}
Costs
OpenRouter uses pay-per-use pricing:
| Model | Input (per 1M tokens) | Output (per 1M tokens) |
|---|---|---|
| Claude 3.5 Sonnet | $3.00 | $15.00 |
| GPT-4o | $5.00 | $15.00 |
| Llama 3.1 70B | $0.59 | $0.79 |
Check current prices: https://openrouter.ai/models
Best Practices
- Use free models for testing - Save costs during development
- Cache responses - Avoid redundant API calls
- Monitor usage - Track costs in OpenRouter dashboard
- Handle errors gracefully - Rate limits, timeouts, etc.