Skip to content

fix(crew): deduplicate manager agent token usage accounting - #4934

Closed
wishhyt wants to merge 1 commit into
crewAIInc:mainfrom
wishhyt:fix/crew-manager-token-double-count
Closed

fix(crew): deduplicate manager agent token usage accounting#4934
wishhyt wants to merge 1 commit into
crewAIInc:mainfrom
wishhyt:fix/crew-manager-token-double-count

Conversation

@wishhyt

@wishhyt wishhyt commented Mar 18, 2026

Copy link
Copy Markdown
Contributor

Summary

  • calculate_usage_metrics() counted the manager agent's token usage twice: once via _token_process.get_summary() and again via llm.get_token_usage_summary(), because the two checks were independent if blocks instead of mutually exclusive branches.
  • For regular agents, the code correctly uses if/else to choose one path. The manager agent path was missing the else.

Changes

Restructured to use the same if isinstance(llm, BaseLLM) / else pattern as regular agents.

Test plan

Run a hierarchical crew and verify usage_metrics values are not inflated


Note

Low Risk
Low risk: small, localized change to usage metrics aggregation logic that only affects reported token counts for hierarchical crews.

Overview
Fixes Crew.calculate_usage_metrics() so the manager agent’s token usage is only added once by making the BaseLLM.get_token_usage_summary() path and the _token_process.get_summary() fallback mutually exclusive (matching the regular agent logic). This prevents inflated usage_metrics/token totals when both mechanisms are present on the manager agent.

Written by Cursor Bugbot for commit d2334c2. This will update automatically on new commits. Configure here.

`calculate_usage_metrics` counted the manager agent's token usage
twice: once via `_token_process.get_summary()` and again via
`llm.get_token_usage_summary()`, because the two checks were
independent if-blocks instead of mutually exclusive branches.

Restructure to use the same if/else pattern as regular agents:
prefer `BaseLLM.get_token_usage_summary()` when available,
otherwise fall back to `_token_process`.

Made-with: Cursor
@github-actions

github-actions Bot commented May 2, 2026

Copy link
Copy Markdown
Contributor

This PR is stale because it has been open for 45 days with no activity.

@Vidit-Ostwal

Copy link
Copy Markdown
Contributor

Closing this. The manager if/else only stops one double-count while still reconstructing usage_metrics from LLM lifetime totals (and _token_process). That source is wrong for shared instances, leftover usage from a prior run, and the manager path.

Direction: per-agent UsageMetrics via snapshot/delta around each agent run, then calculate_usage_metrics() sums those. Kickoff already does the snapshot for LiteAgentOutput; persist it on the agent (not TokenProcess) and include the manager the same way.

Follow the review on #7260 for that rework. This PR is not the shape we want to land.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants