Skip to content

fix(web): make the Anthropic Admin API half of the Usage page actually work - #22

Merged
iaj6 merged 1 commit into
mainfrom
fix/admin-usage-page
Jul 12, 2026
Merged

iaj6 merged 1 commit into
mainfrom
fix/admin-usage-page

Conversation

@iaj6

@iaj6 iaj6 commented Jul 12, 2026

Copy link
Copy Markdown
Owner

Summary

The July audit found the org-wide (Admin API) section of the Usage page has never worked — three compounding defects meant admins with ANTHROPIC_ADMIN_API_KEY configured saw a permanent error banner, and even without the 400 the cards could only render $0.00/zeros.

Defect Fix
Proxies sent start_date/end_date; the API requires starting_at (RFC 3339) — every request 400'd Routes build a last-N-days window (default 30, clamped to the API's 31-day bucket max), bucket_width=1d
Page read total_cost, input_tokens, total_sessions, lines_of_code — fields that don't exist in cost_report/usage_report (real shape: time buckets with results[]; amounts are decimal strings in cents) Routes normalize server-side into small typed shapes ({totalCostUsd, daily[], days, truncated} / token totals with cache splits); the page consumes them typed
has_more/next_page ignored Bounded pagination loop

Also:

  • Activity Summary card removed — its fields come from the separate Claude Code Analytics API, not the usage report, so it could only ever show zeros. Can be re-added if that API gets integrated.
  • Hooks drop the dead start_date/end_date parameterization for a working ?days=N.
  • Token Usage card now shows the cache-read share of input, which the old flat fields couldn't express.

Tests

Admin-route tests rewritten against the real report shapes: asserts starting_at is RFC 3339 and start_date is gone, cents→USD aggregation math, next_page following, token normalization incl. cache_creation subfields. 401/403/501 coverage unchanged. Full workspace suite green (1,288 tests), web lint clean.

Caveat

Verified against the documented API shapes and unit-tested with mocked upstream responses — not exercised against a live org (no ANTHROPIC_ADMIN_API_KEY in this environment). Worth one manual look at /usage with a real key after merge.

🤖 Generated with Claude Code

…y work

The org-wide section of the Usage page has never functioned. Three
compounding defects:

1. Wrong request parameters: the proxies sent start_date/end_date, but
   the Admin API takes starting_at (RFC 3339, required) — so with the
   page's default no-date call the upstream request had no time range at
   all and every request 400'd, rendering a permanent error banner for
   admins.
2. Wrong response parsing: the page read total_cost/daily/input_tokens/
   total_sessions/lines_of_code — none of which exist in the cost_report
   or usage_report responses (real shape: {data: [time buckets with
   results[]], has_more, next_page}; amounts are decimal strings in
   CENTS).
3. No pagination: has_more/next_page were ignored.

Fix: the routes now query a last-N-days window (default 30, clamped to
the API's 31-bucket max), follow pagination (bounded), and normalize
server-side into small typed shapes: cost -> {totalCostUsd, daily[],
days, truncated}; analytics -> token totals incl. cache read/creation
splits and web-search request counts. The page consumes those shapes
with real types instead of Record<string, unknown>.

The Activity Summary card is removed: its fields (sessions, lines of
code, request counts) come from the separate Claude Code Analytics API,
not the usage report, so it could only ever render zeros. Re-add it if
we integrate that API.

Hooks lose the dead start_date/end_date parameterization (never passed,
and the params were meaningless upstream) in favor of a working ?days=N.

Tests rewritten against the real report shapes: RFC 3339 starting_at,
cents->USD aggregation, pagination following, and token normalization.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
@iaj6
iaj6 merged commit ef45469 into main Jul 12, 2026
3 checks passed
@iaj6
iaj6 deleted the fix/admin-usage-page branch July 12, 2026 03:56
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant