DEV Community

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

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 shows:

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.

Comments

No comments yet. Start the discussion.