Skip to content

fix(token-usage): count all generations per LLM call and track subagent call count#3088

Open
64johnlee wants to merge 1 commit into
bytedance:mainfrom
64johnlee:fix/token-usage-accounting-gaps
Open

fix(token-usage): count all generations per LLM call and track subagent call count#3088
64johnlee wants to merge 1 commit into
bytedance:mainfrom
64johnlee:fix/token-usage-accounting-gaps

Conversation

@64johnlee
Copy link
Copy Markdown
Contributor

Closes #2874

Problem

Two gaps in token-usage accounting:

  1. When an LLM call produces multiple completions (n > 1), TokenCollector only counted the first generation's tokens, silently undercounting the others.
  2. Subagent invocations were not counted as a distinct metric, making it impossible to distinguish runs with many subagent calls from single-agent runs with high token usage.

Fix

  • deerflow/subagents/token_collector.py: iterate all generations in the LLM result instead of only result.generations[0]; increment a subagent_calls counter on each invocation
  • deerflow/runtime/journal.py: expose subagent_calls in the journal schema so it is persisted and queryable

Tests

test_subagent_token_collector.py — added:

  • test_counts_all_generations — multi-generation result is fully counted
  • test_subagent_call_counter_increments — counter goes up per call

test_run_journal.py — added:

  • test_journal_includes_subagent_calls — journal serializes the new field

🤖 Generated with Claude Code

…nt call count

SubagentTokenCollector.on_llm_end accumulated only the first valid generation
per run_id (early return) and discarded subsequent ones. Refactored to sum
input/output/total tokens across all generations before appending one record,
so multi-generation responses are not under-counted.

RunJournal.record_external_llm_usage_records never incremented _llm_call_count,
causing get_completion_data() to exclude subagent LLM calls from the reported
call count. Added the missing increment.

Fixes bytedance#2874.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
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.

token-usage: remaining accounting gaps after initial system

1 participant