Loading...
Loading...
Integrate CostLens in under 2 minutes.
Start optimizing costs immediately without CostLens API key. Perfect for development and testing.
npm install costlensimport { CostLens } from 'costlens';
import OpenAI from 'openai';
// No CostLens API key needed
const costlens = new CostLens();
const openai = new OpenAI({ apiKey: process.env.OPENAI_API_KEY });
const ai = costlens.wrapOpenAI(openai);
const response = await ai.chat.completions.create({
model: 'gpt-4',
messages: [{ role: 'user', content: 'Hello' }]
});
// ✅ Automatically optimized and routed to cheaper model!Full feature set with usage tracking, analytics, and team management.
npm install costlens# .env
COSTLENS_API_KEY=cl_your_api_key_here
OPENAI_API_KEY=sk-...import { CostLens } from 'costlens';
import OpenAI from 'openai';
const costlens = new CostLens({
apiKey: process.env.COSTLENS_API_KEY
});
const openai = new OpenAI({ apiKey: process.env.OPENAI_API_KEY });
const ai = costlens.wrapOpenAI(openai);
const response = await ai.chat.completions.create({
model: 'gpt-4',
messages: [{ role: 'user', content: 'Hello' }]
});.env fileCOSTLENS_API_KEY=cl_your_key_herenpm install costlensimport { CostLens } from 'costlens';
import OpenAI from 'openai';
const openai = new OpenAI();
const costlens = new CostLens({
apiKey: process.env.COSTLENS_API_KEY,
autoOptimize: false, // 🚀 Cost tracking (feature in development)
smartRouting: true, // 🚀 Routes to cheapest model
enableCache: true, // 🚀 Savings on repeated queries
costLimit: 0.10 // 🚀 Prevents budget overruns
});
// Wrap your client - savings happen automatically!
const tracked = costlens.wrapOpenAI(openai);
// Use it exactly like normal OpenAI with prompt tagging
const result = await tracked.chat.completions.create({
model: 'gpt-4',
messages: [{ role: 'user', content: 'Hello!' }]
}, {
promptId: 'user-greeting', // 📊 Track costs by use case
userId: 'user-123' // 📊 Track costs by user
});
// ✅ Prompt optimized, routed to cheaper model, cached, and tracked!💰 Money-saving features: smartRouting uses GPT-3.5 for simple queries (20x cheaper), and caching saves on repeated prompts. Real-time cost tracking helps you optimize spending.
📊 Analytics included: Track costs by prompt type, user, model, and provider. See real savings, success rates, and performance metrics in your dashboard.
import { CostLens } from 'costlens';
import Anthropic from '@anthropic-ai/sdk';
const anthropic = new Anthropic();
const costlens = new CostLens({
apiKey: process.env.COSTLENS_API_KEY
});
// Wrap and use
const tracked = costlens.wrapAnthropic(anthropic);
const result = await tracked.messages.create({
model: 'claude-3-opus-20240229',
max_tokens: 1024,
messages: [{ role: 'user', content: 'Hello!' }]
});
// ✅ Done!Tag your prompts to track costs by feature, user, or use case. Perfect for understanding which parts of your app cost the most.
// Tag by feature
await tracked.chat.completions.create({
model: 'gpt-4',
messages: [...]
}, {
promptId: 'code-review',
userId: 'user-123'
});
// Tag by use case
await tracked.chat.completions.create({
model: 'gpt-4',
messages: [...]
}, {
promptId: 'customer-support',
correlationId: 'ticket-456'
});
// Tag by user type
await tracked.chat.completions.create({
model: 'gpt-4',
messages: [...]
}, {
promptId: 'premium-user-query',
userId: 'premium-user-789'
});💡 Pro tip: Use consistent promptId naming (e.g., 'feature-action') to group related costs in your analytics dashboard.
Your AI costs are being optimized automatically. Check your analytics dashboard to see:
See detailed cost breakdowns, savings, and performance metrics
Get notified before you overspend
Track costs by feature, user, or use case
cl_