fix(token-usage): count all generations per LLM call and track subagent call count#3088
Open
64johnlee wants to merge 1 commit into
Open
fix(token-usage): count all generations per LLM call and track subagent call count#308864johnlee wants to merge 1 commit into
64johnlee wants to merge 1 commit into
Conversation
…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>
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.
Closes #2874
Problem
Two gaps in token-usage accounting:
TokenCollectoronly counted the first generation's tokens, silently undercounting the others.Fix
deerflow/subagents/token_collector.py: iterate all generations in the LLM result instead of onlyresult.generations[0]; increment asubagent_callscounter on each invocationdeerflow/runtime/journal.py: exposesubagent_callsin the journal schema so it is persisted and queryableTests
test_subagent_token_collector.py— added:test_counts_all_generations— multi-generation result is fully countedtest_subagent_call_counter_increments— counter goes up per calltest_run_journal.py— added:test_journal_includes_subagent_calls— journal serializes the new field🤖 Generated with Claude Code