feat: Per-Agent Token Tracking for Cost Attribution #40
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
Implements per-agent token tracking to provide granular visibility into token consumption by each CrewAI agent in the workflow.
Closes #39
Changes Made
1. Updated
crew.pycrew.kickoff()completes, capture token usage from each agent's LLM usingagent.llm.get_token_usage_summary()per_agent_metricsdictionary along with existing return values2. Updated
workflow_service.pyper_agent_metricsreturn value fromrun_crew()token_usagefield inWorkflowMetricsfor historical persistence3. Added Documentation
PER_AGENT_TOKEN_TRACKING_GUIDE.md)Benefits
✅ Cost Attribution: Know exactly which agent consumes the most tokens
✅ Optimization Opportunities: Identify agents that need context pruning
✅ Debugging: Track token usage anomalies per agent
✅ Analytics: Historical trends for each agent's efficiency
Example Output
After this implementation, logs will show:
Technical Approach
This implementation follows the same pattern used in CrewAI PR #4132:
get_llm()in agents.py)get_token_usage_summary()WorkflowMetrics.token_usagefieldTesting
Breaking Changes
None - This is a purely additive feature that enhances existing metrics tracking.