fix(web): make the Anthropic Admin API half of the Usage page actually work - #22
Merged
Merged
Conversation
…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>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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_KEYconfigured saw a permanent error banner, and even without the 400 the cards could only render $0.00/zeros.start_date/end_date; the API requiresstarting_at(RFC 3339) — every request 400'dbucket_width=1dtotal_cost,input_tokens,total_sessions,lines_of_code— fields that don't exist incost_report/usage_report(real shape: time buckets withresults[]; amounts are decimal strings in cents){totalCostUsd, daily[], days, truncated}/ token totals with cache splits); the page consumes them typedhas_more/next_pageignoredAlso:
start_date/end_dateparameterization for a working?days=N.Tests
Admin-route tests rewritten against the real report shapes: asserts
starting_atis RFC 3339 andstart_dateis gone, cents→USD aggregation math,next_pagefollowing, token normalization incl.cache_creationsubfields. 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_KEYin this environment). Worth one manual look at/usagewith a real key after merge.🤖 Generated with Claude Code