Why Track Token Usage?
Every LLM API call costs money. Without tracking, you're flying blind — a single misconfigured prompt can burn through your monthly budget in hours. Token tracking gives you:
Setup: Install the SDK
npm install @aicostguard/sdk
Basic Usage with OpenAI
import { CostGuard } from '@aicostguard/sdk';
import OpenAI from 'openai';
const guard = new CostGuard({
apiKey: process.env.COST_GUARD_API_KEY!,
projectId: 'my-chatbot',
});
const openai = guard.wrap(new OpenAI());
// Use openai as normal — every call is tracked automatically
const response = await openai.chat.completions.create({
model: 'gpt-4o',
messages: [{ role: 'user', content: 'Summarize this article...' }],
});
That's it. Two lines of setup, and every API call now logs:
Tracking Anthropic (Claude)
import Anthropic from '@anthropic-ai/sdk';
const anthropic = guard.wrap(new Anthropic());
const message = await anthropic.messages.create({
model: 'claude-sonnet-4-20250514',
max_tokens: 1024,
messages: [{ role: 'user', content: 'Hello, Claude!' }],
});
Adding Custom Metadata
Tag requests with custom metadata for fine-grained attribution:
const response = await openai.chat.completions.create({
model: 'gpt-4o-mini',
messages: [{ role: 'user', content: userQuery }],
}, {
costGuard: {
userId: user.id,
feature: 'customer-support',
environment: 'production',
},
});
Viewing Your Data
Once integrated, visit your AI Cost Guard dashboard to see:
Setting Budget Alerts
// In your AI Cost Guard dashboard, or via API:
await guard.setBudgetAlert({
amount: 500, // $500/month
alertAt: [0.5, 0.8, 1.0], // Alert at 50%, 80%, 100%
action: 'notify', // or 'auto-stop'
channels: ['slack', 'email'],
});