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.
The problem with manual logging
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.
Auto-logging with track()
In v0.1.2, I added a track() function that wraps your OpenAI or Anthropic client:
npm install @ozperium/aicost-tracker
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', ... });
What gets logged
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
How it works
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.
Per-project breakdown
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.
Top comments (2)
Automatic logging is one of those features that feels small until the first weird model failure. Having the request shape, model, latency, token use, and response metadata in one place saves a lot of guesswork.
The main thing I would guard is accidental capture of sensitive prompt/tool data. For AI logs, the default should be useful observability with aggressive redaction, not a perfect transcript of everything the app saw.
I fully agree with your approach. Logging every single operation is a surefire way to leak personally identifiable information (PII) or trade secrets into the logs. Observability should provide insight into how the system works, not collect confidential information.
Today, users are increasingly spinning the reels on their smartphones, demanding maximum flexibility and mobility from the service. Mobile-optimized Fast Payout Casinos animeflix.city/ let you request payouts with a single click via Apple Pay, Google Pay, or crypto wallets. Manage your finances on the go and claim your winnings immediately after finishing your gaming session.