Every time you call an AI API, you're spending money. But unless you're checking the dashboard after each session, you have no idea which project or model is driving the bill.
I solved this by wrapping my AI clients with a one-line middleware that logs every call automatically.
AICostTracker lets you run aicost log myapp gpt-4o 1500 800
to record a call. Useful — but nobody does this consistently. You forget, you're in flow, you'll do it later.
The logs end up patchy and the summary is useless.
track()
In v0.1.2, I added a track()
function that wraps your OpenAI or Anthropic client:
npm install @ozperium/aicost-tracker
python
import OpenAI from 'openai';
import { track } from '@ozperium/aicost-tracker';
const openai = track(new OpenAI(), { project: 'myapp' });
// From here, every call is logged automatically
const response = await openai.chat.completions.create({
model: 'gpt-4o',
messages: [{ role: 'user', content: 'Summarize this doc' }]
});
That's it. No changes to your existing code beyond the wrap.
It works the same way with Anthropic:
import Anthropic from '@anthropic-ai/sdk';
import { track } from '@ozperium/aicost-tracker';
const anthropic = track(new Anthropic(), { project: 'summarizer' });
const response = await anthropic.messages.create({ model: 'claude-3-5-sonnet-20241022', ... });
After a few sessions:
aicost summary
AI Cost Tracker — Summary
══════════════════════════════════════════════════
Total cost: $1.2847
Input tokens: 124,000
Output tokens: 67,500
By Project:
──────────────────────────────────────────────────
myapp $ 0.9102 31 calls
summarizer $ 0.3745 12 calls
By Model:
──────────────────────────────────────────────────
gpt-4o $ 0.9102 31 calls
claude-3-5-sonnet $ 0.3745 12 calls
track()
patches client.chat.completions.create
(OpenAI) or client.messages.create
(Anthropic) to intercept the response, extract usage from the usage
field, and call logUsage()
before returning. It's a thin synchronous wrapper — zero added latency.
If logging fails for any reason, the error is swallowed silently. Your API call always gets its response.
The { project: 'myapp' }
option lets you tag every call with a project name. If you're running multiple agents or tools in the same codebase, give each its own project tag:
const researchAgent = track(new OpenAI(), { project: 'research' });
const summaryAgent = track(new OpenAI(), { project: 'summary' });
Then aicost summary
shows the breakdown by project automatically.
GitHub: https://github.com/Ozperium/aicost-tracker
Also: AgentSpec for testing AI agent behavior, quota for monitoring rate limits.