Best Practices
Optimize your AIRouter integration for performance, cost, and reliability
💰 Cost Optimization
1. Enable Response Caching
Cached responses cost $0 and return instantly. Enable for all repeated queries.
{
"model": "gpt-3.5-turbo",
"messages": [...],
"enableCache": true // Default: true
}2. Use Cheaper Models When Possible
❌ Bad
model: "gpt-4" // $30/1M tokens✅ Good
model: "gpt-3.5-turbo" // $0.50/1M tokens3. Use Cost-Based Routing
Let AIRouter automatically select the cheapest provider
{
"routingStrategy": "cost"
}⚡ Performance Optimization
1. Use Latency-Based Routing
For real-time applications, route to the fastest provider
{
"routingStrategy": "latency"
}2. Monitor Rate Limits
Check response headers to avoid hitting rate limits
const remaining = response.headers.get('X-RateLimit-Remaining');
const reset = response.headers.get('X-RateLimit-Reset');
if (remaining < 10) {
console.warn(`Only ${remaining} requests remaining`);
}🔐 Security Best Practices
1. Secure API Key Storage
❌ Bad
const key = "sk-air-1234..."✅ Good
const key = process.env.API_KEY2. Use Appropriate Permissions
Create separate keys with minimal required permissions
- Read-only keys for analytics dashboards
- Full access keys for production apps
- Set expiration dates for temporary keys
3. Rotate Keys Regularly
Implement key rotation every 90 days for production environments
🛡️ Reliability Best Practices
1. Use Fallback Routing for Critical Operations
{
"routingStrategy": "fallback" // Automatic failover
}2. Implement Error Handling
try {
const response = await chat({...});
} catch (error) {
if (error.status === 429) {
// Rate limited - wait and retry
} else if (error.status === 402) {
// Insufficient credits
} else if (error.status === 503) {
// Service unavailable
}
}✅ Quick Checklist
- ✓ Enable response caching
- ✓ Use appropriate routing strategy
- ✓ Store API keys securely
- ✓ Monitor rate limits
- ✓ Implement error handling
- ✓ Track costs in analytics
- ✓ Set up fallback routing for critical systems
- ✓ Rotate API keys regularly