fix(output): expose token usage under both names on agent and crew results#6507
fix(output): expose token usage under both names on agent and crew results#6507joaomdmoura wants to merge 1 commit into
Conversation
…sults Agent.kickoff() returned LiteAgentOutput with a plain dict at .usage_metrics and no token_usage attribute, while Crew.kickoff() returned CrewOutput with a UsageMetrics object at .token_usage and no usage_metrics attribute — so a usage accessor written for one path raised AttributeError on the other, and every consumer had to duck-type both shapes. Give both result types both surfaces, each name with one consistent shape everywhere: .token_usage is a UsageMetrics object and .usage_metrics is a plain dict, on both LiteAgentOutput and CrewOutput. Added as read-only properties, so existing fields, serialization, and constructors are unchanged. Fixes EPD-178. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
📝 WalkthroughWalkthrough
ChangesUsage metrics parity
Suggested reviewers: 🚥 Pre-merge checks | ✅ 4 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (4 passed)
✨ Finishing Touches📝 Generate docstrings
🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
There was a problem hiding this comment.
🧹 Nitpick comments (1)
lib/crewai/tests/test_usage_shape_parity.py (1)
1-1: 📐 Maintainability & Code Quality | 🔵 Trivial | 💤 Low valueConsider removing
# mypy: ignore-errorsor scoping it more narrowly.The PR objectives mention passing mypy checks, but this directive disables mypy for the entire test file. If the goal is to verify type safety of the new accessors, consider either removing the directive or replacing it with targeted
# type: ignore[code]comments on specific lines that need it.🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the rest with a brief reason, keep changes minimal, and validate. In `@lib/crewai/tests/test_usage_shape_parity.py` at line 1, Remove the file-wide `# mypy: ignore-errors` directive from `test_usage_shape_parity.py`; resolve any resulting type errors, or replace only unavoidable cases with targeted `# type: ignore[code]` comments on the specific affected lines in the parity tests and accessor checks.
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
Nitpick comments:
In `@lib/crewai/tests/test_usage_shape_parity.py`:
- Line 1: Remove the file-wide `# mypy: ignore-errors` directive from
`test_usage_shape_parity.py`; resolve any resulting type errors, or replace only
unavoidable cases with targeted `# type: ignore[code]` comments on the specific
affected lines in the parity tests and accessor checks.
ℹ️ Review info
⚙️ Run configuration
Configuration used: Organization UI
Review profile: CHILL
Plan: Pro Plus
Run ID: 862001f0-bf91-44a2-89b7-015c2487870c
📒 Files selected for processing (3)
lib/crewai/src/crewai/crews/crew_output.pylib/crewai/src/crewai/lite_agent_output.pylib/crewai/tests/test_usage_shape_parity.py
Summary
Fixes EPD-178: the same conceptual data — token usage — was exposed in different shapes and attribute names per code path.
Agent.kickoff()→LiteAgentOutputwith a plain dict at.usage_metricsand notoken_usageattribute;Crew.kickoff()→CrewOutputwith aUsageMetricsobject at.token_usageand nousage_metricsattribute. A single accessor written for one path raisedAttributeErroron the other, forcing every metrics/observability integration to duck-type both shapes.Fix
Both result types now expose both surfaces, and each name has one consistent shape everywhere:
.token_usage→UsageMetricsobject (onCrewOutputas before; new read-only property onLiteAgentOutput, zeroed when no usage was captured soresult.token_usage.prompt_tokensnever crashes)..usage_metrics→ plain dict (onLiteAgentOutputas before; new read-only property onCrewOutputviatoken_usage.model_dump()).Fully backward compatible: existing fields, constructors, and serialization are untouched — the aliases are properties, so
model_dump()output doesn't change. Field descriptions cross-reference the twin surface, withtoken_usage/UsageMetricsas the canonical form going forward.Not in scope (possible follow-up):
Flow.usage_metrics(a property on the flow instance, not a result object) returns aUsageMetricsobject under the dict-shaped name — worth aligning if/when flows grow a result wrapper.Testing
tests/test_usage_shape_parity.py: unit parity tests for both classes (including the zeroed-when-absent case and key-set parity) and an offline end-to-end mirroring the ticket repro — one object-shaped and one dict-shaped accessor exercised against bothAgent.kickoff()andCrew.kickoff()results.tests/agents/test_lite_agent.py(29), output/usage-related tests intest_crew.py(14), ruff, and mypy.🤖 Generated with Claude Code
Note
Low Risk
Additive read-only properties on result models with no changes to constructors or
model_dump(); behavior is covered by new regression tests.Overview
Aligns token usage access on
Agent.kickoff()(LiteAgentOutput) andCrew.kickoff()(CrewOutput) so the same observability code works on both paths (EPD-178).CrewOutputgains a read-onlyusage_metricsproperty that returnstoken_usage.model_dump().LiteAgentOutputgains a read-onlytoken_usageproperty that builds aUsageMetricsfrom the stored dict, or returns empty metrics when usage was not captured. Field descriptions now cross-reference the twin surface. Stored fields and serialization are unchanged—aliases are properties only.Adds
tests/test_usage_shape_parity.pywith unit parity checks and an offline end-to-end test that exercises object- and dict-shaped accessors on both kickoff paths.Reviewed by Cursor Bugbot for commit 2c13b72. Bugbot is set up for automated code reviews on this repo. Configure here.