Skip to content

brian-mwirigi/aitoken-cli

Repository files navigation

aitoken-cli

Track every dollar you spend on AI APIs

npm version  npm downloads  license  TypeScript  Node.js

Local-first • Privacy-focused • Multi-provider • 42 models supported

DocumentationnpmIssues


Why aitoken-cli?

You're spending $200+/month on GPT-4, Claude, and Gemini. But you have no idea where it's going.

$ at stats

┌─────────────────┬──────────┐
│ Metric          │ Value    │
├─────────────────┼──────────┤
│ Total Requests  │ 127      │
│ Total Tokens    │ 3.2M     │
│ Total Cost      │ $24.5670 │
└─────────────────┴──────────┘

┌──────────┬──────────┬─────────┬──────────┬────────────┐
│ Provider │ Requests │ Tokens  │ Cost     │ % of Total │
├──────────┼──────────┼─────────┼──────────┼────────────┤
│ openai   │ 85       │ 2.1M    │ $18.2340 │ 74.2%      │
│ anthropic│ 32       │ 890K    │ $5.1230  │ 20.9%      │
│ google   │ 10       │ 210K    │ $1.2100  │ 4.9%       │
└──────────┴──────────┴─────────┴──────────┴────────────┘

Table of Contents


Quick Start

npm install -g aitoken-cli

# Track usage
at add -p openai -m gpt-4o -i 1500 -o 800

# View stats
at stats

Features

Feature Description
Automatic Tracking Built-in wrappers, middleware, and SDK extensions
Multi-Provider OpenAI, Anthropic, Google, Azure, Cohere
Automatic Costs Up-to-date pricing for all 42 models
Beautiful Stats Usage by provider, model, and time period
Local Storage All data in SQLite — nothing leaves your machine
JSON Export Pipe data to other tools
Programmatic API Use in your code without CLI calls

CLI Commands

at add — Track usage

at add -p openai -m gpt-4o -i 1500 -o 800
at add -p anthropic -m claude-3.5-sonnet -i 2000 -o 1200 -n "Code review session"
Flag Description
-p, --provider Provider (openai, anthropic, google, azure, cohere)
-m, --model Model name
-i, --input Input/prompt tokens
-o, --output Output/completion tokens
-n, --notes Optional notes

at list — View usage

at list              # Last 20 entries
at list -p openai    # Filter by provider
at list --json       # Export as JSON

at stats — View statistics

at stats             # Overall stats
at stats -d 7        # Last 7 days
at stats -p anthropic # Specific provider

at today — Today's snapshot

at today
at today --json

at models — Supported models & pricing

at models            # All models
at models -p openai  # Filter by provider

at clear — Clear data

at clear --yes              # Clear all
at clear -p openai --yes    # Clear by provider
at clear --before 2026-01-01 --yes  # Clear before date

Automatic Tracking (v1.1.0+)

Three integration methods — pick the one that fits your workflow:

Method 1: Wrapper Functions (Easiest)
import { trackedGPT, trackedClaude } from 'aitoken-cli/wrappers';
import OpenAI from 'openai';

const openai = new OpenAI({ apiKey: process.env.OPENAI_API_KEY });

const response = await trackedGPT(openai, {
  model: 'gpt-4o',
  messages: [{ role: 'user', content: 'Hello!' }]
  // Automatically tracked!
});
Method 2: Middleware Pattern (Zero Code Changes)
import { createTrackedClient } from 'aitoken-cli/middleware';
import OpenAI from 'openai';

const openai = new OpenAI({ apiKey: process.env.OPENAI_API_KEY });
const trackedOpenAI = createTrackedClient(openai, {
  provider: 'openai',
  model: 'gpt-4o'
});

// Use exactly like normal — tracking is automatic
const response = await trackedOpenAI.chat.completions.create({
  model: 'gpt-4o',
  messages: [{ role: 'user', content: 'Hello!' }]
});
Method 3: SDK Extensions (Drop-in Replacement)
import { TrackedOpenAI } from 'aitoken-cli/extensions';

// Change the import — everything else stays the same
const openai = new TrackedOpenAI({
  apiKey: process.env.OPENAI_API_KEY
});

const response = await openai.chat.completions.create({
  model: 'gpt-4o',
  messages: [{ role: 'user', content: 'Hello!' }]
});
// Automatically tracked!

See EXAMPLES.md for 12 complete examples including Express.js, Next.js, and chatbot integrations.


Programmatic API

import { addUsage, getUsage, getStats, calculateCost } from 'aitoken-cli';

const cost = calculateCost('openai', 'gpt-4o', 1500, 800);
// $0.0195

addUsage({
  provider: 'openai',
  model: 'gpt-4o',
  promptTokens: 1500,
  completionTokens: 800,
  totalTokens: 2300,
  cost,
  timestamp: new Date().toISOString(),
  notes: 'My API call',
});

const stats = getStats({ provider: 'openai' });
console.log(`Total: $${stats.totalCost.toFixed(2)}`);

Supported Providers & Models (42 Models)

Provider Models Highlights
OpenAI 16 GPT-5.2, GPT-4.1, GPT-4o, o4-mini, o1
Anthropic 14 Claude Sonnet 4.5, Claude Opus 4.5, Claude 3.5
Google 5 Gemini 1.5 Pro, Gemini 1.5 Flash
Azure OpenAI 3 GPT-4, GPT-4-32K, GPT-3.5 Turbo
Cohere 4 Command R+, Command R, Command

Architecture

┌─────────────────────────────────────────┐
│              aitoken-cli                │
├───────────────┬─────────────────────────┤
│  CLI Layer    │  Programmatic API       │
│  (Commander)  │  (addUsage, getStats)   │
├───────────────┴─────────────────────────┤
│           Cost Calculator               │
│     (42 models, auto-updated pricing)   │
├─────────────────────────────────────────┤
│           SQLite Database               │
│        ~/.token-tracker/usage.db        │
└─────────────────────────────────────────┘

All data stays on your machine. Nothing is sent anywhere.


Development

git clone https://github.com/brian-mwirigi/aitoken-cli.git
cd aitoken-cli
npm install
npm run build
npm link
at stats

Contributing

Contributions welcome! Please open an issue or submit a PR.

License

MIT


Built by Brian Munene Mwirigi

npmDocsIssues

About

Track AI API token usage and costs across OpenAI, Anthropic, Google and more. Local-first CLI.

Topics

Resources

License

Stars

Watchers

Forks

Packages

 
 
 

Contributors