npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

aitoken-cli

v1.2.0

Published

Track AI API usage and costs across OpenAI, Anthropic, Google, and more

Readme


The Problem

You're spending $200+/month on GPT-4, Claude, and other AI APIs.

But you have no idea where it's going.

The Solution

Track every API call locally. See exactly what you're spending.

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

# View stats
at stats
# Total: $34.56 (245,890 tokens)

Features

  • Automatic Tracking - Built-in wrappers, middleware, and SDK extensions (NEW in v1.1.0)
  • Multi-Provider Support - OpenAI, Anthropic, Google, Azure, Cohere
  • Automatic Cost Calculation - Up-to-date pricing for all major models
  • Beautiful Stats - See usage by provider, model, and time period
  • Local Storage - All data stored locally in SQLite (privacy-first)
  • Fast & Lightweight - CLI tool, no GUI overhead
  • JSON Export - Pipe data to other tools
  • Programmatic API - Use in your code without CLI calls

Installation

npm install -g aitoken-cli

CLI Usage

Add Usage

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

# With notes
at add -p anthropic -m claude-3.5-sonnet -i 2000 -o 1200 -n "Code review session"

# Options:
# -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

View Usage

# List recent usage (default: last 20)
at list

# Filter by provider
at list -p openai

# Limit results
at list -l 50

# Export as JSON
at list --json > usage.json

View Stats

# Overall stats
at stats

# Last 7 days
at stats -d 7

# Specific provider
at stats -p anthropic

# Export as JSON
at stats --json

Today's Usage

# Quick view of today
at today

# As JSON
at today --json

Supported Models

# List all models and pricing
at models

# Filter by provider
at models -p openai

Clear Data

# Clear all data (requires confirmation)
at clear --yes

# Clear specific provider
at clear -p openai --yes

# Clear before date
at clear --before 2026-01-01 --yes

Example Output

$ at stats

Overall Stats

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

By Provider

┌──────────┬──────────┬─────────┬──────────┬────────────┐
│ 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%       │
└──────────┴──────────┴─────────┴──────────┴────────────┘

Automatic Tracking (v1.1.0+)

The package includes built-in automatic tracking functions - no need for exec() or CLI calls in your code!

Import and Use Directly

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

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

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

const claudeResponse = await trackedClaude(anthropic, {
  model: 'claude-sonnet-4.5',
  max_tokens: 1024,
  messages: [{ role: 'user', content: 'Explain TypeScript' }]
  // 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 });

// Wrap your client once
const trackedOpenAI = createTrackedClient(openai, {
  provider: 'openai',
  model: 'gpt-4o'
});

// Use it exactly like normal - tracking happens automatically
const response = await trackedOpenAI.chat.completions.create({
  model: 'gpt-4o',
  messages: [{ role: 'user', content: 'Hello!' }]
});
// Automatically tracked with zero code changes! 
//  Method 3: SDK Extensions (Drop-in Replacement)
import { TrackedOpenAI, TrackedAnthropic } from 'aitoken-cli/extensions';

// Just 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';

// Calculate cost for a request
const cost = calculateCost('openai', 'gpt-4o', 1500, 800);
console.log(`Cost: $${cost.toFixed(4)}`); // $0.0195

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

// Get usage entries
const usage = getUsage({ provider: 'openai', limit: 10 });

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

Data Storage

All data is stored locally in ~/.token-tracker/usage.db (SQLite).

No data is sent anywhere. 100% local.

Supported Providers & Models (42 Models)

OpenAI (16 models)

  • GPT-5.2, GPT-5.2 Pro, GPT-5 Mini
  • GPT-4.1, GPT-4.1 Mini, GPT-4.1 Nano
  • o4-mini
  • GPT-4, GPT-4-32K, GPT-4 Turbo, GPT-4o, GPT-4o Mini
  • GPT-3.5 Turbo, GPT-3.5 Turbo 16K
  • o1-preview, o1-mini

Anthropic (14 models)

  • Claude Sonnet 4.5, Claude Haiku 4.5, Claude Opus 4.5
  • Claude 3.5 Sonnet, Claude 3.5 Haiku
  • Claude 3 Opus, Claude 3 Sonnet, Claude 3 Haiku
  • Claude 2.1, Claude 2.0, Claude Instant

Google (5 models)

  • Gemini 1.5 Pro, Gemini 1.5 Flash
  • Gemini 1.0 Pro, Gemini Pro, Gemini Pro Vision

Azure OpenAI (3 models)

  • GPT-4, GPT-4-32K, GPT-3.5 Turbo

Cohere (4 models)

  • Command, Command Light, Command R, Command R+

Development

# Clone repo
git clone https://github.com/brian-mwirigi/aitoken-cli.git
cd aitoken-cli

# Install dependencies
npm install

# Run in dev mode
npm run dev -- add -p openai -m gpt-4o -i 1000 -o 500

# Build
npm run build

# Test locally
npm link
at stats

Contributing

Contributions welcome! Please open an issue or PR.

License

MIT

Author

Built by Brian Mwirigi