Export chain
The Token Usage Dashboard only reads files. The data directory is filled by two Python scripts that query ccusage and rtk, check the output and write it atomically into the data directory. No step overwrites an existing file before the new output has been checked.
The chain runs on macOS, Linux and Windows and needs only the Python standard library.
The data suppliers of the export chain
The Token Usage Dashboard export chain gets its data from two tools:
ccusagereads the JSONL logs of Claude Code under~/.claudeand emits daily, project, session and block data as JSON.rtkis a proxy in front of developer tools. It filters command output and counts how many tokens therefore never reached a model.
The export chain scripts
The following table lists the scripts of the Token Usage Dashboard export chain and their purpose:
| File | Purpose |
|---|---|
ccusage-export.py | calls ccusage in three modes |
rtk-export.py | calls rtk gain; one run per day is enough |
exportlib.py | shared helpers: binary search, logs, status |
ccusage-check.py | checks a fresh export against the existing file |
ccusage-merge.py | layers an export onto an existing archive file |
rtk-merge.py | distributes the RTK export across monthly files and merges them |
export-status.py | writes the status files of a run |
The scripts live in the repository under export/. The installer copies them to <data>/bin, and the jobs call those copies. That way a work in progress in the repository cannot drag the nightly export down with it. If the copies differ from the repository, the installer reports it and leaves them alone; --force brings them up to date. A release package always replaces them.
If you change a script directly in <data>/bin, you lose the change with the next install.py --force. Always change the source and bring it over with the installer.
The modes of ccusage-export.py
The following table shows the modes of ccusage-export.py, what they do and when they run:
| Call | What happens | When |
|---|---|---|
daily | rewrites the current month and projects/; on the first three days of a month also the previous month | daily at 04:00 |
weekly | archives blocks/ and sessions/ from a rolling 14-day window | Sundays at 04:30 |
monthly | freezes the completed previous month | on the 1st at 05:00 |
rtk-export.py has no modes. It fetches the complete history and merges it into rtk/YYYY-MM.json. It creates the rtk/ directory only once rtk gain --format json actually returns JSON.
Why the weekly run is mandatory
Blocks and sessions in the Token Usage Dashboard come from the JSONL files under ~/.claude, whose history only reaches back about 31 days. If the weekly run fails for longer than a month, that period is irrecoverably lost. Daily and project data, by contrast, can be regenerated at any time.
The gap actually opens earlier: the weekly run only reaches back 14 days (CCUSAGE_LOOKBACK_DAYS). After a success at time T, the next run at T+7 covers T-7 to T+7, the one after at T+14 covers T to T+14. If that one fails too, the stretch T to T+7 is missing from T+21 on. Until the 31 days run out, it can only be closed by a run with a larger lookback. The dashboard names the exact call for that.
If a single run fails, the next one catches up thanks to the overlap. If you switch the machine off for longer, above all with cron, start the weekly run by hand afterwards.
Safety mechanisms of the export chain
The Token Usage Dashboard export chain protects the data directory with these mechanisms:
Write atomically. The export goes into a temporary file. Only after it passes the check does an atomic rename replace the target file. An aborted run cannot damage a good file.
Catch regressions. If a fresh export contains less than the existing file, that is a regression. It is judged differently depending on the run. The following table shows the rules:
| Rule | Meaning | Used for |
|---|---|---|
strict | a regression is an error, the file stays as it is | the current month, which can only grow |
merge | the export is layered onto the existing file instead of replacing it | the rolling weekly window |
freeze | the archive is more complete than what ccusage can still produce today; the file stays, the run counts as successful | completed previous months |
You can allow a regression deliberately: CCUSAGE_ALLOW_SHRINK=1.
Merge instead of replace. When merging, blocks are deduplicated by id, sessions by sessionId and RTK days by date; the later export wins. Existing totals are recomputed from the merged rows.
The scheduled jobs of the export chain
The Token Usage Dashboard installer sets up to four jobs, in local time. The following table shows the call and time of each job:
| Job | Call | Time |
|---|---|---|
ccusage-daily | ccusage-export.py daily | daily at 04:00 |
rtk-daily | rtk-export.py | daily at 04:15 |
ccusage-weekly | ccusage-export.py weekly | Sundays at 04:30 |
ccusage-monthly | ccusage-export.py monthly | on the 1st at 05:00 |
rtk-daily is created only when a working rtk was found. Every job calls the copy under <data>/bin with absolute paths: interpreter, script, --data-dir, and --ccusage-bin or --rtk-bin. A scheduler starts with its own sparse PATH; with the pinned paths, the job decides exactly as the installer did.
The following table shows, for each scheduler, where the jobs live, what they are called and how to check them:
| Scheduler | Where | Name | Check |
|---|---|---|---|
| launchd (macOS) | ~/Library/LaunchAgents/<prefix>.<job>.plist | prefix com.<user> | launchctl list | grep ccusage |
| systemd (Linux) | ~/.config/systemd/user/<prefix>-<job>.service and .timer | prefix token-usage-dashboard | systemctl --user list-timers |
| cron (Linux without systemd) | a marked block in the crontab | prefix token-usage-dashboard | crontab -l |
| Task Scheduler (Windows) | folder \<prefix>\ | prefix Token-Usage-Dashboard | schtasks /Query /TN "\Token-Usage-Dashboard\ccusage-daily" |
--label-prefix replaces the prefix. On Windows the jobs run with pythonw.exe, so no console window opens at night.
Missed runs. systemd catches up a start that was missed while the machine was off (Persistent=true), and so does the Windows Task Scheduler (StartWhenAvailable). launchd catches up after sleep, not after power-off. cron catches up nothing. systemd user timers and Windows tasks run only while you are logged in; on Linux loginctl enable-linger lifts that.
Running the installer again with jobs rewrites them. Lines outside the cron block stay.
Start an export job right away
This is how you start a scheduled Token Usage Dashboard export job right away through the scheduler:
launchctl kickstart -p "gui/$(id -u)/com.$(id -un).ccusage-daily" # macOS
systemctl --user start token-usage-dashboard-ccusage-daily.service # systemd
schtasks /Run /TN "\Token-Usage-Dashboard\ccusage-daily" # Windows
Or call the script directly:
DATA="$HOME/Library/Application Support/Claude-Code-Usage"
python3 "$DATA/bin/ccusage-export.py" weekly --data-dir "$DATA"
Options and environment variables of the export scripts
The following table lists the options and environment variables of the Token Usage Dashboard export scripts:
| Option | Variable | Script | Purpose |
|---|---|---|---|
--data-dir | CCUSAGE_DATA_DIR | ccusage-export.py | data directory |
--data-dir | RTK_DATA_DIR | rtk-export.py | data directory |
--ccusage-bin | CCUSAGE_BIN | ccusage-export.py | path to the ccusage binary |
--rtk-bin | RTK_BIN | rtk-export.py | path to the rtk binary |
--lookback-days | CCUSAGE_LOOKBACK_DAYS | ccusage-export.py | lookback of the weekly run, default 14 |
| – | CCUSAGE_SEARCH_DIRS | ccusage-export.py, installer | directories searched for ccusage before PATH |
| – | RTK_SEARCH_DIRS | rtk-export.py, installer | directories searched for rtk before PATH |
| – | CCUSAGE_ALLOW_SHRINK | ccusage-export.py | 1 allows a regression |
An option beats its variable, the variable beats the default. The options exist because cmd.exe, PowerShell and the Windows Task Scheduler cannot put a variable in front of a command. Separate the *_SEARCH_DIRS lists with a colon on macOS and Linux, a semicolon on Windows. A set but empty value means no fixed directories, only PATH.
Where the scripts look for the binaries
The export scripts look for ccusage and rtk in this order: option, variable, fixed directories, then PATH. A given path that is no longer executable (for example after an nvm upgrade) is passed over.
The following table lists the fixed directories the export scripts search on each system:
| System | ccusage | rtk |
|---|---|---|
| macOS | ~/.nvm/versions/node/*/bin (newest first), ~/.volta/bin, ~/.bun/bin, ~/.npm-global/bin, ~/.local/bin, /opt/homebrew/bin, /usr/local/bin | /opt/homebrew/bin, /usr/local/bin, ~/.cargo/bin, ~/.local/bin |
| Linux | as macOS up to ~/.local/bin, then /usr/local/bin, /home/linuxbrew/.linuxbrew/bin, /usr/bin | ~/.cargo/bin, ~/.local/bin, /usr/local/bin, /home/linuxbrew/.linuxbrew/bin |
| Windows | %APPDATA%\npm, %LOCALAPPDATA%\pnpm, %LOCALAPPDATA%\Volta\bin, ~\.bun\bin | ~\.cargo\bin |
On Windows the scripts try every name with the extensions from PATHEXT, because npm installs ccusage.cmd and cargo installs rtk.exe. If the search finds nothing, the run aborts with a message and a status file.
Logs of the export chain
The Token Usage Dashboard export chain writes one file per job under <data>/logs/: daily.log, weekly.log, monthly.log, rtk.log, each trimmed to the last 2000 lines. The messages are German. A line ABBRUCH (“aborted”) always means: the target file was left unchanged.
Besides these, the scheduler writes its own output: launchd-<job>.out and .err (macOS), systemd-<job>.out and .err (systemd) or cron-<job>.out (cron). The Windows jobs have no console; everything is in the run's own log.
Name collision for rtk
For the Token Usage Dashboard export chain, what is meant is the token proxy with the command rtk gain. There is a second tool of the same name, reachingforthejack/rtk (Rust Type Kit). If that one is on the path, the export appears to run through, and the mix-up only shows as an empty RTK tab. rtk-export.py therefore first checks whether any JSON comes back at all. Check beforehand yourself:
which rtk
rtk gain # must show a savings statistic, not a type error