[BUG] Cost tracking understates actual spend — sideQuery/classifier tokens and background subagent costs excluded from /cost
Preflight Checklist
- [x] I have searched existing issues and this hasn't been reported yet
- [x] This is a single bug report (please file separate reports for different bugs)
- [x] I am using the latest version of Claude Code
What's Wrong?
The /cost command understates actual Anthropic API spend across four scenarios:
- sideQuery/classifier tokens not tracked — Auto-mode permission checks, memory scanning, session
search, and the permission explainer all use sideQuery(), a separate API call path that deliberately
skips addToTotalSessionCost(). Every auto-mode tool approval makes a real billable API call that
never appears in /cost.
- Background subagent costs not rolled up — Subagents spawned with
run_in_background=truerun in
a separate process with isolated cost state. Their token usage never reaches the parent session's cost
tracker.
- Session cost history silently discarded on ID mismatch —
restoreCostStateForSession()silently
returns undefined when lastSessionId doesn't match, with no warning. Concurrent sessions or any ID
mismatch causes prior cost history to be lost without notice.
- Unknown model pricing falls back silently — Custom models set via
ANTHROPIC_MODELfall back to
the default model's pricing rate. The hasUnknownModelCost flag is set but the displayed dollar
figure is still wrong rather than shown as unknown.
What Should Happen?
- sideQuery calls should be tracked separately (e.g. "internal overhead") or clearly documented with an
estimated overhead note shown in /cost
- Background subagent costs should be aggregated back to the parent session on completion
- Session ID mismatch on restore should display a warning that prior cost history could not be
recovered
- Unknown model cost should display as "unknown" rather than a misleading wrong number
Error Messages/Logs
Steps to Reproduce
For issue #1 (classifier tokens):
- Run claude with --auto flag
- Give it a task that triggers many tool calls
- Compare
/costoutput against your Anthropic usage dashboard — actual spend will be higher
For issue #2 (background subagents):
- Use the Agent tool with run_in_background=true
- Let the subagent complete
- Check
/costin parent session — subagent token usage is absent
For issue #3 (session ID mismatch):
- Open two Claude Code sessions in the same project directory concurrently
- Let both accumulate costs
- Close one and resume — prior cost data from the overwritten session is silently gone
For issue #4 (unknown model):
- Set ANTHROPIC_MODEL=any-unknown-model-name
- Run a query and check
/cost - Cost shown uses default model rates with no indication of which rate was applied
Claude Model
None
Is this a regression?
No, this never worked
Last Working Version
_No response_
Claude Code Version
2.1.92
Platform
Anthropic API
Operating System
macOS
Terminal/Shell
Terminal.app (macOS)
Additional Information
Root cause references (from source):
- src/utils/permissions/permissions.ts:766 — explicit comment: "sideQuery does NOT call addToTotalSessionCost"
- src/utils/sideQuery.ts — no addToTotalSessionCost call anywhere in the file
- src/cost-tracker.ts:93 — silent undefined return on session ID mismatch
- src/utils/modelCost.ts:158 — unknown model falls back to default model pricing
This issue has 2 comments on GitHub. Read the full discussion on GitHub ↗