fix(analytics): include sub-agent transcripts in token usage extraction#358
Merged
codemie-ai merged 5 commits intoJun 15, 2026
Merged
Conversation
Sub-agent runs (Task/Agent dispatches parsed into parsed.subagents) were invisible to the cost pipeline: extractClaudeUsageRecords read only the main transcript, so session and report totals undercounted every session that dispatched agents. Merge all sub-agent message arrays at the extraction choke point; existing (message.id, requestId) dedup guards against double-written responses. Generated with AI Co-Authored-By: codemie-ai <codemie.ai@gmail.com>
Merged main + sub-agent records feed buildCostSeries, which assumes record order is time order. Sort by timestamp when every record is timed (mirroring the series' useTs rule); fall back to concatenation order otherwise, matching the ordinal-axis fallback. Generated with AI Co-Authored-By: codemie-ai <codemie.ai@gmail.com>
…cher Integration guards: sub-agent tokens fold into the session total with the growth-series endpoint equal to it, and sub-agent records participate in cross-session response dedup. Generated with AI Co-Authored-By: codemie-ai <codemie.ai@gmail.com>
Generated with AI Co-Authored-By: codemie-ai <codemie.ai@gmail.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Token usage extraction for Claude sessions only read the main transcript, so any session that dispatched Task/Agent sub-agents had the sub-agents' token usage silently dropped — undercounting both token totals and cost for those sessions. This PR folds every sub-agent transcript into the owning session's usage records and keeps the merged records in chronological order so the per-session cost/token time-series stays monotonic.
Changes
allMessageArrays()helper inusage-readers.tsyields the main transcript followed by each parsed sub-agent transcript;extractClaudeUsageRecordsnow iterates all of them instead of only the mainmessagesarray.buildCostSeriesuses for its real-time axis); when any record is untimed, ordering falls back to concatenation order (main transcript first, then sub-agent transcripts in discovery order).cost-enricher.test.tsandusage-readers.test.ts.Impact
message.id+requestId) are still deduped bykeyby callers.Checklist