Skip to content

fix(analytics): include sub-agent transcripts in token usage extraction#358

Merged
codemie-ai merged 5 commits into
codemie-ai:mainfrom
pigorv:fix/analytics-subagent-token-usage
Jun 15, 2026
Merged

fix(analytics): include sub-agent transcripts in token usage extraction#358
codemie-ai merged 5 commits into
codemie-ai:mainfrom
pigorv:fix/analytics-subagent-token-usage

Conversation

@pigorv

@pigorv pigorv commented Jun 15, 2026

Copy link
Copy Markdown
Contributor

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

  • Include sub-agent transcripts in usage extraction — new allMessageArrays() helper in usage-readers.ts yields the main transcript followed by each parsed sub-agent transcript; extractClaudeUsageRecords now iterates all of them instead of only the main messages array.
  • Order merged records chronologically — when every record is timed, records are sorted by timestamp (the same condition buildCostSeries uses 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).
  • Tests — lock the sub-agent usage-folding and ordering invariants in cost-enricher.test.ts and usage-readers.test.ts.

Impact

  • Sessions that dispatch sub-agents now report complete token usage and cost instead of undercounting.
  • The cumulative cost/token series stays monotonic in time for these merged sessions.
  • Dedup behavior is unchanged — replayed messages (same message.id + requestId) are still deduped by key by callers.
  • No API, CLI, or config changes; single-transcript sessions are unaffected (the chronological sort is a no-op when there is nothing to merge).

Checklist

  • Self-reviewed
  • Manual testing performed
  • Documentation updated (if needed)
  • No breaking changes (or clearly documented)

pigorv and others added 3 commits June 12, 2026 22:07
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>
@pigorv pigorv marked this pull request as ready for review June 15, 2026 09:41
@pigorv pigorv changed the title fix(analytics): include sub-agent transcripts in token usage extraction fix(analytics): include sub-agent transcripts in token usage extraction Jun 15, 2026
pigorv and others added 2 commits June 15, 2026 13:26
@codemie-ai codemie-ai merged commit baf00e4 into codemie-ai:main Jun 15, 2026
5 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants