Skip to main content

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:

  • ccusage reads the JSONL logs of Claude Code under ~/.claude and emits daily, project, session and block data as JSON.
  • rtk is 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:

FilePurpose
ccusage-export.pycalls ccusage in three modes
rtk-export.pycalls rtk gain; one run per day is enough
exportlib.pyshared helpers: binary search, logs, status
ccusage-check.pychecks a fresh export against the existing file
ccusage-merge.pylayers an export onto an existing archive file
rtk-merge.pydistributes the RTK export across monthly files and merges them
export-status.pywrites 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.

Do not edit the copy

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:

CallWhat happensWhen
dailyrewrites the current month and projects/; on the first three days of a month also the previous monthdaily at 04:00
weeklyarchives blocks/ and sessions/ from a rolling 14-day windowSundays at 04:30
monthlyfreezes the completed previous monthon 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:

RuleMeaningUsed for
stricta regression is an error, the file stays as it isthe current month, which can only grow
mergethe export is layered onto the existing file instead of replacing itthe rolling weekly window
freezethe archive is more complete than what ccusage can still produce today; the file stays, the run counts as successfulcompleted 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:

JobCallTime
ccusage-dailyccusage-export.py dailydaily at 04:00
rtk-dailyrtk-export.pydaily at 04:15
ccusage-weeklyccusage-export.py weeklySundays at 04:30
ccusage-monthlyccusage-export.py monthlyon 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:

SchedulerWhereNameCheck
launchd (macOS)~/Library/LaunchAgents/<prefix>.<job>.plistprefix com.<user>launchctl list | grep ccusage
systemd (Linux)~/.config/systemd/user/<prefix>-<job>.service and .timerprefix token-usage-dashboardsystemctl --user list-timers
cron (Linux without systemd)a marked block in the crontabprefix token-usage-dashboardcrontab -l
Task Scheduler (Windows)folder \<prefix>\prefix Token-Usage-Dashboardschtasks /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:

OptionVariableScriptPurpose
--data-dirCCUSAGE_DATA_DIRccusage-export.pydata directory
--data-dirRTK_DATA_DIRrtk-export.pydata directory
--ccusage-binCCUSAGE_BINccusage-export.pypath to the ccusage binary
--rtk-binRTK_BINrtk-export.pypath to the rtk binary
--lookback-daysCCUSAGE_LOOKBACK_DAYSccusage-export.pylookback of the weekly run, default 14
–CCUSAGE_SEARCH_DIRSccusage-export.py, installerdirectories searched for ccusage before PATH
–RTK_SEARCH_DIRSrtk-export.py, installerdirectories searched for rtk before PATH
–CCUSAGE_ALLOW_SHRINKccusage-export.py1 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:

Systemccusagertk
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
Linuxas 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