Skip to main content

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

  1. Sign up at OpenRouter
  2. Go to API Keys
  3. Create a new key
  4. 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)

ModelDescription
meta-llama/llama-3.2-3b-instruct:freeFast, basic tasks
meta-llama/llama-3.2-1b-instruct:freeSmaller, faster
google/gemma-2-9b-it:freeGoogle's open model
ModelDescription
anthropic/claude-3.5-sonnetBest quality
openai/gpt-4oExcellent reasoning
meta-llama/llama-3.1-70b-instructGood 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:

ModelInput (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

  1. Use free models for testing - Save costs during development
  2. Cache responses - Avoid redundant API calls
  3. Monitor usage - Track costs in OpenRouter dashboard
  4. Handle errors gracefully - Rate limits, timeouts, etc.