DEV Community

Paw from Oz
Paw from Oz

Posted on

I built a local-first CLI to track what I'm actually spending on AI APIs

The worst part of using AI APIs isn't the cost. It's not knowing what you're spending until the bill arrives.

I wanted per-project cost tracking — which project used the most tokens? Which model was the expensive one? The cloud dashboards don't tell you that.

So I built AICostTracker — a local CLI that logs every API call and breaks down costs by project and model.

Auto-logging (the easy way)

Install as a library and wrap your AI client — every call is logged automatically, no manual work:

npm install @ozperium/aicost-tracker
Enter fullscreen mode Exit fullscreen mode
import OpenAI from 'openai';
import { track } from '@ozperium/aicost-tracker';

const openai = track(new OpenAI(), { project: 'myapp' });

// Nothing else to do — all calls now logged
const response = await openai.chat.completions.create({
  model: 'gpt-4o',
  messages: [{ role: 'user', content: 'hello' }]
});
Enter fullscreen mode Exit fullscreen mode

Works with Anthropic too:

import Anthropic from '@anthropic-ai/sdk';
import { track } from '@ozperium/aicost-tracker';

const anthropic = track(new Anthropic(), { project: 'docs-bot' });
// All messages.create calls now logged automatically
Enter fullscreen mode Exit fullscreen mode

The wrapper is transparent — it never surfaces logging errors to your code, and adds zero latency to the API call itself.

See your costs

aicost summary
Enter fullscreen mode Exit fullscreen mode
  AI Cost Tracker — Summary
  ══════════════════════════════════════════════════
  Total entries:    12
  Total cost:       $0.4821
  Input tokens:     48,000
  Output tokens:    31,200

  By Project:
  ──────────────────────────────────────────────────
  myapp                $    0.3102  8 calls
  docs-bot             $    0.1719  4 calls

  By Model:
  ──────────────────────────────────────────────────
  gpt-4o               $    0.3102  8 calls
  claude-3-5-sonnet    $    0.1719  4 calls
Enter fullscreen mode Exit fullscreen mode

Manual logging (CLI)

If you prefer not to install as a library, you can log calls manually:

npm install -g @ozperium/aicost-tracker
aicost log myapp gpt-4o 1500 800 "generated README"
aicost summary
Enter fullscreen mode Exit fullscreen mode

Why local-first?

All data lives in ~/.aicost/usage.jsonl — one JSON line per call, greppable, portable, yours. No cloud account, no telemetry, no dashboard to maintain.

Pricing is baked in for OpenAI (gpt-4o, o1, gpt-4-turbo, gpt-3.5), Anthropic (claude-3.5-sonnet, claude-3-opus, haiku), Google (gemini-1.5), and local/Ollama (cost: $0).

GitHub: https://github.com/Ozperium/aicost-tracker


Also in the stack: AgentSpec for testing AI agent behavior, and quota for monitoring rate limits before they stop you.

Top comments (0)