#13215: fix: pass agentId to loadCostUsageSummary in /usage cost command
stale
Cluster:
Usage Cost Enhancements
## Problem
When using named agents (not `main`), the `/usage cost` command shows **$0 for all time ranges** because `loadCostUsageSummary()` defaults to `agentId="main"` and scans `~/.openclaw/agents/main/sessions/` instead of the correct agent folder.
## Root Cause
In `src/auto-reply/reply/commands-session.ts`, the `/usage cost` handler calls:
```typescript
const summary = await loadCostUsageSummary({ days: 30, config: params.cfg });
```
Without passing `agentId`, `loadCostUsageSummary` defaults to `main` (line 399 in `src/infra/session-cost-usage.ts`):
```typescript
const sessionsDir = resolveSessionTranscriptsDirForAgent(params?.agentId);
```
## Solution
Resolve `agentId` from `params.sessionKey` before calling `loadCostUsageSummary`:
```typescript
const agentId = params.sessionKey
? resolveAgentIdFromSessionKey(params.sessionKey)
: undefined;
const summary = await loadCostUsageSummary({ days: 30, config: params.cfg, agentId });
```
## Impact
✅ **Fixes**:
- `/usage cost` now shows correct totals for non-main agents
- 1-day, 7-day, 30-day ranges all work correctly
- Dashboard cost panel displays accurate usage
✅ **Unchanged**:
- Per-session cost already worked (uses direct file path)
- Global `usage.cost` gateway handler intentionally unchanged (may aggregate across all agents)
## Testing
- [x] Code follows project style
- [ ] Tested with custom agent name (requires non-main agent setup)
- [ ] Verified cost totals match transcript JSONL files
## References
Fixes #13173
<!-- greptile_comment -->
<h2>Greptile Overview</h2>
<h3>Greptile Summary</h3>
This PR fixes `/usage cost` for non-`main` agents by resolving the agent id from `params.sessionKey` and passing it into `loadCostUsageSummary()`, so the cost scan reads from the correct agent’s sessions directory. The change is localized to the command handler and aligns with the existing default behavior in `loadCostUsageSummary` (still defaults to `main` when `agentId` is omitted/undefined), keeping the gateway/global usage handler behavior unchanged.
<h3>Confidence Score: 5/5</h3>
- This PR is safe to merge with minimal risk.
- The change is small, localized, and uses existing sessionKey parsing/normalization utilities; it preserves default behavior when sessionKey is absent and does not affect other `loadCostUsageSummary` call sites.
- No files require special attention
<!-- greptile_other_comments_section -->
<sub>(5/5) You can turn off certain types of comments like style [here](https://app.greptile.com/review/github)!</sub>
<!-- /greptile_comment -->
Most Similar PRs
#20603: fix(gateway): scan all agents in usage.cost RPC (#20558)
by lailoo · 2026-02-19
86.7%
#15982: fix: pass agentId to resolveSessionFilePath in reply flow (NX-003)
by automagik-genie · 2026-02-14
82.1%
#15792: fix: pass agentId to resolveSessionFilePath in additional call sites
by MisterGuy420 · 2026-02-13
81.8%
#20650: fix(gateway): include deleted/reset sessions in usage.cost RPC (#20...
by lailoo · 2026-02-19
80.9%
#20336: fix(sessions): resolve transcriptPath using agentId when storePath ...
by Limitless2023 · 2026-02-18
80.9%
#15852: fix: pass agentId when resolving IRC session paths
by MisterGuy420 · 2026-02-14
79.0%
#22977: fix(ui): resolve agent names from config in session dropdown
by NikhilGaddam · 2026-02-21
78.0%
#19177: fix: use parseAgentSessionKey instead of fragile split pattern
by El-Patronum · 2026-02-17
77.6%
#7301: fix(hooks): use resolveAgentIdFromSessionKey instead of split(":")[0]
by tsukhani · 2026-02-02
77.3%
#21646: fix(cron): pass agentDir to runEmbeddedPiAgent for correct auth res...
by zhangjunmengyang · 2026-02-20
77.3%