Worktoken-monitor-nextjs-nestjs

Token Monitor

I built Token Monitor, a full-stack platform that tracks AI token usage and cost across every machine and AI coding tool I use.

Role
Full Stack Developer
Timeline
Apr 2026
Main stack
NestJS & Next.js
Token Monitor — NestJS & Next.js project screenshot

Overview

I built Token Monitor, a full-stack platform that tracks AI token usage and cost across every machine and AI coding tool I use. A Python agent on each machine reads the local logs of eight tools, an npm CLI installs and manages it, a NestJS API stores the records in PostgreSQL, and a Next.js dashboard shows usage, cost and device status live over Socket.IO.

Problem

AI coding tools each keep their own usage logs in a different place and format, on every machine. Nobody can see one total: how many tokens were used, what it cost, which tool or model used them, or which laptop is still reporting.

Approach

I built one pipeline. A small Python agent runs on each machine, reads the local logs of each tool, remembers where it stopped and sends only new records. The API stores them once, even if a batch is sent twice, and the dashboard updates live. An npm CLI makes setup a two-command job.

Outcome

AI tool sources
8
API endpoints
30
Database tables
5
Dashboard routes
5
CLI commands
9
Lines of code
~7.9k

What I built

  • Python agent that reads usage from Claude Code, Puku CLI, Command Code, Cursor, Codex, Antigravity and GitHub Copilot (IDE and CLI)
  • Log watching with watchdog plus a 30-second fallback poll, with file offsets stored in SQLite so nothing is read twice
  • Idempotent ingest: a unique index on device and request id means a retried batch never double counts
  • Cost estimated per model from a price table for Claude, GPT, Gemini and Cursor models, split into input, output, cache read and cache write
  • Agent config is Fernet-encrypted on disk, and each device authenticates with its own agent token
  • npm CLI (@mirzasaikatahmmed/token-monitor) with init, start, stop, restart, status, logs, config, doctor and uninstall
  • Runs as a background service: systemd on Linux, launchd on macOS, Task Scheduler on Windows
  • Live dashboard over Socket.IO: daily token chart, token mix donut, recent records and per-device online or offline status from heartbeats
  • JWT sign-in, an admin panel for users, plans and subscriptions, and device limits per plan
  • Manual import for usage the agent cannot read, and Docker setup for the backend

Architecture

System architecture

Diagram: a Python agent on each machine sends usage to a NestJS API with PostgreSQL, which feeds a Next.js dashboard; an npm CLI manages the agent.
Three parts and a CLI. The agent collects, the API stores and broadcasts, the dashboard shows it live. Open full size ↗

Supported sources

Diagram: eight AI tool sources feed one agent that outputs one record format.
Each tool stores usage differently. The agent turns all eight into one record shape before it leaves the machine. Open full size ↗

Data model

Diagram: five tables, user, device, usage, plan and subscription.
Five tables. A unique index on device and request id makes ingest safe to retry. Open full size ↗

Agent loop

Diagram: watch, offset, parse, batch and ingest steps, with a separate heartbeat.
The agent resumes from a stored offset, pushes only new records, and sends a heartbeat so the dashboard knows the device is alive. Open full size ↗

Engineering decisions

  1. 01Read local logs, not provider APIs

    Most tools expose no usage API, but all of them write logs. Reading those files works offline, needs no keys from the providers, and covers tools that have no billing page.

  2. 02Offsets in SQLite, dedupe in Postgres

    The agent stores how far it has read each file, so a restart does not re-send history. The database also enforces a unique device and request id, so a retry can never double count.

  3. 03Costs are estimates, labelled as such

    Prices come from a model rate table and are meant for monitoring, not billing. Tools that report their own cost, like Puku CLI, use that value instead.

  4. 04Config encrypted on the machine

    The agent token is never stored as plain text. The config is Fernet-encrypted in the system data folder, and an old plain config is migrated on first run.

  5. 05A CLI instead of a manual install

    The npm package bundles the agent, so init sets up config, Python dependencies and autostart. doctor and status make a broken install easy to diagnose.

Need something similar?

Tell me what you are building and I will reply within one working day.

Start a project
↑↓ navigate↵ openesc closeCtrl K toggles this menu