Loading...
Loading...
Track and optimize your Anthropic API costs with automatic smart routing between Claude models.
💰 Cost Optimization: CostLens automatically routes Claude Opus requests to cheaper models when safe - save up to 98% on simple tasks!
npm install costlens @anthropic-ai/sdk
Use the wrapper for automatic tracking and cost optimization:
import { CostLens } from 'costlens';
import Anthropic from '@anthropic-ai/sdk';
const anthropic = new Anthropic({ apiKey: process.env.ANTHROPIC_API_KEY });
const costlens = new CostLens({
apiKey: process.env.COSTLENS_API_KEY,
smartRouting: true // Enable cost optimization
});
// Wrap your client
const tracked = costlens.wrapAnthropic(anthropic);
// Use it exactly like normal Anthropic
const result = await tracked.messages.create({
model: 'claude-3-opus-20240229', // May be auto-routed to claude-3-haiku
max_tokens: 1024,
messages: [{ role: 'user', content: 'Simple summary please' }]
});
// ✅ Automatically tracked and optimized!
✨ Smart Routing Benefits: Simple tasks automatically use Claude Haiku (98% cheaper), medium complexity uses Claude Sonnet (93% cheaper), complex tasks stay on Claude Opus for quality.
Save costs by caching responses:
const costlens = new CostLens({
apiKey: process.env.COSTLENS_API_KEY,
enableCache: true
});
const tracked = costlens.wrapAnthropic(anthropic);
const result = await tracked.messages.create(
{ model: 'claude-3-opus', max_tokens: 1024, messages: [...] },
{ cacheTTL: 3600000 } // Cache for 1 hour
);
For more control, track manually:
import { CostLens } from 'costlens';
import Anthropic from '@anthropic-ai/sdk';
const anthropic = new Anthropic({ apiKey: process.env.ANTHROPIC_API_KEY });
const costlens = new CostLens({
apiKey: process.env.COSTLENS_API_KEY
});
const params = {
model: 'claude-3-opus-20240229',
max_tokens: 1024,
messages: [{ role: 'user', content: 'Hello!' }]
};
const start = Date.now();
const result = await anthropic.messages.create(params);
await costlens.trackAnthropic(params, result, Date.now() - start);
import { CostLens } from 'costlens';
import Anthropic from '@anthropic-ai/sdk';
// Initialize
const costlens = new CostLens({
apiKey: process.env.COSTLENS_API_KEY
});
const anthropic = new Anthropic({
apiKey: process.env.ANTHROPIC_API_KEY
});
// Make your API call
const params = {
model: 'claude-3-opus-20240229',
max_tokens: 1024,
messages: [
{ role: 'user', content: 'Hello!' }
]
};
const start = Date.now();
const result = await anthropic.messages.create(params);
// Track the call
await costlens.trackAnthropic(params, result, Date.now() - start);
console.log(result.content[0].text);
Pass a promptId
to group and analyze specific prompts:
const params = {
model: 'claude-3-opus-20240229',
max_tokens: 1024,
messages: [...]
};
const start = Date.now();
const result = await anthropic.messages.create(params);
// Pass promptId as 4th parameter
await costlens.trackAnthropic(
params,
result,
Date.now() - start,
'content-generation-v1'
);
const start = Date.now();
try {
const result = await anthropic.messages.create(params);
await costlens.trackAnthropic(params, result, Date.now() - start);
return result;
} catch (error) {
await costlens.trackError(
'anthropic',
params.model,
JSON.stringify(params.messages),
error,
Date.now() - start
);
throw error;
}
Default pricing:
💡 Custom Pricing: Set your own negotiated rates in Settings → Pricing for maximum accuracy.
⚠️ Your Responsibility: You are responsible for verifying and setting accurate pricing. CostLens is not responsible for pricing accuracy. Always check anthropic.com/pricing for current rates.