← Back to PRs

#20603: fix(gateway): scan all agents in usage.cost RPC (#20558)

by lailoo open 2026-02-19 04:08 View on GitHub →
gateway size: M experienced-contributor
## 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