mirror of
https://github.com/crewAIInc/crewAI.git
synced 2026-07-01 05:08:12 +00:00
`flow.kickoff().token_usage` only returned the last @listen method's `CrewOutput.token_usage`, so multi-crew flows under-reported by a factor of N and bare `LLM.call(...)` invocations were ignored entirely. SDK totals therefore disagreed with the CrewAI Enterprise UI (Wharf), which aggregates every LLM span. Add a new `flow.usage_metrics` property that wires an `LLMCallCompletedEvent` listener for the duration of `kickoff_async`. The listener scopes to the active flow via the `current_flow_id` contextvar (the event bus copies the context at emit time, so the value the handler sees is the one set when the LLM call fired) and normalizes the provider-specific usage dict into a `UsageMetrics`. This covers every LLM call inside the flow — crew-led, tool-led, and bare `LLM.call(...)` — and matches the UI totals 1:1.