Repository navigation
Description
Target: v6.
Target branch:
v6only. Open the PR with--base v6.hive-open-prdefaults to the repo's default branch (v5) when--baseis omitted. The admin MCP (src/pkg/adminmcp) exists only onv6.
Citations:path:lineagainstv6@7146d3c99.
Executive summary
An operator can't easily answer "which agent used the most tokens in the last 24 hours?". The MCP's spend_read returns per-agent totals only for all time. Its daily_rollup is split by day but adds up all agents together. On the dashboard, each agent card shows a small $ chip for the Cost section's selected timeframe. But it shows dollars only (no tokens), it's spread across the cards (no ranked list), and the MCP has no equivalent.
This issue adds a per-agent breakdown for a chosen time window, with tokens and estimated $, in both places:
- the Cost card's existing by-agent table follows the timeframe selector, and
- the admin MCP can read the same numbers.
Every hive wants this; nothing about it is specific to one deployment.
What exists today (don't rebuild it)
- 5-minute cost snapshots (
CostHistoryEntry,src/pkg/dashboard/server.go:1745) keep about 30 days (costHistoryMaxEntries,server.go:1766). Each carriesAgents map[string]float64(cumulative $ per agent) andModels map[string]CostModelSnap(cumulative tokens and $ per model).sampleCostHistory(server.go:4736) writes them. Per-agent tokens are not recorded: only $. GET /api/cost/history(api.go:163,handleCostHistoryapi.go:1523) returns the raw snapshots.- Client-side per-agent window $:
agentWindowSpend(name)(static-src/js/cost-trend.js:568) sums the positive step-to-step increases ofe.agents[name]insidecostWindow()(cost-trend.js:159).agentSpendChip/agentSpendRow(static-src/js/agent-cards.js:25,31) render it on the agent cards. - The Cost card's by-agent table (
static-src/js/cost.js:453,est.by_agent) shows all-time totals fromGET /api/cost. It ignores the timeframe selector. - Server-side positive-increment rules:
costFoldDailyRollups(src/pkg/dashboard/cost_rollup.go:166) folds snapshots into days. It discards restore-shaped jumps (costRollupStepRegainsGround,costRollupStepAllowed; 🐛 cost: daily rollup discards real spend after sampling gaps (discontinuity ceiling) #13482, 🐛 Cost chart: fixed $500 discontinuity floor discards real daily session-flush spend on Opus/Fable hives #13497) so a re-seed isn't counted as spend. - MCP:
spend_readmaps to plainGET /api/cost(src/pkg/adminmcp/adminmcp.go:862) and takes no window argument.launch_cost_read(src/pkg/adminmcp/tools_launch_cost.go) is the pattern to copy for a tool with validated arguments (LaunchCostReadPath).
Wanted behavior
- Record per-agent tokens in each snapshot. Add an additive, omitempty per-agent token map next to
Agents(for exampleAgentTokens map[string]CostAgentSnapwith input, output, cache_read and cache_create). Fill it insampleCostHistoryfrom the sameestimatedCost().ByAgentrows that already supply the $. Older snapshots without it must still load. - One server-side window aggregation. Add a function that takes the snapshot ring plus
[start, end)and returns one row per agent: tokens (input, output, cache_read, cache_create, total), estimated USD, first/last sample time in the window, and acoverageflag for when the window starts before the oldest snapshot carrying that agent. Use the same positive-increment and discontinuity rules ascostFoldDailyRollups(factor shared helpers out; don't fork the logic). Sort by USD, then total tokens, descending. - API. Expose it read-only. Pick one and say why in the PR: a new
GET /api/cost/agents?since=<ms>&until=<ms>(orhours=N), or a query parameter on an existing cost endpoint. Bound the window (it can't reach past the ring; say so in the response instead of returning zeros). - Dashboard. Make the Cost card's by-agent table follow the existing timeframe selector (hourly/daily/weekly/monthly/custom), showing tokens and $ for the window from the new endpoint, highest first. Keep the all-time view available. Match the existing Cost card's look and wording; no new layout system. Make sure the agent-card chips and the table agree (ideally both read the server numbers).
- MCP (parity). Extend
spend_readwith optionalhours(1–720) orsince/untilarguments, or add a sibling read tool. Validate the arguments before any request, the wayLaunchCostReadPathdoes. The tool description must say the numbers are estimates and name the window.
Acceptance criteria
- A new snapshot carries per-agent token counts; a snapshot written before this change still decodes, and the window function treats an agent with no token data in range as "no data" (null), not 0.
- With synthetic snapshots, the window function gives the same per-agent $ as today's
agentWindowSpendfor the same window (positive increments only), and drops a restore-shaped jump exactly ascostFoldDailyRollupsdoes. - A window older than the ring returns
coverage: partial(or similar) plus the real start time, not silent zeros. - The Cost card by-agent table changes with the timeframe selector and shows tokens + $ per agent, sorted highest first.
- The MCP answers "top agent by tokens over the last 24 hours" in one call, with the same numbers the dashboard shows for a 24-hour custom range.
- Bad MCP arguments (negative hours,
untilbeforesince, over the cap) are refused with a reason and make no request. - No change to the governor budget, budget enforcement, or
/api/launch-cost.
Test plan
- Go unit tests next to
cost_rollup_test.go/cost_history_test.go: window aggregation with (a) steady growth, (b) a counter drop after a restart, (c) a restore-shaped jump, (d) a window starting before the ring, (e) old snapshots with no token map. - Admin MCP: argument validation and path building, following
src/pkg/dashboard/admin_mcp_launch_cost_test.go. - JS: the existing cost-trend/cost tests for the table; add a case for range switching. If a test lifts card JS by function name, register new helpers the same way the existing ones are.
- Run
gofmt -llocally; let the hive's CI run the Go and JS suites.
Out of scope / do not change
- Per-issue or per-PR attribution (
/api/launch-cost,launch_cost_read). - Budgets, governor window, alerts.
- Backends without a per-message timeline: they still contribute through snapshot deltas; no new collection work.
- Price tables.
Activity
hivecommons-hive commentedon Nov 18, 2026
A hive agent asked to claim this issue, but it is covered by #18971 (@Danathar). The claim was not recorded; the owner decides whether to proceed.
hivecommons-hive commentedon Nov 18, 2026
Fixed by #18971 (merged 03f97c5). Closing. Reply /reopen if the problem persists.
github-actions commentedon Nov 18, 2026
@Danathar this issue was closed by @hivecommons-hive[bot] as completed.
If this was closed by a merged PR, the fix only takes effect once it ships in a tagged release and your deployment updates to it — check your running version against the fix before concluding the fix failed.
If you are still seeing the problem, comment
/reopenand it will be reopened for you — you do not need write access. Please add what you are still observing (running version, logs, steps) so it is not closed the same way again.