Repository navigation
✨ [v6] Cost card + admin MCP: tokens and spend per agent over a time window - #18971
Conversation
…and MCP An operator could not answer "which agent used the most tokens in the last 24 hours": spend_read gave per-agent totals for all time only, and the agent cards' $ chips were dollars only, spread across the cards. - Each 5-minute cost snapshot now carries per-agent token counters (agent_tokens) beside the per-agent dollars; older snapshots still decode and their tokens read as unknown (null), not zero. - costAgentWindow folds the ring into one row per agent for [since, until]: tokens (input, output, cache read, cache create, total), estimated usd, first/last sample and coverage, highest spend first. It uses the daily rollup's correction rules through one shared costCorrectionGuard, factored out of costFoldDailyRollups (behaviour unchanged there). - GET /api/cost/agents?since=&until= (RFC3339) or ?hours=N, the advisor endpoints' window parameters; none means the last 24 hours. A window that starts before the ring answers coverage partial with data_since. - Cost card: usage by sandbox follows the timeframe selector (tokens, input, output, est. $ per agent), with an all-time switch; the agent cards' spend chips read the same server rows. - Admin MCP: spend_read takes hours (1-720) or since/until and then reads /api/cost/agents; a bad window is refused before any request. Same in the stdio server. Signed-off-by: Danathar <6772335+Danathar@users.noreply.github.com> Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01BQYnPGFJx81c8JXR3o1sg1
|
[APPROVALNOTIFIER] This PR is NOT APPROVED This pull-request has been approved by: The full list of commands accepted by this bot can be found here. DetailsNeeds approval from an approver in each of these files:Approvers can indicate their approval by writing |
Open PR overlap check✅ Resolved: no conflicting open PRs or same-file overlaps remain against |
There was a problem hiding this comment.
Correctness review at d7bd02c. I read the per-agent window fold, the shared costCorrectionGuard refactor, SpendReadPath against advisorWindow on v6, and the dashboard fetch/render path. No blocker found.
- The
costFoldDailyRollupsrefactor is behaviour-preserving: the same guard condition and the same threshold inputs. - MCP and server window semantics agree.
hoursplusuntilanduntil-only both resolve to the same bounds, and a window withuntilbeforesinceis refused on both sides.
Advisory, non-blocking:
- Agents with tokens but $0 are invisible (
cost_agent_window.go,costAgentWindowname collection;server.go,sampleCostHistory). Names come only frome.Agents, which is written only whenrow.USD > 0. An agent that has token counters but unpriced or zero USD is recorded inAgentTokensyet never appears in the window table.- Fix: also collect names from
e.AgentTokens, and havecostAgentWindowForreadAgents[name]as 0 when it is absent.
- Fix: also collect names from
- Failed range fetch reads as "loading" forever (
cost.js, range branch). If/api/cost/agentsreturns non-2xx,_costAgentWindowFailedis set but the table body still saysloading the selected range….- Fix: when
_costAgentWindowFailed === costAgentWindowQuery().key, render "could not load the selected range".
- Fix: when
— hive: agent=adjudicator-5 backend=claude model=claude-sonnet-5-5 claude=2.1.327
…read fails Review on hivecommons#18971: an agent with tokens but no $ entry never got a row, and a failed /api/cost/agents read left the table on "loading" for good. Name agents from AgentTokens too (carrying the last $ across a missing entry), and show the failure, retrying the read after a minute. Signed-off-by: Danathar <6772335+Danathar@users.noreply.github.com> Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01BQYnPGFJx81c8JXR3o1sg1
|
Re the adjudicator-5 review at d7bd02c: I checked both advisories, and both were right. Both are fixed in 5815599.
|
Summary
You can now see which agent used the most tokens and estimated dollars over any time range, on the Cost card and through the admin MCP. Before this,
spend_readgave per-agent totals for all time only. The agent cards showed dollars only, spread across the cards, with no ranked list.spend_readtakeshours(1–720) orsince/until. It returns the same rows the table shows for that range.Fixes #18968
What changed
Recording
agent_tokens), next to the per-agent dollars it already stored.estimatedCost().ByAgentrows as the dollars.null(unknown), never 0.Server
costAgentWindow(src/pkg/dashboard/cost_agent_window.go) folds the snapshot ring into one row per agent for[since, until].input,output,cache_read,cache_create,total_tokensand estimatedusd, plusfirst_sample,last_sample,samplesandcoverage.costFoldDailyRollupsinto onecostCorrectionGuard(cost_rollup.go). Both the daily fold and the window use it. The fold's behaviour does not change.agentWindowSpendcomputes it, minus the restore-shaped jumps the daily rollup already drops.API
GET /api/cost/agents.?since=&until=(RFC3339) or?hours=N(1–720), parsed by the advisor endpoints'advisorWindow. With no window it answers the last 24 hours./api/cost./api/advisor/spend, so the frontend reuses the same bounds.coverage: "partial"withdata_since(the real start), not zeros.dashboard/openapi.jsonand the API reference.Dashboard
selected range / all timeswitch, styled like the existing range pills. The choice is remembered per browser./api/cost/agentsfor the window chosen above.agentWindowSpendreturns the server row when one exists for the current window. Until then it falls back to the browser's own sum, as before.static/index.htmlis regenerated fromstatic-src.Admin MCP
spend_readtakeshours,sinceanduntil. With any of them it reads/api/cost/agents; with none it reads/api/costas before.SpendReadPath, modelled onLaunchCostReadPath). Refused with a reason:hours;sincetogether withhours;untilbeforesince;cmd/hive-admin-mcp) both routespend_readthrough it.Against the issue
Nothing in the issue was wrong against current v6 (
86e4c0a9b); its code pointers still matched. Two choices it left open:since/until, not millisecond values. The issue offered either form. RFC3339 matches the advisor endpoints and the window the dashboard already sends.spend_readinstead of adding a sibling tool. The issue's first option, and the call with no arguments is unchanged.Tests
cost_agent_window_test.gocovers the five cases in the issue's test plan:agentWindowSpend;costFoldDailyRollupsdrops it, while a large climb to a new high is kept;admin_mcp_spend_window_test.go:/api/cost;hoursgoes to/api/cost/agentswith the agents rows capped;adminmcp/tools_spend_test.go: path building and every refusal.dashboard/test/cost-agent-window.test.js:Local checks
gofmt -l, the new node test (5/5 pass),check-docs-citations.py(2183 references verified) andcheck-api-reference-citations.sh.api.goandserver.goline numbers.check-api-reference-citations.sh --fixre-anchored them, so most of the doc diff is line numbers only.go test,go buildandgo vet, so CI is the first run of the Go tests.Out of scope (unchanged)
/api/launch-costandlaunch_cost_read.🤖 Generated with Claude Code
https://claude.ai/code/session_01BQYnPGFJx81c8JXR3o1sg1