# Claude usage How much of your Claude limits your agents are using, and when they'll run out. A popup shows the account's 5-hour and weekly use as colored bars with their reset times and a forecast ("At this pace, runs out at 5:24 pm"), today's estimated cost across every Claude Code session on the computer next to a 7-day bar chart, and each Claude Code agent in Herdr with its session's tokens and cost. The agent spending fastest right now is highlighted with its rate and an Interrupt button that stops its turn; clicking any agent goes to it. The same numbers fit in a narrow panel beside your terminals, and in a drawer if you put the mod in a layout's floating block. Toasts warn at 80% and 95% of a limit, and when a limit will run out within 30 minutes. ![Claude usage](preview.png) ## What you see - **Popup**, 720 by 760px at most, 40px from the top of the window, on `popover`, 16px padding, 12px between parts: - A header: a 16px gauge icon in `accent`, "Claude usage" in 14px semibold, the plan ("Max 5x", "Pro", "Demo") as an 11px `muted` pill on `raised` with 5px corners, and "updated just now" / "updated 2m ago" in 11px `dim`. - One 20px row per limit: the name in 12.5px `muted` in a 104px column ("5-hour", "Weekly", and "Fable weekly" or any other per-model weekly limit), an 8px bar on `border` filled to the percentage, the percentage in 13px semibold in a 44px right-aligned column, and "resets in 1h 34m" / "resets in 4d 6h" in 11.5px `dim`. Bar and percentage are `ansi2` (green) below 80%, `ansi3` (amber) from 80%, `ansi1` (red) from 95%. - Under each bar, indented 116px to line up with it, the forecast in 11px: - "At this pace, runs out at 5:24 pm (in 28m) · 30%/h" in `ansi3`, or `ansi1` when that is within 30 minutes. A time on another day reads "Sat 3:40 pm". - "On pace to reset first · 0.4%/h" in `dim` when the limit would last past its reset. - "Not rising over the last 30 min" (5-hour) / "… last 3 h" (weekly) in `dim`. - "Forecast after 12 more min of samples" in `dim` until there is enough history. - "At the limit until it resets" in `ansi1` at 100%. - A 1px `border` rule, then two columns, 24px apart: - Left: "TODAY, EVERY CLAUDE CODE SESSION" in 10.5px semibold `dim` caps; today's estimated cost in 26px semibold, with "7 sessions · 1.2K replies" in 11.5px `dim` beside it; a wrapping line of "in 7.7K out 2.8M cache read 177M cache write 5.9M" (labels `dim`, numbers `muted`); then up to 4 models, most expensive first, each a name in an 84px column, a 4px `accent` bar relative to the top model, and its cost right-aligned. - Right, 250px: "LAST 7 DAYS" with the week's total on the right, then seven bars, oldest first, 5px apart, each in a 74px-tall slot: the day's cost above it in 10px (`$45`, `$1.2K`, `$0.90`, `–` for none), the bar's height relative to the most expensive day with 3px corners, and the weekday below in 10.5px. Today's bar is `accent` with `foreground` labels; earlier days are `accent` at 35% with `dim` / `muted` labels. - A rule, then "AGENTS IN HERDR (5)" with the sum of their sessions' costs on the right. - One row per Claude Code agent, in Herdr's order, scrolling past 380px: the status (below), the agent's program icon (Claude's, 15px), its title in 13px `foreground` and "space / tab" in 11px `dim`; underneath in 11px `dim`, "Opus 5.5 · in 2.6K · out 899K · cache 56M read, 1.9M write"; the session's cost in 13px medium on the right. The focused agent's row is `selected`, a hovered row is `hover`, 7px corners. - The **top spender**, the agent whose session cost the most in the last 15 minutes (at least $0.01), has an `ansi3` tint (8% fill, 1px border at 35%) and, under its cost, "top spender · $0.42/min" in 11px medium `ansi3` and a 20px **Interrupt** button: 11px `muted` text, 1px `input` border, 5px corners. Pressed once it turns into "Click again to stop", semibold `background` text on `ansi1`; while stopping it reads "Stopping…" in `dim`. - A footnote in 10.5px `dim`: costs are estimates at API list prices. - **Panel** (a pane or a layout block), on `surface`, 12px padding, 10px between parts, the whole of it one vertical scroll: the same header; each limit as a line (name, reset time, percentage) over a 6px bar, with its forecast line under it in 11px; TODAY with the cost in 20px and up to 3 models; LAST 7 DAYS with 36px bars; AGENTS with the same rows, top spender and Interrupt included; the footnote. - **Drawer** (a layout `floating` block), 232px wide, no background of its own: one row per limit with the percentage in 14px semibold on the window-edge side of a 36px column and "5-hour · 1h 33m" over a 4px bar beside it; a "$" row with today's cost; then the four most expensive agents with their status and cost. On the right edge every row is mirrored so the numbers face the edge, which is what a `peek` drawer shows while closed. - Agent status: working is `mods/workspace-chrome/spinner.svg` spinning in `working`; blocked an 8px `blocked` dot; done an 8px `done` dot; idle an 8px ring in `idle`. - Sub-lines that say "≈" in `ansi3` mark a session guessed from the agent's folder (below). A remote agent says "on another machine: no local transcript"; an agent whose session isn't found yet says "session not found yet". - Without account limits the bars give way to one `dim` line: not signed in ("claude /login"), sign-in expired ("renews on Claude Code's next request"), or couldn't read them (with the HTTP status). ## How it behaves - `ctrl-b u` opens the popup (`mod.open`); Escape or a click outside closes it. `ctrl-b y` splits the panel to the right of the focused pane. - Clicking an agent row runs `agent.focus {terminal}`: Herdr shows that agent's space, tab and pane. From the popup it closes the popup first. Demo and remote rows don't respond. - **Interrupt** (top spender only): the first click arms it for 3 seconds ("Click again to stop") and changes nothing; it disarms by itself. A second click within those 3 seconds runs `hc interrupt
`, which presses Claude Code's stop key (Escape) and watches whether the turn stopped. If the agent still reports working, a second Escape is pressed: with Claude Code's vim mode in insert mode, the first one only switches to normal mode. A toast says "Stopped " or "Couldn't confirm <title> stopped". The click doesn't also focus the row. The agent keeps running; only its current turn stops. - **Forecast**: each fetch of the limits adds a (time, percent) sample, kept 3 hours in the shared state. The rate is the least-squares slope over the last 30 minutes for the 5-hour limit (at least 6 samples spanning 15 minutes) and the last 3 hours for weekly limits (6 samples over 45 minutes). With less, it says how many more minutes it needs. A limit whose reset time moves, or whose percentage falls by 10 points or more, starts its samples over; smaller dips count toward the slope. - When a forecast says a limit runs out within 30 minutes, before its reset, a toast says "Claude 5-hour limit runs out around 5:24 pm" (warning, 12 seconds), once per reset period. - Agent status follows Herdr live (working, blocked, done, idle), and the rows redraw the moment it changes; the numbers come from the last scan. - Numbers refresh every 20 seconds: a listing of the transcript files, then only the bytes written since the last look. The account limits refresh every 2 minutes, or every 5 minutes after an error (the endpoint answers HTTP 429 when asked too often) or with an expired sign-in. - When a limit passes 80%, and again at 95%, a toast says "Claude 5-hour limit at 86%" and when it resets (warning, then error level, 10 seconds). Each fires once per reset period, whichever copy is open, and not again after a restart. - Limit bars slide to a new width and day bars to a new height over 450ms (`transition`). Nothing else animates and there is no redraw timer; the only timer besides the 20-second collection is the 3-second one-shot that disarms Interrupt. ## Built from - One mod, `mods/claude-usage/`. `mod.json`: `place: "none"` (nothing docks into your layout) and the popup overlay (`kind: popup`, `at: top`, `offset: 40`, 720 by 760). - `main.js` picks the view: a non-null `placement()` is the drawer; `props { view: "panel" }` the panel; otherwise (opened by `mod.open`) the popup, closing itself with `props().overlay`. - `data.js` collects, `forecast.js` keeps the samples and works out the forecast, `view.js` draws, `pricing.js` holds the price table, `scan.sh` and `scan.awk` read transcripts. - **One collector.** Copies of a mod share no module state, so every copy runs a `Collector`, but only the one holding the lease in shared `"claude-usage"` (`owner`, `beat`) works; the others draw from that shared value. A copy takes over a lease whose holder hasn't written for 100 seconds, and a closing copy (`close()`) hands it back at once, so a popup opened alone collects straight away. The collector writes only when something changed, or every 45 seconds to keep the lease, because each `setShared` redraws every mod in the window. - **Agent to session.** For each agent in `agents()` with `app === "claude-code"` (or `program.key === "claude"`) and a local address, its terminal in `terminals()` gives `foreground.pid`, the running Claude Code. Claude Code writes `~/.claude/sessions/<pid>.json` with its `sessionId`, read with `readTextFile`. While Claude Code runs a tool the foreground can be the tool, so the last answer is kept. With no sessions file, the newest `*.jsonl` in the agent's project folder is used (`ls -t`; the folder is the cwd with every non-alphanumeric character as `-`) and marked "≈". - **Transcripts.** `scan.sh list <projects> <minutes> <session ids…>` runs `find … -name '*.jsonl' ( -mmin -<since midnight 6 days ago> -o -path */<id>.jsonl -o -path */<id>/subagents/* ) -exec wc -c {} +`. `scan.sh read scan.awk <path> <offset> <size> …` pipes `tail -c +offset+1 | head -c size-offset` into `LC_ALL=C awk`, which prints `id model timestamp input output cache_read cache_write_5m cache_write_1h speed` per assistant line that has a usage object, and `#used <bytes>` counting only whole lines, so a line still being written is read next time. The collector keeps per file: the offset, the last message id, the last model, totals per local day and model, and `[time, cost]` of each reply in the last 15 minutes. Up to 150 MB or 80 files go in one `exec`. - **Cache.** That per-file state is saved to `claude-usage.cache.json` in the Herdr home (`{v: 1, root, files: {path: {o, i, m, d, r}}}`, about 85 KB for 215 files) at most every 2 minutes, and by a copy that closes while collecting. It's read back with `exec("cat")` (no 1 MiB limit) and written with `writeTextFile` against the text last read; if another copy wrote in between, it rereads and writes again. A cache for another Claude folder is ignored. Demo mode uses no cache. - A session's usage is its `<id>.jsonl` plus every `<id>/subagents/*.jsonl`. Today is every file written since local midnight, counting lines whose timestamp falls today; the 7 days are the same for each of the last seven local dates. - **Top spender**: for each agent's session, the sum of its files' replies in the last 15 minutes; the largest, if at least $0.01, is shared as `top: {address, perMin}`, its sum divided by 15. - **Limits.** The sign-in is Claude Code's: on macOS `security find-generic-password -s "Claude Code-credentials" -w`, else `<claude dir>/.credentials.json`, both JSON with `claudeAiOauth.{accessToken, expiresAt, subscriptionType, rateLimitTier}`. `fetch("https://api.anthropic.com/api/oauth/usage", {headers: {Authorization: "Bearer …", "anthropic-beta": "oauth-2025-04-20"}})` returns `five_hour` and `seven_day` (`utilization`, `resets_at`), optional `seven_day_opus` / `seven_day_sonnet`, and `limits[]`, whose `weekly_scoped` entries name a model (`scope.model.display_name`, e.g. "Fable"). The plan label comes from `rateLimitTier` (`…max_5x` → "Max 5x") or `subscriptionType`. - **Costs** (`pricing.js`, dated `AS_OF`): dollars per million tokens per model prefix, longest match wins: input, output and cache read; cache writes are 1.25x input (5-minute) and 2x input (1-hour); `speed: "fast"` doubles it; Haiku 5.5 prompts over 100K use its second rate card. Each reply is priced on its own, so a session that switches models is priced right. An unknown model counts tokens and shows its cost with a "+". - Config, optional, `claude-usage.json` in the Herdr home: `{ "claude_dir": "~/.claude" }` (default `$CLAUDE_CONFIG_DIR`, else `~/.claude`), or `{ "demo": "<folder>" }` to show made-up data from `demo/make-demo.py`: agents.json, quota.json (with `demo_samples`, earlier fetches as `[minutes ago, percent]`, which the real response doesn't have) and transcripts over the last 7 days, two agents with replies in the last 15 minutes. - Herdr functions: `props`, `placement`, `agents`, `terminals`, `theme`, `shared`, `setShared`, `notify`, `run`, `closeOverlay`, `exec`, `hc`, `readTextFile`, `writeTextFile`, `home`, `appHome`, `window`, and `fetch`. Commands: `mod.open`, `pane.split` (with `mod`/`props`), `agent.focus`. Program: `hc interrupt <address> --json --timeout 4`, which prints `{"key": "esc", "outcome": "stopped" | "still_working"}`. - Icons: Lucide's `gauge` and `sparkle` (ISC), tinted with `svg()`; the agent's own program icon from Herdr; the shipped `workspace-chrome` spinner. ## Watch out - **Install by copying.** A symlinked mod folder loads its JavaScript but draws no icons. `install.sh` copies the files. The scripts are found at `<Herdr home>/mods/claude-usage/`, the `MOD` constant in `data.js`: rename both together. - Transcripts are large (tens of MB for a long session with subagents). Never read them whole on every refresh, and not with `readTextFile` (1 MiB limit). Reading from a byte offset with `tail -c` keeps a refresh to milliseconds; the first scan of a busy day reads everything once (about 3 seconds for 74 files on the machine it was built on). - One reply is written as several lines with the same `message.id` and identical usage. Count each id once. They are always consecutive, so remembering the last id per file is enough. - The usage on a line is the last `"usage":{` on it: it comes after the message content, and quotes inside content strings are escaped. Check `"type":"assistant"`; other line types carry no usage. - A session's subagents write to `<id>/subagents/`, not to `<id>.jsonl`. Most of a team session's tokens are there. - The usage endpoint is not a documented API. Its shape has changed before and may again; anything missing is left out rather than shown as 0. Never refresh the OAuth token yourself: that rotates Claude Code's refresh token. An expired token waits for Claude Code to renew it. - On macOS the first `security` call from Herdr may ask to allow Keychain access. - `resets_at` has six fractional digits; trim to three before `new Date()`. There is no `Intl`: numbers are formatted by hand. - Herdr's agent list and `agent.next` order belong to the agents mod; this mod doesn't call `setAgentOrder`. - Remote agents (an address with `/`) have no local transcript and show without numbers. The quota is the signed-in account's on this computer. - Use `hc interrupt`, not `hc keys <t> esc`: it knows each program's stop key and reports whether the turn stopped. With Claude Code's vim mode on, a working agent is usually in insert mode, and one Escape only leaves it; the second press stops the turn. Press the second time only when `hc` reports `still_working`, so a stopped agent doesn't get a stray Escape. - The quota's percentages are whole numbers and change in steps. A slope from a few minutes of samples is noise, which is why the forecast waits for 15 minutes (5-hour) or 45 minutes (weekly) of samples. - `resets_at` comes back slightly different on every call (other microseconds, and sometimes just under the minute). Compare reset times rounded to the nearest minute (`resetKey`), or every fetch looks like a new window and the 80%/95% toasts would repeat. Version 1 keyed its toasts that way. - A hot reload (saving a file, `install.sh`) doesn't call `close()`, so the old copy's lease looks alive and numbers stop for up to 100 seconds until a copy takes it over. - A floating block needs your layout to place it: add `{ "id": "claude-usage", "content": { "mod": "claude-usage" }, "edge": "right", "align": "center", "peek": 52 }` to a node's `floating`. The drawer view was checked by rendering it in a pane with a forced placement, not as a real floating block. - A view that throws while drawing shows Herdr's "This view could not be rendered" box, but `herdr-app status` still lists no errors. Look at a `herdr-app shot` after every change. - Costs are estimates. Subscription plans aren't billed per token, and the table needs editing when prices change. ## Done when - `herdr-app check ~/.config/herdr-app/mods/claude-usage` passes and `herdr-app status` shows no errors. - `herdr-app do mod.open --name claude-usage` shows the account's limits as Claude Code reports them, today's cost, and one row per Claude Code agent in Herdr with a session cost. - A row's tokens match a JSON parse of `<id>.jsonl` plus `<id>/subagents/*.jsonl`, deduplicated by `message.id`. - Clicking a row shows that agent and closes the popup. - With `{ "demo": … }` from `demo/make-demo.py`: the 5-hour bar is amber at 86% with "At this pace, runs out at … (in 28m)" in red, Weekly says "On pace to reset first · 0.4%/h", Fable weekly is red at 96% with "Forecast after … more min of samples"; three toasts appear once (80%, 95%, runs out soon); "Refactor auth middleware" is the top spender at about $0.42/min; the 7-day chart has a bar for each day. - The 7-day totals match a JSON parse of the same transcripts grouped by local date. - On a throwaway agent (`claude --model haiku` given a long answer to write, in insert mode), Interrupt then a second click within 3 seconds stops the turn ("Interrupted" in its terminal) and the toast says "Stopped"; a single click turns back into "Interrupt" after 3 seconds. - `pane.split {direction: "right", mod: "claude-usage", props: {view: "panel"}}` shows the panel; with no Claude sign-in it shows a note in place of the bars and still lists agents.