#20603: fix(gateway): scan all agents in usage.cost RPC (#20558)
gateway
size: M
experienced-contributor
Cluster:
Usage Cost Enhancements
## Summary
- **Bug**: `usage.cost` RPC only reports tokens/cost from the default "main" agent, severely under-reporting in multi-agent deployments
- **Root cause**: `loadCostUsageSummaryCached` in `src/gateway/server-methods/usage.ts` called `loadCostUsageSummary` without an `agentId`, which defaults to scanning only the "main" agent's sessions directory
- **Fix**: Iterate all agents via `listAgentsForGateway` and merge per-agent summaries (daily + totals)
Fixes #20558
## Problem
`loadCostUsageSummary()` at `src/infra/session-cost-usage.ts:314` resolves the sessions directory via `resolveSessionTranscriptsDirForAgent(params?.agentId)`. When `agentId` is `undefined`, it defaults to `DEFAULT_AGENT_ID` ("main"), so only `agents/main/sessions/` is scanned.
The `sessions.usage` RPC already handles multi-agent correctly via `discoverAllSessionsForUsage`, but `usage.cost` did not.
**Before fix:**
Input: Multi-agent deployment with main (150 tokens) + worker (300 tokens)
Output: `totalTokens: 150` (only main agent)
## Changes
- `src/gateway/server-methods/usage.ts` — Add `loadCostUsageSummaryAllAgents` that iterates all agents and merges daily/totals; use it in `loadCostUsageSummaryCached`
- `src/gateway/server-methods/usage.cost-multi-agent.test.ts` — Regression tests for multi-agent cost aggregation
- `CHANGELOG.md` — Add fix entry
**After fix:**
Input: Multi-agent deployment with main (150 tokens) + worker (300 tokens)
Output: `totalTokens: 450` (all agents combined)
## Test plan
- [x] New test: multi-agent cost summary includes all agents (2 test cases)
- [x] All 8 existing session-cost-usage tests pass
- [x] All 5 existing usage handler tests pass
- [x] Lint passes
## Effect on User Experience
**Before:** Dashboard shows incomplete token usage and cost — only the default agent's sessions are counted, making multi-agent deployments appear to use far fewer tokens than reality.
**After:** Dashboard accurately reflects total token usage and cost across all configured agents.
<!-- greptile_comment -->
<h3>Greptile Summary</h3>
Fixed `usage.cost` RPC to aggregate token usage and costs across all agents instead of only the default "main" agent. The implementation adds `loadCostUsageSummaryAllAgents` that iterates all agents via `listAgentsForGateway`, loads per-agent summaries, and merges daily breakdowns and totals. The fix ensures multi-agent deployments accurately report complete usage metrics in the dashboard.
<h3>Confidence Score: 5/5</h3>
- This PR is safe to merge with minimal risk
- The fix is well-isolated, focused on a specific bug, includes comprehensive test coverage with two test cases validating the aggregation logic, and follows existing patterns in the codebase. The implementation correctly merges daily entries by date and accumulates totals across all cost/token fields.
- No files require special attention
<sub>Last reviewed commit: 9663533</sub>
<!-- greptile_other_comments_section -->
<!-- /greptile_comment -->
Most Similar PRs
#13215: fix: pass agentId to loadCostUsageSummary in /usage cost command
by veast · 2026-02-10
86.7%
#20650: fix(gateway): include deleted/reset sessions in usage.cost RPC (#20...
by lailoo · 2026-02-19
83.6%
#15982: fix: pass agentId to resolveSessionFilePath in reply flow (NX-003)
by automagik-genie · 2026-02-14
75.6%
#21679: fix: Mission Control dashboard issues for agent status, stale sessi...
by thejawdoc · 2026-02-20
74.9%
#21651: fix(gateway): token fallback + operator.admin scope superset in pai...
by lan17 · 2026-02-20
74.9%
#13895: fix(usage): exclude cache tokens from context-window accounting
by zerone0x · 2026-02-11
74.3%
#20336: fix(sessions): resolve transcriptPath using agentId when storePath ...
by Limitless2023 · 2026-02-18
74.1%
#11175: fix: add sessions.usage methods to READ_METHODS for authorization
by Yida-Dev · 2026-02-07
73.5%
#17253: fix: propagate lastTurnTotal through usage accumulator for accurate...
by robbyczgw-cla · 2026-02-15
73.5%
#22977: fix(ui): resolve agent names from config in session dropdown
by NikhilGaddam · 2026-02-21
73.2%