Writes your Claude usage limits and credits to ~/.claude/usage-reporter/usage.json so other tools can read them

A Claude Code mod that writes your Claude usage limits and credits to a file, so menu bar apps, status lines, scripts, and other mods can read them without each one asking Anthropic.
It writes ~/.claude/usage-reporter/usage.json:
{
"version": 1,
"at": "2026-10-03T19:51:50.920Z",
"windows": [
{ "kind": "session", "percent": 7, "resetsAt": "2026-10-04T00:50:00.000Z", "at": "2026-10-03T19:51:50.920Z" },
{ "kind": "weekly", "percent": 25, "resetsAt": "2026-10-04T23:00:00.000Z", "at": "2026-10-03T19:51:50.920Z" },
{ "kind": "weekly", "label": "Fable", "percent": 48, "resetsAt": "2026-10-04T22:59:59.000Z", "at": "2026-10-03T19:49:51.598Z" }
],
"credits": { "enabled": false, "used": 0, "limit": null, "currency": "USD", "at": "2026-10-03T19:51:50.920Z" },
"cloudSessionCredits": { "used": 0, "limit": 250, "currency": "USD", "resetsAt": "2026-11-05T07:59:00.000Z", "at": "2026-10-03T19:51:50.920Z" },
"projectSetupCredit": { "used": 17.993769, "limit": 100, "currency": "USD", "expiresAt": "2026-10-05T17:16:23.346Z", "at": "2026-10-04T18:37:30.984Z" },
"grants": [
{ "id": "extra_usage", "label": "Extra usage", "used": 0, "limit": 100, "currency": "USD", "at": "2026-10-04T18:37:30.984Z" },
{ "id": "iguana_necktie", "label": "Cloud sessions", "used": 1.864015, "limit": 250, "currency": "USD", "endsAt": "2026-11-05T07:59:00.000Z", "ends": "expiry", "at": "2026-10-04T18:37:30.984Z" },
{ "id": "harbor_lantern", "label": "Project setup", "used": 17.993769, "limit": 100, "currency": "USD", "endsAt": "2026-10-05T17:16:23.346Z", "ends": "expiry", "at": "2026-10-04T18:37:30.984Z" }
],
"weeklyBreakdown": {
"windowStartedAt": "2026-09-27T23:00:00.902Z",
"rows": [
{ "key": "claude_code", "label": "Claude Code", "percent": 100 },
{ "key": "chat", "label": "Chats", "percent": 0 },
{ "key": "cowork", "label": "Cowork", "percent": 0 },
{ "key": "other", "label": "Other", "percent": 0 }
],
"at": "2026-10-03T19:51:50.920Z"
},
"raw": {}
}
Requires a Claude Code version with mods (Anthropic supports mods on 2.1.287 and later; the mod has also run on 2.1.251) and a Claude subscription login.
git clone https://github.com/tksunw/usage-reporter ~/.claude/mods/usage-reporter
Then point Claude Code at that folder's parent in ~/.claude/settings.json, if it does not already:
{
"env": {
"CLAUDE_CODE_PLUGIN_DIRS": "~/.claude/mods"
}
}
Claude Code loads every folder under ~/.claude/mods as a mod, which is why it lives there and not in ~/.claude/skills: pointing CLAUDE_CODE_PLUGIN_DIRS at the skills folder would try to load every skill as a mod.
Start a new Claude Code session. The file appears after the session starts. To remove the mod, delete ~/.claude/mods/usage-reporter; the last report stays in ~/.claude/usage-reporter/ until you delete that too.
To try it for one session without installing: claude --plugin-dir /path/to/usage-reporter.
Pull the latest version into the folder you cloned:
git -C ~/.claude/mods/usage-reporter pull
The next Claude Code session you start runs the new version.
The tksunw marketplace lists this mod, so Claude Code can fetch and update it for you:
claude plugin marketplace add tksunw/claude-plugins
claude plugin install usage-reporter@tksunw
Turn on auto-update for the tksunw marketplace in /plugin (third-party marketplaces default to off), or run claude plugin marketplace update tksunw by hand. Updates are keyed on version in .claude-plugin/plugin.json, so a release without a version bump is not picked up. Use one install method, not both: remove the clone from ~/.claude/mods before installing this way.
Format version 1. A reader should check version and stop if it is not one it knows.
| Field | Meaning |
|---|---|
version | 1 |
at | When the file was last written, ISO 8601 UTC |
windows[] | One entry per usage window. Empty on an Enterprise login, which has no windows, only a spend budget in credits (and in grants when spending is enabled) |
windows[].kind | session (the 5-hour window) or weekly (the 7-day window) |
windows[].label | Present on a weekly window scoped to one model family, for example Fable. Absent on the all-models windows |
windows[].percent | Percent of the window used, 0 to 100 |
windows[].resetsAt | When the window resets, ISO 8601 UTC. Can be absent |
windows[].at | When this window's figure was read. Scoped windows can be older than the others |
credits | Usage credits (extra usage). Absent when Anthropic's response carries no credit figures |
credits.enabled | Whether credits are turned on |
credits.used | Credits spent, a number in major units of currency (dollars, not cents) |
credits.limit | The spend limit, in the same units. null when no limit is set. Absent when a limit is set in a shape the mod does not know |
credits.currency | ISO 4217 code, for example USD. Can be absent |
credits.at | When the credit figures were read. Can be older than the file's at |
cloudSessionCredits | The credit grant for cloud sessions. Absent when Anthropic's response does not carry it |
cloudSessionCredits.used | Dollars spent |
cloudSessionCredits.limit | Dollars granted. Can be absent |
cloudSessionCredits.currency | USD |
cloudSessionCredits.resetsAt | When the credit expires, ISO 8601 UTC. Despite the name it is an expiry, as Claude's usage page shows it; the name stays for existing readers. Can be absent |
cloudSessionCredits.at | When the figures were read. Can be older than the file's at |
projectSetupCredit | The one-time Claude Projects setup credit, shown in Claude Desktop as "Project setup credit". Absent when Anthropic's response does not carry it, which includes before it is granted |
projectSetupCredit.used | Dollars spent |
projectSetupCredit.limit | Dollars granted. Can be absent |
projectSetupCredit.currency | USD |
projectSetupCredit.expiresAt | When the credit expires, ISO 8601 UTC. It does not reset. Can be absent |
projectSetupCredit.at | When the figures were read. Can be older than the file's at |
grants[] | Every dollar credit in one list, for readers that want to show them all without knowing each kind: extra usage when it is turned on, then every grant in Anthropic's response, including ones this mod has never seen. Absent when there are none. Build on this rather than the three fields above |
grants[].id | Where it came from: extra_usage, or Anthropic's codename for the grant (iguana_necktie, harbor_lantern). Stable; use it to tell grants apart |
grants[].label | A name to show, for example Cloud sessions. For a grant the mod does not know, the codename itself |
grants[].used | Dollars spent |
grants[].limit | Dollars available. null when no limit is set. Can be absent |
grants[].currency | ISO 4217 code, for example USD |
grants[].endsAt | When the grant resets or expires, ISO 8601 UTC. Can be absent |
grants[].ends | reset or expiry, saying which endsAt is. Absent when not known |
grants[].at | When the figures were read. Can be older than the file's at |
plan | The subscription plan, from Anthropic's profile endpoint, asked for at most every six hours. Absent with an API-key login, offline, or before the first successful call. Added in 0.6.0 |
plan.label | A short name to show, from the organization type and then the tier within it: Team (Premium) when a Team seat carries default_claude_max_5x (Premium seats do), else Team (Standard); Max (20x) on default_claude_max_20x, else Max (5x); Pro; Enterprise; Free for any other organization type. With no organization type, Max or Pro from the account's flags. Changed in 0.6.2: 0.6.1 wrote Team for every Team seat and Max for a Max organization with no tier |
plan.tier | Anthropic's rate_limit_tier as sent, when it sent one |
plan.at | When the plan was read |
weeklyBreakdown | The weekly window's usage split by surface, account-wide (claude.ai chat included). Absent when Anthropic's response does not carry it |
weeklyBreakdown.windowStartedAt | When the weekly window began, ISO 8601 UTC. Can be absent |
weeklyBreakdown.rows[] | One entry per surface, in Anthropic's order. Keys not listed here are passed through; show them rather than dropping them |
weeklyBreakdown.rows[].key | Surface id. Seen: claude_code, chat, cowork, other |
weeklyBreakdown.rows[].label | Anthropic's display name, for example Chats. Can be absent |
weeklyBreakdown.rows[].percent | See below. Not the percent of the weekly limit |
weeklyBreakdown.at | When the figures were read. Can be older than the file's at |
raw | Anthropic's last usage response, unparsed, for debugging. Its shape is theirs and changes without notice. Do not build on it |
What weeklyBreakdown.rows[].percent measures is not settled. Every reading so far had one non-zero row, claude_code at 100, while the weekly window stood at 20% and later 43%. So it is not the weekly percent, and it fits "share of this week's usage, rows summing to 100", but no reading with two non-zero rows has confirmed that. Treat it as a relative share until one does.
A window whose resetsAt has passed has rolled over; treat it as empty until the next report.
Reading it from a shell:
jq -r '.windows[] | "\(.kind) \(.label // "all") \(.percent)%"' ~/.claude/usage-reporter/usage.json
Only while a Claude Code session is running. Nothing runs on a timer.
credits, cloudSessionCredits, projectSetupCredit, grants, and weeklyBreakdown come only from the endpoint, so they carry over unchanged until the next call.plan and at.So session and weekly follow each turn, and model-scoped windows and credits update at most every five minutes. Usage from claude.ai chat or Claude Desktop shows up at the next Claude Code turn. A reader that wants the file fresher between sessions can start a session of its own with only the mod loaded (claude --setting-sources "" --plugin-dir <mod> --tools "", no prompt) and end it once the report lands; the session-start call runs the same way, inside the same floor. Tokenometer does this.
$.session.authorize(), gets an opaque handle, and passes the handle to $.http.fetch. Claude Code attaches the credential on its side.GET https://api.anthropic.com/api/oauth/usage, made by Claude Code. This is the call behind /usage.~/.claude/usage-reporter/usage.json and reads it back to merge. The file holds percentages, reset times, credit figures, the per-surface split, and raw, Anthropic's last response as given. No token, no prompts. The mod cannot set the file's mode, so it gets your default permissions; on a Mac with other accounts that can reach ~/.claude, they can read your usage and credit figures.HOME, else USERPROFILE.claude plugin validate . prints the same list from the source. The whole mod is hooks/register.ts.
cloudSessionCredits is read from a key Anthropic names by codename (iguana_necktie), matched to the credit by its amount. If they rename it, the field goes absent until the mod is updated.grants treats any top-level object in Anthropic's response with a numeric used_dollars as a grant, other than the usage windows. A new grant appears there under its codename without a mod update, but with no friendly label or ends until the mod learns it.projectSetupCredit is read from harbor_lantern, another codename, matched to Claude Desktop's "Project setup credit" bar by its limit, spend, and expiry. After the credit expires the key goes back to null, so projectSetupCredit and its grant drop out of the file (seen 2026-10-05).limits[] with null five_hour and seven_day) while the status line has no windows either. The file then has an empty windows[] and the budget in credits, and in grants when spending is enabled, refreshed at most every five minutes. Any other response without windows is treated as a shape the mod cannot read, and the last report's windows stay. The response carries no reset date for the budget, so none is written; Claude's settings page shows it resetting at the start of each month.CLAUDE_CODE_DISABLE_NONESSENTIAL_TRAFFIC set, Claude Code refuses the call and you get session and weekly only.claude plugin validate .
claude plugin test .
CI runs both on every push to main and every pull request, against the latest Claude Code.
MIT. See LICENSE.
hooks/register.ts 328 lines1import type { EngineInterface, Register, SessionRateLimit } from 'claude-code'
2
3// Claude Code makes the usage call with its own login, and this mod writes the answer to a file
4// other tools read. The credential never reaches the mod.
5const ENDPOINT = 'https://api.anthropic.com/api/oauth/usage'
6const FILE = '.claude/usage-reporter/usage.json'
7const FLOOR_MS = 5 * 60_000 // the endpoint rate-limits; one call per five minutes across all sessions
8const BACKOFF_MS = 10 * 60_000 // after a 429
9const PROFILE_ENDPOINT = 'https://api.anthropic.com/api/oauth/profile'
10const PLAN_TTL_MS = 6 * 60 * 60_000 // a plan changes rarely; ask again after six hours
11
12/** One usage window. A weekly window with a `label` is scoped to that model family. */
13type Window = { kind: 'session' | 'weekly'; label?: string; percent: number; resetsAt?: string; at: string }
14
15/**
16 * Usage credits, in major units of `currency` (dollars, not cents). `limit` is null when no limit
17 * is set, and absent when one is set in a shape this mod does not know.
18 */
19type Credits = { enabled: boolean; used: number; limit?: number | null; currency?: string; at: string }
20
21/** Cloud session credits, in dollars. `resetsAt` is when the credit expires (Claude's usage page says "Expires"); the name predates that. */
22type CloudCredits = { used: number; limit?: number; currency: 'USD'; resetsAt?: string; at: string }
23
24/** The one-time Claude Projects setup credit, in dollars. It expires at `expiresAt`; it does not reset. */
25type SetupCredit = { used: number; limit?: number; currency: 'USD'; expiresAt?: string; at: string }
26
27/**
28 * One dollar credit or grant. `id` is the key it came from (`extra_usage`, or Anthropic's codename),
29 * `label` a name for people, the codename itself when the mod does not know it. `limit` is null when
30 * no limit is set. `ends` says whether `endsAt` is a reset or an expiry, and is absent when not known.
31 */
32type Grant = { id: string; label: string; used: number; limit?: number | null; currency: string; endsAt?: string; ends?: 'reset' | 'expiry'; at: string }
33
34/** Codenamed grants the mod knows. Any other top-level object with a numeric `used_dollars` still becomes a grant. */
35const KNOWN_GRANTS: Record<string, { label: string; ends?: Grant['ends'] }> = {
36 iguana_necktie: { label: 'Cloud sessions', ends: 'expiry' },
37 harbor_lantern: { label: 'Project setup', ends: 'expiry' },
38}
39
40/** The weekly window's usage by surface (Claude Code, chat, ...). Rows pass through as given, unknown keys included. */
41type Breakdown = { windowStartedAt?: string; rows: { key: string; label?: string; percent: number }[]; at: string }
42
43/** The subscription plan. `label` is short (`Max (5x)`, `Max (20x)`, `Team (Premium)`, `Team (Standard)`, `Pro`, `Free`, `Enterprise`, or `Max` from the account flag alone); `tier` is Anthropic's `rate_limit_tier` as given, when it sent one. */
44type Plan = { label: string; tier?: string; at: string }
45
46/** The file, format version 1. `raw` is Anthropic's last response, unparsed; its shape is theirs. */
47type Report = {
48 version: 1
49 at: string
50 windows: Window[]
51 credits?: Credits
52 cloudSessionCredits?: CloudCredits
53 projectSetupCredit?: SetupCredit
54 grants?: Grant[]
55 weeklyBreakdown?: Breakdown
56 plan?: Plan
57 raw?: unknown
58}
59
60export const register: Register = on => {
61 on('session.start', async ($, e, next) => {
62 const started = await next(e)
63 await report($).catch(() => {})
64 return started
65 })
66
67 on('session.measure', async ($, e, next) => {
68 if (e.changed.includes('rateLimits')) await report($, e.rateLimits).catch(() => {})
69 return next(e)
70 })
71
72 // A quiet stretch moves no whole point, so nothing above writes and readers see an old file. Once the
73 // floor has lapsed, a main-conversation turn reports anyway, which stamps the file current.
74 on('turn.complete', async ($, e, next) => {
75 const done = await next(e)
76 if (!e.agentId && (await $.clock.now()) >= Number((await $.store.get('nextFetchAt')) ?? 0)) await report($).catch(() => {})
77 return done
78 })
79}
80
81async function report($: EngineInterface, rateLimits?: readonly SessionRateLimit[]) {
82 // HOME, else USERPROFILE for Windows; status-enhanced resolves it the same way.
83 const home = (await $.env.get('HOME')) || (await $.env.get('USERPROFILE'))
84 if (!home) return
85 const path = `${home}/${FILE}`
86 const now = await $.clock.now()
87 const at = new Date(now).toISOString()
88 const last = await $.fs.read(path).then(text => JSON.parse(text) as Partial<Report>, () => ({}) as Partial<Report>)
89 const prior = last.version === 1 ? last : ({} as Partial<Report>)
90 const plan = await readPlan($, prior.plan, now, at)
91 const write = (rest: Omit<Report, 'version' | 'at' | 'plan'>) => $.fs.write(path, JSON.stringify({ version: 1, at, plan, ...rest } satisfies Report))
92
93 // A plan that just appeared on a file without one is an upgrade from an older mod: skip the floor
94 // once so the fields that version never wrote arrive now. Self-limiting, since the next read has a plan.
95 const newPlan = prior.plan === undefined && plan !== undefined
96 if (newPlan || now >= Number((await $.store.get('nextFetchAt')) ?? 0)) {
97 // Claim the slot before the call so a second session starting now skips it.
98 await $.store.set('nextFetchAt', now + FLOOR_MS)
99 try {
100 const auth = await $.session.authorize()
101 if (auth?.kind === 'bearer') {
102 const res = await $.http.fetch(ENDPOINT, { headers: { 'anthropic-beta': 'oauth-2025-04-20' }, auth: auth.handle })
103 if (res.ok) {
104 const raw: unknown = JSON.parse(res.text)
105 const credits = fromCredits(raw, at)
106 const grants = fromGrants(raw, credits, at)
107 const windows = fromUsage(raw, at)
108 // An Enterprise login has no windows, only a spend budget, and is written with `windows: []`.
109 // It is recognized by its shape, not by an empty status line, which a session also has before
110 // its first turn. Any other response without windows is a shape this mod does not read, and
111 // the merge below keeps the last report's windows instead.
112 const enterprise =
113 windows.length === 0 && isWindowless(raw) && fromRateLimits(rateLimits ?? (await $.session.usage()).rateLimits, at).length === 0
114 if (windows.length > 0 || enterprise)
115 return write({
116 windows,
117 credits,
118 cloudSessionCredits: fromCloudCredits(raw, at),
119 projectSetupCredit: fromSetupCredit(raw, at),
120 grants,
121 weeklyBreakdown: fromBreakdown(raw, at),
122 raw,
123 })
124 }
125 if (res.status === 429) await $.store.set('nextFetchAt', now + BACKOFF_MS)
126 }
127 } catch {
128 // Refused or offline: fall through to the figures the session already has.
129 }
130 }
131
132 // Inside the floor, or the call failed: the session and weekly percent Claude Code already holds
133 // for its status line, merged into the last report so the model-scoped windows and credits stay.
134 // With none (an Enterprise login), the last report stands, except that a plan fetched just now is
135 // still written: its slot in the store is already spent.
136 const latest = fromRateLimits(rateLimits ?? (await $.session.usage()).rateLimits, at)
137 const { windows = [], credits, cloudSessionCredits, projectSetupCredit, grants, weeklyBreakdown } = prior
138 if (latest.length === 0) {
139 if (plan !== prior.plan) await write({ windows, credits, cloudSessionCredits, projectSetupCredit, grants, weeklyBreakdown, raw: prior.raw })
140 return
141 }
142 await write({ windows: merge(windows, latest), credits, cloudSessionCredits, projectSetupCredit, grants, weeklyBreakdown, raw: prior.raw })
143}
144
145/**
146 * The plan: the last report's while it is fresh, else one profile call (at most one per five minutes
147 * across sessions, claimed before the call). A miss keeps the old plan rather than dropping it, so an
148 * API-key login or an offline start leaves `plan` as it was, or absent.
149 */
150async function readPlan($: EngineInterface, prior: Plan | undefined, now: number, at: string): Promise<Plan | undefined> {
151 const fresh = prior && now - Date.parse(prior.at) < PLAN_TTL_MS
152 if (fresh || now < Number((await $.store.get('nextPlanAt')) ?? 0)) return prior
153 await $.store.set('nextPlanAt', now + FLOOR_MS)
154 try {
155 const auth = await $.session.authorize()
156 if (auth?.kind !== 'bearer') return prior
157 const res = await $.http.fetch(PROFILE_ENDPOINT, { headers: { 'anthropic-beta': 'oauth-2025-04-20' }, auth: auth.handle })
158 return res.ok ? (fromProfile(JSON.parse(res.text), at) ?? prior) : prior
159 } catch {
160 return prior
161 }
162}
163
164/**
165 * A plan from the profile response, by organization type, then the tier within it. A Team Premium seat carries
166 * `default_claude_max_5x`, so that tier on a Team organization is Premium, and any other is Standard; a Max
167 * organization is 20x on `default_claude_max_20x`, else 5x. Another organization type reads Free. With no
168 * organization type at all, the account's Max and Pro flags decide, and with neither the plan is undefined.
169 */
170function fromProfile(raw: any, at: string): Plan | undefined {
171 const org = raw?.organization
172 const tier = typeof org?.rate_limit_tier === 'string' && org.rate_limit_tier ? org.rate_limit_tier : undefined
173 const type = typeof org?.organization_type === 'string' ? org.organization_type : ''
174 const label = type.includes('enterprise')
175 ? 'Enterprise'
176 : type === 'claude_team'
177 ? tier === 'default_claude_max_5x' ? 'Team (Premium)' : 'Team (Standard)'
178 : type === 'claude_max'
179 ? tier === 'default_claude_max_20x' ? 'Max (20x)' : 'Max (5x)'
180 : type === 'claude_pro'
181 ? 'Pro'
182 : type
183 ? 'Free'
184 : raw?.account?.has_claude_max === true
185 ? 'Max'
186 : raw?.account?.has_claude_pro === true
187 ? 'Pro'
188 : undefined
189 return label ? { label, ...(tier ? { tier } : {}), at } : undefined
190}
191
192/** The usage response: `limits[]` when present, else the older `five_hour` / `seven_day` objects. */
193function fromUsage(raw: any, at: string): Window[] {
194 const windows: Window[] = []
195 for (const limit of Array.isArray(raw?.limits) ? raw.limits : []) {
196 if (typeof limit?.percent !== 'number') continue
197 const base = { percent: limit.percent, resetsAt: iso(limit.resets_at), at }
198 if (limit.kind === 'session') windows.push({ kind: 'session', ...base })
199 else if (limit.kind === 'weekly_all') windows.push({ kind: 'weekly', ...base })
200 else if (limit.kind === 'weekly_scoped') {
201 const scope = limit.scope
202 windows.push({ kind: 'weekly', label: scope?.model?.display_name ?? scope?.display_name ?? scope?.name ?? 'Scoped', ...base })
203 }
204 }
205 if (windows.some(w => w.label === undefined)) return windows
206
207 const older = (key: string, kind: Window['kind'], label?: string): Window[] =>
208 typeof raw?.[key]?.utilization === 'number' ? [{ kind, label, percent: raw[key].utilization, resetsAt: iso(raw[key].resets_at), at }] : []
209 return [...older('five_hour', 'session'), ...older('seven_day', 'weekly'), ...older('seven_day_opus', 'weekly', 'Opus'), ...older('seven_day_sonnet', 'weekly', 'Sonnet')]
210}
211
212/**
213 * A response that says outright there are no windows: an empty `limits[]` and null `five_hour` and
214 * `seven_day`, as an Enterprise login returns. A missing key is not enough; that may be a renamed one.
215 */
216function isWindowless(raw: any): boolean {
217 return Array.isArray(raw?.limits) && raw.limits.length === 0 && raw.five_hour === null && raw.seven_day === null
218}
219
220/** Credits from the usage response: `spend` when it parses, else `extra_usage`. Undefined when neither does. */
221function fromCredits(raw: any, at: string): Credits | undefined {
222 const spend = raw?.spend
223 const used = major(spend?.used)
224 if (used !== undefined) {
225 // A set limit mirrors `used`. A bare number is unseen; it would be read as minor units at `used`'s exponent.
226 const limit = spend.limit === null ? null : major(spend.limit, spend.used.exponent)
227 return { enabled: spend.enabled === true, used, limit, currency: text(spend.used.currency), at }
228 }
229
230 // `used_credits` and `monthly_limit` are minor units (cents) at `decimal_places`, 2 when absent.
231 // Seen for both: `monthly_limit` 10000 beside a `spend.limit` of 100.00, and a non-zero
232 // `used_credits` equal to `spend.used.amount_minor`.
233 const extra = raw?.extra_usage
234 const places = extra?.decimal_places ?? 2
235 const spent = major(extra?.used_credits, places)
236 if (spent === undefined) return undefined
237 const limit = extra.monthly_limit === null ? null : major(extra.monthly_limit, places)
238 return { enabled: extra.is_enabled === true, used: spent, limit, currency: text(extra.currency), at }
239}
240
241/**
242 * Cloud session credits, from `iguana_necktie`. That key is Anthropic's codename, matched to the
243 * credit by its amount. When they rename it this field goes absent, and the fix is the key here.
244 */
245function fromCloudCredits(raw: any, at: string): CloudCredits | undefined {
246 const grant = raw?.iguana_necktie
247 if (typeof grant?.used_dollars !== 'number') return undefined
248 const limit = typeof grant.limit_dollars === 'number' ? grant.limit_dollars : undefined
249 return { used: grant.used_dollars, limit, currency: 'USD', resetsAt: iso(grant.resets_at), at }
250}
251
252/**
253 * The Projects setup credit, from `harbor_lantern`, another codename. Matched on 2026-10-04 to the
254 * "Project setup credit" bar in Claude Desktop by its $100 limit, used amount, and expiry time.
255 */
256function fromSetupCredit(raw: any, at: string): SetupCredit | undefined {
257 const grant = raw?.harbor_lantern
258 if (typeof grant?.used_dollars !== 'number') return undefined
259 const limit = typeof grant.limit_dollars === 'number' ? grant.limit_dollars : undefined
260 return { used: grant.used_dollars, limit, currency: 'USD', expiresAt: iso(grant.resets_at), at }
261}
262
263/**
264 * Every dollar credit in one list: extra usage when it is on, then each top-level object in the
265 * response with a numeric `used_dollars`, other than the usage windows (`five_hour`, `seven_day*`).
266 * Undefined when there are none.
267 */
268function fromGrants(raw: any, credits: Credits | undefined, at: string): Grant[] | undefined {
269 const grants: Grant[] = []
270 if (credits?.enabled) grants.push({ id: 'extra_usage', label: 'Extra usage', used: credits.used, limit: credits.limit, currency: credits.currency ?? 'USD', at })
271 for (const [id, grant] of Object.entries<any>(raw && typeof raw === 'object' ? raw : {})) {
272 if (id.startsWith('five_hour') || id.startsWith('seven_day') || typeof grant?.used_dollars !== 'number') continue
273 const known = KNOWN_GRANTS[id]
274 const limit = typeof grant.limit_dollars === 'number' ? grant.limit_dollars : undefined
275 grants.push({ id, label: known?.label ?? id, used: grant.used_dollars, limit, currency: 'USD', endsAt: iso(grant.resets_at), ends: known?.ends, at })
276 }
277 return grants.length > 0 ? grants : undefined
278}
279
280/** The weekly breakdown, from `seven_day_breakdown`. Rows without a string key and a numeric percent are dropped. */
281function fromBreakdown(raw: any, at: string): Breakdown | undefined {
282 const breakdown = raw?.seven_day_breakdown
283 const rows = (Array.isArray(breakdown?.rows) ? breakdown.rows : [])
284 .filter((row: any) => typeof row?.key === 'string' && typeof row.percent === 'number')
285 .map((row: any) => ({ key: row.key, label: text(row.display_name), percent: row.percent }))
286 return rows.length > 0 ? { windowStartedAt: iso(breakdown.window_started_at), rows, at } : undefined
287}
288
289/** A money figure in major units: `{ amount_minor, exponent }`, or a bare number of minor units at `exponent`. */
290function major(value: any, exponent?: unknown): number | undefined {
291 const minor = typeof value === 'number' ? value : value?.amount_minor
292 const places = value?.exponent ?? exponent
293 return typeof minor === 'number' && typeof places === 'number' ? minor / 10 ** places : undefined
294}
295
296const text = (value: unknown) => (typeof value === 'string' ? value : undefined)
297
298/** The status line's figures: `five_hour` and `seven_day`, no model-scoped windows. */
299function fromRateLimits(rateLimits: readonly SessionRateLimit[], at: string): Window[] {
300 const kinds: Record<string, Window['kind']> = { five_hour: 'session', seven_day: 'weekly' }
301 return rateLimits.flatMap(limit => {
302 const kind = kinds[limit.kind]
303 return kind ? [{ kind, percent: limit.percentUsed, resetsAt: iso(limit.resetsAt), at }] : []
304 })
305}
306
307/**
308 * Lays newer windows over the previous ones. The status line trails the endpoint by a point, so
309 * inside one window (same reset time) a lower reading never replaces a higher one.
310 */
311function merge(previous: Window[], latest: Window[]): Window[] {
312 const key = (w: Window) => `${w.kind}/${w.label ?? ''}`
313 const merged = previous.map(old => {
314 const next = latest.find(w => key(w) === key(old))
315 if (!next) return old
316 const isSameWindow = old.resetsAt !== undefined && next.resetsAt !== undefined && Math.abs(Date.parse(old.resetsAt) - Date.parse(next.resetsAt)) < 60_000
317 return isSameWindow && next.percent < old.percent ? old : next
318 })
319 return [...merged, ...latest.filter(w => !previous.some(old => key(old) === key(w)))]
320}
321
322/** A timestamp as ISO 8601 UTC, or as given when it does not parse. */
323function iso(value: unknown): string | undefined {
324 if (typeof value !== 'string') return undefined
325 const time = Date.parse(value)
326 return Number.isNaN(time) ? value : new Date(time).toISOString()
327}
328