codex-token-tracker 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 Codex Token Tracker contributors
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,179 @@
1
+ # codex-token-tracker
2
+
3
+ Menu bar / system tray app **and** headless agent that tracks your [OpenAI Codex](https://github.com/openai/codex) usage — tokens, cache hit rate, model mix, tokens per second of the running session, subscription rate limits and the **API-equivalent cost in dollars** — and syncs it to your team's Codex Tracker dashboard (Next.js + Clerk + Convex).
4
+
5
+ - macOS menu bar app and native Windows tray app (Electron)
6
+ - `codex-tracker agent` headless mode for **WSL2**, Linux and servers
7
+ - Reads Codex CLI / Codex Desktop rollout logs (`~/.codex/sessions`) locally — no API keys, no proxies
8
+ - Real-time: today's totals, current session tokens/sec, context window use, weekly / 5-hour rate limits
9
+ - Activity heatmap merged from **local data + your other devices** (realtime database)
10
+ - English / 中文, follows your OS language and can be switched and persisted
11
+ - Light / dark follows the system theme
12
+
13
+ > Screenshots: `docs/` (coming with the first release).
14
+
15
+ ## Install
16
+
17
+ ```bash
18
+ npm install -g codex-token-tracker
19
+ # or run without installing
20
+ npx codex-token-tracker
21
+ ```
22
+
23
+ Node.js 20+ is required. Electron is an *optional* dependency: if its binary cannot be downloaded (locked-down servers, WSL without a desktop), the package still installs and runs in agent mode.
24
+
25
+ ## Quick start
26
+
27
+ ```bash
28
+ codex-tracker login # connect this device to the dashboard (Google / GitHub via Clerk)
29
+ codex-tracker # start the menu bar app (agent mode when there is no display)
30
+ codex-tracker status # terminal summary — handy in WSL / over SSH
31
+ ```
32
+
33
+ By default the tool connects to **https://codex.chenli.dev**; other teams pass their own dashboard with `--dashboard <url>` (remembered afterwards). `login` prints a short code and opens `https://<dashboard>/cli-auth?code=XXXX-XXXX` in your browser. Approve the device there and the tracker receives a device token. Local tracking works without signing in; signing in enables uploads and the multi-device heatmap.
34
+
35
+ ### Self-hosted dashboard
36
+
37
+ ```bash
38
+ codex-tracker login --dashboard https://tracker.your-company.com
39
+ # or
40
+ codex-tracker config set dashboardUrl https://tracker.your-company.com
41
+ ```
42
+
43
+ The tracker discovers the Convex deployment through `<dashboard>/api/config`.
44
+
45
+ ## Commands
46
+
47
+ | Command | What it does |
48
+ | --- | --- |
49
+ | `codex-tracker` | Menu bar app; falls back to `agent` when no display / no Electron |
50
+ | `codex-tracker menubar [--background]` | Start the tray app (detached with `--background`) |
51
+ | `codex-tracker agent [--interval <sec>] [--once]` | Headless tracking + uploads; `--once` runs one cycle and exits |
52
+ | `codex-tracker login [--dashboard <url>]` | Device-code login |
53
+ | `codex-tracker logout` | Forget the device token (local data stays) |
54
+ | `codex-tracker status [--json]` | Today / 7d / 30d usage, live session, rate limits, models |
55
+ | `codex-tracker paths` | Detected Codex session directories |
56
+ | `codex-tracker config get [key]` / `config set <key> <value>` | Settings (see below) |
57
+ | `codex-tracker lang <en\|zh\|auto>` | Display language |
58
+ | `codex-tracker --version` / `--help` | |
59
+
60
+ ## Menu bar app
61
+
62
+ - Tray title shows today's tokens (`12.4k`) — `config set trayTitle tokens|cost|none`
63
+ - Click: popover with Today, Live session (tokens/s, context window, rate limits), Activity heatmap, Models, Account
64
+ - Right-click: Open dashboard, Sign in/out, Language, Launch at login, Refresh, Quit
65
+ - Launch at login uses a LaunchAgent on macOS and the registry run key on Windows
66
+
67
+ ## Windows and WSL2
68
+
69
+ **Native Windows**: `npm i -g codex-token-tracker` in PowerShell, then `codex-tracker`. The tray app also scans every WSL distro (`\\wsl$\<distro>\home\*\.codex\sessions`) so sessions run inside WSL are counted.
70
+
71
+ **Inside WSL2**: Electron cannot show a Windows tray from WSL, so run the agent:
72
+
73
+ ```bash
74
+ codex-tracker login # prints the URL + code; open it in your Windows browser
75
+ codex-tracker agent # keep running (tmux / nohup / systemd --user)
76
+ ```
77
+
78
+ The agent also scans `/mnt/c/Users/*/.codex/sessions`, so one agent covers both sides. WSLg can display the Electron window but tray support is limited; the Windows tray app is the recommended UI.
79
+
80
+ Example `systemd --user` unit (`~/.config/systemd/user/codex-tracker.service`):
81
+
82
+ ```ini
83
+ [Service]
84
+ ExecStart=%h/.npm-global/bin/codex-tracker agent
85
+ Restart=always
86
+ [Install]
87
+ WantedBy=default.target
88
+ ```
89
+
90
+ ## Configuration
91
+
92
+ Config lives in `~/.codex-tracker/` (override with `CODEX_TRACKER_HOME`):
93
+
94
+ | Key | Default | Notes |
95
+ | --- | --- | --- |
96
+ | `dashboardUrl` | `https://codex.chenli.dev` | Your team dashboard (self-hosters: `codex-tracker login --dashboard <url>`) |
97
+ | `language` | `auto` | `en`, `zh` or `auto` (OS language) |
98
+ | `uploadIntervalSec` | `60` | Push interval |
99
+ | `heartbeatIntervalSec` | `15` | Live status interval |
100
+ | `extraSessionDirs` | `[]` | Extra session folders (comma-separated in `config set`) |
101
+ | `launchAtLogin` | `false` | macOS / Windows |
102
+ | `trayTitle` | `tokens` | `tokens`, `cost` or `none` |
103
+
104
+ `CODEX_HOME` is honoured when locating `sessions/` and `archived_sessions/`.
105
+
106
+ ### Pricing overrides
107
+
108
+ Costs are "API-equivalent" — standard OpenAI list prices per 1M tokens (input, cached input, output; reasoning tokens are billed as output). Models missing from the built-in table are priced by family and marked **est.** Override or add prices in `~/.codex-tracker/pricing.json`:
109
+
110
+ ```json
111
+ {
112
+ "gpt-5.6-sol": { "input": 1.75, "cachedInput": 0.175, "output": 14 }
113
+ }
114
+ ```
115
+
116
+ ## Sources
117
+
118
+ The tracker reads the local transcripts of every agent that can use a Codex subscription (ChatGPT login) and
119
+ attributes usage to an **agent** (shown as "Sources" chips in the popover, a `Sources` line in `codex-tracker status`,
120
+ and as a tag on live sessions / model rows). Only Codex-subscription providers are counted unless
121
+ `trackAllProviders` is `true`.
122
+
123
+ | Source | Where it looks | Notes |
124
+ |---|---|---|
125
+ | `codex` – Codex CLI / Codex Desktop | `$CODEX_HOME` or `~/.codex/sessions/YYYY/MM/DD/rollout-*.jsonl`, `archived_sessions/` | Reference format; also carries rate-limit snapshots and context-window size |
126
+ | `pi` – [pi coding agent](https://github.com/badlogic/pi-mono) | `$PI_CODING_AGENT_DIR` or `~/.pi/agent/sessions/<project>/*.jsonl` | Counts messages whose `provider` is `openai-codex` (Codex OAuth); other providers (API keys) only with `trackAllProviders` |
127
+ | `opencode` – [OpenCode](https://github.com/sst/opencode) | `$XDG_DATA_HOME/opencode` or `~/.local/share/opencode/storage/` (Windows: `%LOCALAPPDATA%\opencode`, `%APPDATA%\opencode`) | One JSON file per message; `providerID` `openai` counts when `auth.json` shows an OAuth login. *Best-effort – format inferred, not verified on a real install* |
128
+ | `cline`, `roo`, `kilo` – Cline / Roo Code / Kilo Code (VS Code, Cursor, Windsurf, VSCodium, Trae, VS Code Remote) | `<globalStorage>/<extension id>/tasks/<task>/ui_messages.json` + `task_metadata.json` | Per-request `api_req_started` entries; model/provider from `model_usage`. *Best-effort* |
129
+ | `hermes` – Hermes agent | `$HERMES_HOME` or `~/.hermes/sessions/**/*.json\|jsonl` (+ `state.db` via `node:sqlite` when available) | Generic parser: any JSON/JSONL with per-request `usage` objects. *Best-effort* |
130
+ | custom | `extraSessionDirs` | `{"path": "~/.myagent/logs", "agent": "myagent", "format": "generic"}` (formats: `codex`, `pi`, `generic`, `opencode`, `cline`) |
131
+
132
+ All sources are on by default. Turn one off with `codex-tracker config set sources.opencode false` (or
133
+ `config set sources '{"pi":false}'`); `codex-tracker paths` shows which roots were found. On Windows the WSL
134
+ distros' homes are scanned too, and inside WSL the Windows user profiles under `/mnt/c/Users`.
135
+
136
+ The opencode / cline / hermes readers were written from the projects' known on-disk layouts without a real
137
+ install to test against. If your usage is missing, please open an issue with one anonymised sample file
138
+ (`ui_messages.json`, a message JSON, or a session JSONL) and the output of `codex-tracker paths`.
139
+
140
+ ## Rate limits
141
+
142
+ The popover and `codex-tracker status` show your account's rate-limit windows (e.g. weekly / 5-hour) **live** from
143
+ `https://chatgpt.com/backend-api/wham/usage`, the same endpoint the official Codex client uses. The request is
144
+ made with the access token from your local Codex login (`~/.codex/auth.json`); the token is read fresh each time,
145
+ never written, never refreshed by the tracker, and is sent **only to chatgpt.com** – never to the dashboard.
146
+ Because every Codex-subscription consumer (pi, OpenCode, …) draws from the same account, this is the only
147
+ accurate number; the values inside Codex logs are just snapshots from Codex's own last request.
148
+
149
+ - Refreshed every `usageRefreshSec` (default 60 s) and ~10 s after new local usage is seen.
150
+ - If the request fails (offline, expired token, API-key login) the card falls back to the latest values from Codex
151
+ logs and is labelled *From logs · as of <time>* with the reason.
152
+ - Disable with `codex-tracker config set liveRateLimits false`.
153
+
154
+ ## What gets uploaded
155
+
156
+ Only aggregates: token counts per UTC hour and model, per-session totals, the model name, the project **folder name** and a SHA-256 of its path, plus a heartbeat (tokens/s, today's totals). Prompts, code, file paths and session contents never leave your machine. Timestamps are stored in UTC; the app and dashboard display them in your local time zone.
157
+
158
+ ## Development
159
+
160
+ ```bash
161
+ pnpm install # from the repo root
162
+ pnpm --filter codex-token-tracker build
163
+ node packages/menubar/bin/codex-tracker.js status
164
+ CODEX_TRACKER_DEBUG=1 node packages/menubar/bin/codex-tracker.js menubar # verbose logs
165
+ CODEX_TRACKER_DEVTOOLS=1 ... # open DevTools
166
+ pnpm --filter codex-token-tracker build:icons # regenerate tray icons
167
+ ```
168
+
169
+ Publishing (from the repo root): `pnpm release:menubar` — `prepublishOnly` rebuilds and typechecks first.
170
+
171
+ ## License
172
+
173
+ MIT
174
+
175
+ ## Troubleshooting
176
+
177
+ - **"Electron is not installed" / tray app does not start** – the Electron binary is downloaded by an install script. If your package manager skipped or cached it incompletely, run `npm rebuild electron` (or `node "$(npm root -g)/codex-token-tracker/node_modules/electron/install.js"`). With pnpm, allow the build script (`pnpm approve-builds`). The headless `codex-tracker agent` and `codex-tracker status` work without Electron.
178
+ - **Nothing is tracked** – run `codex-tracker paths`; Codex must have written rollouts under `~/.codex/sessions` (or set `CODEX_HOME`). Add other locations with `codex-tracker config set extraSessionDirs '["/path/to/sessions"]'`.
179
+ - **Uploads fail with BAD_TOKEN** – the device was revoked in the dashboard; run `codex-tracker login` again.
Binary file
Binary file
Binary file
Binary file
Binary file
@@ -0,0 +1,3 @@
1
+ #!/usr/bin/env node
2
+ "use strict";
3
+ require("../dist/cli.js");