cd /news/developer-tools/stop-manually-logging-your-ai-api-ca… · home topics developer-tools article
[ARTICLE · art-72992] src=dev.to ↗ pub= topic=developer-tools verified=true sentiment=↑ positive

Stop manually logging your AI API calls — one-line auto-logging for OpenAI and Anthropic

A developer released AICostTracker, an open-source tool that adds one-line auto-logging for OpenAI and Anthropic API calls. The library's track() function wraps the client to intercept responses and extract token usage, providing per-project cost summaries via CLI without modifying existing code.

read2 min views1 publishedJul 25, 2026

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.

── more in #developer-tools 4 stories · sorted by recency
── more on @aicosttracker 3 stories trending now
sponsored brought to you by zahid.host 4,200+ EU-deployed projects
reading about agents? ship yours in a single git push.

Run your AI side-project on zahid.host

EU-based hosting, git-push deploys, automatic HTTPS, no cold starts. Free tier with a custom domain — perfect for shipping the agent you just read about.

$git push zahid main
Live at https://your-agent.zahid.host
Get free account → Pricing
from €0/mo · no card required
LIVE [news/stop-manually-loggin…] indexed:0 read:2min 2026-07-25 ·