diff --git a/lib/crewai/src/crewai/crew.py b/lib/crewai/src/crewai/crew.py index 2c7f583b98..130e9a201a 100644 --- a/lib/crewai/src/crewai/crew.py +++ b/lib/crewai/src/crewai/crew.py @@ -203,6 +203,10 @@ class Crew(FlowTrackable, BaseModel): default=None, description="Metrics for the LLM usage during all tasks execution.", ) + workflow_token_metrics: Any | None = Field( + default=None, + description="Detailed per-agent and per-task token metrics.", + ) manager_llm: str | InstanceOf[BaseLLM] | Any | None = Field( description="Language model that will run the agent.", default=None ) @@ -944,17 +948,32 @@ async def _aexecute_tasks( continue if task.async_execution: + # Capture token usage before async task execution + tokens_before = self._get_agent_token_usage(exec_data.agent) + context = self._get_context( task, [last_sync_output] if last_sync_output else [] ) - async_task = asyncio.create_task( - task.aexecute_sync( - agent=exec_data.agent, - context=context, - tools=exec_data.tools, + + # Wrap task execution to capture tokens immediately after completion + # Use default arguments to capture values at definition time (avoid late-binding closure issue) + async def _wrapped_task_execution( + _task=task, + _exec_data=exec_data, + _context=context + ): + result = await _task.aexecute_sync( + agent=_exec_data.agent, + context=_context, + tools=_exec_data.tools, ) - ) - pending_tasks.append((task, async_task, task_index)) + # Capture tokens immediately after task completes + # This reduces (but doesn't eliminate) race conditions + tokens_after = self._get_agent_token_usage(_exec_data.agent) + return result, tokens_after + + async_task = asyncio.create_task(_wrapped_task_execution()) + pending_tasks.append((task, async_task, task_index, exec_data.agent, tokens_before)) else: if pending_tasks: task_outputs = await self._aprocess_async_tasks( @@ -962,12 +981,22 @@ async def _aexecute_tasks( ) pending_tasks.clear() + # Capture token usage before task execution + tokens_before = self._get_agent_token_usage(exec_data.agent) + context = self._get_context(task, task_outputs) task_output = await task.aexecute_sync( agent=exec_data.agent, context=context, tools=exec_data.tools, ) + + # Capture token usage after task execution and attach to task output + tokens_after = self._get_agent_token_usage(exec_data.agent) + task_output = self._attach_task_token_metrics( + task_output, task, exec_data.agent, tokens_before, tokens_after + ) + task_outputs.append(task_output) self._process_task_result(task, task_output) self._store_execution_log(task, task_output, task_index, was_replayed) @@ -981,7 +1010,7 @@ async def _ahandle_conditional_task( self, task: ConditionalTask, task_outputs: list[TaskOutput], - pending_tasks: list[tuple[Task, asyncio.Task[TaskOutput], int]], + pending_tasks: list[tuple[Task, asyncio.Task[tuple[TaskOutput, Any]], int, Any, Any]], task_index: int, was_replayed: bool, ) -> TaskOutput | None: @@ -996,13 +1025,20 @@ async def _ahandle_conditional_task( async def _aprocess_async_tasks( self, - pending_tasks: list[tuple[Task, asyncio.Task[TaskOutput], int]], + pending_tasks: list[tuple[Task, asyncio.Task[tuple[TaskOutput, Any]], int, Any, Any]], was_replayed: bool = False, ) -> list[TaskOutput]: """Process pending async tasks and return their outputs.""" task_outputs: list[TaskOutput] = [] - for future_task, async_task, task_index in pending_tasks: - task_output = await async_task + for future_task, async_task, task_index, agent, tokens_before in pending_tasks: + # Unwrap the result which includes both output and tokens_after + task_output, tokens_after = await async_task + + # Attach token metrics using the captured tokens_after + task_output = self._attach_task_token_metrics( + task_output, future_task, agent, tokens_before, tokens_after + ) + task_outputs.append(task_output) self._process_task_result(future_task, task_output) self._store_execution_log( @@ -1141,26 +1177,64 @@ def _execute_tasks( continue if task.async_execution: + # Capture token usage before async task execution + tokens_before = self._get_agent_token_usage(exec_data.agent) + context = self._get_context( task, [last_sync_output] if last_sync_output else [] ) - future = task.execute_async( - agent=exec_data.agent, - context=context, - tools=exec_data.tools, - ) - futures.append((task, future, task_index)) + + # Create a wrapper that captures tokens immediately after task completion + # to avoid race conditions with concurrent tasks from the same agent + # Use default arguments to capture values at definition time (avoid late-binding) + def _wrapped_sync_task_execution( + _task=task, + _exec_data=exec_data, + _context=context, + _self=self + ): + result = _task.execute_sync( + agent=_exec_data.agent, + context=_context, + tools=_exec_data.tools, + ) + # Capture tokens immediately after task completes within the thread + tokens_after = _self._get_agent_token_usage(_exec_data.agent) + return result, tokens_after + + # Submit to thread pool and get future + future: Future[tuple[TaskOutput, Any]] = Future() + def _run_in_thread(): + try: + result = _wrapped_sync_task_execution() + future.set_result(result) + except Exception as e: + future.set_exception(e) + + import threading + threading.Thread(daemon=True, target=_run_in_thread).start() + futures.append((task, future, task_index, exec_data.agent, tokens_before)) else: if futures: task_outputs = self._process_async_tasks(futures, was_replayed) futures.clear() + # Capture token usage before task execution + tokens_before = self._get_agent_token_usage(exec_data.agent) + context = self._get_context(task, task_outputs) task_output = task.execute_sync( agent=exec_data.agent, context=context, tools=exec_data.tools, ) + + # Capture token usage after task execution and attach to task output + tokens_after = self._get_agent_token_usage(exec_data.agent) + task_output = self._attach_task_token_metrics( + task_output, task, exec_data.agent, tokens_before, tokens_after + ) + task_outputs.append(task_output) self._process_task_result(task, task_output) self._store_execution_log(task, task_output, task_index, was_replayed) @@ -1174,7 +1248,7 @@ def _handle_conditional_task( self, task: ConditionalTask, task_outputs: list[TaskOutput], - futures: list[tuple[Task, Future[TaskOutput], int]], + futures: list[tuple[Task, Future[tuple[TaskOutput, Any]], int, Any, Any]], task_index: int, was_replayed: bool, ) -> TaskOutput | None: @@ -1401,16 +1475,30 @@ def _create_crew_output(self, task_outputs: list[TaskOutput]) -> CrewOutput: json_dict=final_task_output.json_dict, tasks_output=task_outputs, token_usage=self.token_usage, + token_metrics=getattr(self, 'workflow_token_metrics', None), ) def _process_async_tasks( self, - futures: list[tuple[Task, Future[TaskOutput], int]], + futures: list[tuple[Task, Future[tuple[TaskOutput, Any]], int, Any, Any]], was_replayed: bool = False, ) -> list[TaskOutput]: + """Process async tasks executed via threading. + + Each future returns a tuple of (TaskOutput, tokens_after) where tokens_after + was captured immediately after task completion within the thread to avoid + race conditions. + """ task_outputs: list[TaskOutput] = [] - for future_task, future, task_index in futures: - task_output = future.result() + for future_task, future, task_index, agent, tokens_before in futures: + # Unwrap the result which includes both output and tokens_after + task_output, tokens_after = future.result() + + # Attach token metrics using the captured tokens_after + task_output = self._attach_task_token_metrics( + task_output, future_task, agent, tokens_before, tokens_after + ) + task_outputs.append(task_output) self._process_task_result(future_task, task_output) self._store_execution_log( @@ -1616,12 +1704,83 @@ def _finish_execution(self, final_string_output: str) -> None: def calculate_usage_metrics(self) -> UsageMetrics: """Calculates and returns the usage metrics.""" + from crewai.types.usage_metrics import ( + AgentTokenMetrics, + WorkflowTokenMetrics, + ) + total_usage_metrics = UsageMetrics() - + + # Preserve existing workflow_token_metrics if it exists (has per_task data) + if hasattr(self, 'workflow_token_metrics') and self.workflow_token_metrics: + workflow_metrics = self.workflow_token_metrics + else: + workflow_metrics = WorkflowTokenMetrics() + + # Build per-agent metrics from per-task data (more accurate) + # This avoids the cumulative token issue where all agents show the same total + # Key by agent_id to handle multiple agents with the same role + agent_token_sums = {} + agent_info_map = {} # Map agent_id to (agent_name, agent_id) + + # First, build a map of all agents by their ID + for agent in self.agents: + agent_role = getattr(agent, 'role', 'Unknown Agent') + agent_id = str(getattr(agent, 'id', '')) + agent_info_map[agent_id] = (agent_role, agent_id) + + if workflow_metrics.per_task: + # Sum up tokens for each agent from their tasks + # We need to find which agent_id corresponds to each task's agent_name + for task_name, task_metrics in workflow_metrics.per_task.items(): + agent_name = task_metrics.agent_name + # Find the agent_id for this agent_name from agent_info_map + # For now, we'll use the agent_name as a temporary key but this needs improvement + # TODO: Store agent_id in TaskTokenMetrics to avoid this lookup + matching_agent_ids = [aid for aid, (name, _) in agent_info_map.items() if name == agent_name] + + # Use the first matching agent_id (limitation: can't distinguish between same-role agents) + # This is better than nothing but ideally we'd store agent_id in TaskTokenMetrics + for agent_id in matching_agent_ids: + if agent_id not in agent_token_sums: + agent_token_sums[agent_id] = { + 'total_tokens': 0, + 'prompt_tokens': 0, + 'cached_prompt_tokens': 0, + 'completion_tokens': 0, + 'successful_requests': 0 + } + # Only add to the first matching agent (this is the limitation) + agent_token_sums[agent_id]['total_tokens'] += task_metrics.total_tokens + agent_token_sums[agent_id]['prompt_tokens'] += task_metrics.prompt_tokens + agent_token_sums[agent_id]['cached_prompt_tokens'] += task_metrics.cached_prompt_tokens + agent_token_sums[agent_id]['completion_tokens'] += task_metrics.completion_tokens + agent_token_sums[agent_id]['successful_requests'] += task_metrics.successful_requests + break # Only add to first matching agent + + # Create per-agent metrics from the summed task data, keyed by agent_id for agent in self.agents: + agent_role = getattr(agent, 'role', 'Unknown Agent') + agent_id = str(getattr(agent, 'id', '')) + + if agent_id in agent_token_sums: + # Use accurate per-task summed data + sums = agent_token_sums[agent_id] + agent_metrics = AgentTokenMetrics( + agent_name=agent_role, + agent_id=agent_id, + total_tokens=sums['total_tokens'], + prompt_tokens=sums['prompt_tokens'], + cached_prompt_tokens=sums['cached_prompt_tokens'], + completion_tokens=sums['completion_tokens'], + successful_requests=sums['successful_requests'] + ) + # Key by agent_id to avoid collision for agents with same role + workflow_metrics.per_agent[agent_id] = agent_metrics + + # Still get total usage for overall metrics if isinstance(agent.llm, BaseLLM): llm_usage = agent.llm.get_token_usage_summary() - total_usage_metrics.add_usage_metrics(llm_usage) else: # fallback litellm @@ -1629,22 +1788,66 @@ def calculate_usage_metrics(self) -> UsageMetrics: token_sum = agent._token_process.get_summary() total_usage_metrics.add_usage_metrics(token_sum) - if self.manager_agent and hasattr(self.manager_agent, "_token_process"): - token_sum = self.manager_agent._token_process.get_summary() - total_usage_metrics.add_usage_metrics(token_sum) - - if ( - self.manager_agent - and hasattr(self.manager_agent, "llm") - and hasattr(self.manager_agent.llm, "get_token_usage_summary") - ): - if isinstance(self.manager_agent.llm, BaseLLM): - llm_usage = self.manager_agent.llm.get_token_usage_summary() - else: - llm_usage = self.manager_agent.llm._token_process.get_summary() + if self.manager_agent: + manager_role = getattr(self.manager_agent, 'role', 'Manager Agent') + manager_id = str(getattr(self.manager_agent, 'id', '')) + + if hasattr(self.manager_agent, "_token_process"): + token_sum = self.manager_agent._token_process.get_summary() + total_usage_metrics.add_usage_metrics(token_sum) + + # Create per-agent metrics for manager + manager_metrics = AgentTokenMetrics( + agent_name=manager_role, + agent_id=manager_id, + total_tokens=token_sum.total_tokens, + prompt_tokens=token_sum.prompt_tokens, + cached_prompt_tokens=token_sum.cached_prompt_tokens, + completion_tokens=token_sum.completion_tokens, + successful_requests=token_sum.successful_requests + ) + # Key by manager_id to be consistent with regular agents + workflow_metrics.per_agent[manager_id] = manager_metrics - total_usage_metrics.add_usage_metrics(llm_usage) + if ( + hasattr(self.manager_agent, "llm") + and hasattr(self.manager_agent.llm, "get_token_usage_summary") + ): + if isinstance(self.manager_agent.llm, BaseLLM): + llm_usage = self.manager_agent.llm.get_token_usage_summary() + else: + llm_usage = self.manager_agent.llm._token_process.get_summary() + total_usage_metrics.add_usage_metrics(llm_usage) + + # Update or create manager metrics (key by manager_id for consistency) + if manager_id in workflow_metrics.per_agent: + workflow_metrics.per_agent[manager_id].total_tokens += llm_usage.total_tokens + workflow_metrics.per_agent[manager_id].prompt_tokens += llm_usage.prompt_tokens + workflow_metrics.per_agent[manager_id].cached_prompt_tokens += llm_usage.cached_prompt_tokens + workflow_metrics.per_agent[manager_id].completion_tokens += llm_usage.completion_tokens + workflow_metrics.per_agent[manager_id].successful_requests += llm_usage.successful_requests + else: + manager_metrics = AgentTokenMetrics( + agent_name=manager_role, + agent_id=manager_id, + total_tokens=llm_usage.total_tokens, + prompt_tokens=llm_usage.prompt_tokens, + cached_prompt_tokens=llm_usage.cached_prompt_tokens, + completion_tokens=llm_usage.completion_tokens, + successful_requests=llm_usage.successful_requests + ) + workflow_metrics.per_agent[manager_id] = manager_metrics + + # Set workflow-level totals + workflow_metrics.total_tokens = total_usage_metrics.total_tokens + workflow_metrics.prompt_tokens = total_usage_metrics.prompt_tokens + workflow_metrics.cached_prompt_tokens = total_usage_metrics.cached_prompt_tokens + workflow_metrics.completion_tokens = total_usage_metrics.completion_tokens + workflow_metrics.successful_requests = total_usage_metrics.successful_requests + + # Store workflow metrics (preserving per_task data) + self.workflow_token_metrics = workflow_metrics self.usage_metrics = total_usage_metrics return total_usage_metrics @@ -1918,3 +2121,56 @@ def _show_tracing_disabled_message() -> None: padding=(1, 2), ) console.print(panel) + + def _get_agent_token_usage(self, agent: BaseAgent | None) -> UsageMetrics: + """Get current token usage for an agent.""" + if not agent: + return UsageMetrics() + + if isinstance(agent.llm, BaseLLM): + return agent.llm.get_token_usage_summary() + elif hasattr(agent, "_token_process"): + return agent._token_process.get_summary() + + return UsageMetrics() + + def _attach_task_token_metrics( + self, + task_output: TaskOutput, + task: Task, + agent: BaseAgent | None, + tokens_before: UsageMetrics, + tokens_after: UsageMetrics + ) -> TaskOutput: + """Attach per-task token metrics to the task output.""" + from crewai.types.usage_metrics import TaskTokenMetrics + + if not agent: + return task_output + + # Calculate the delta (tokens used by this specific task) + task_tokens = TaskTokenMetrics( + task_name=getattr(task, 'name', None) or task.description[:50], + task_id=str(getattr(task, 'id', '')), + agent_name=getattr(agent, 'role', 'Unknown Agent'), + total_tokens=tokens_after.total_tokens - tokens_before.total_tokens, + prompt_tokens=tokens_after.prompt_tokens - tokens_before.prompt_tokens, + cached_prompt_tokens=tokens_after.cached_prompt_tokens - tokens_before.cached_prompt_tokens, + completion_tokens=tokens_after.completion_tokens - tokens_before.completion_tokens, + successful_requests=tokens_after.successful_requests - tokens_before.successful_requests + ) + + # Attach to task output + task_output.usage_metrics = task_tokens + + # Store in workflow metrics + if not hasattr(self, 'workflow_token_metrics') or self.workflow_token_metrics is None: + from crewai.types.usage_metrics import WorkflowTokenMetrics + self.workflow_token_metrics = WorkflowTokenMetrics() + + # Use task_id in the key to prevent collision when multiple tasks have the same name + task_key = f"{task_tokens.task_id}_{task_tokens.task_name}_{task_tokens.agent_name}" + self.workflow_token_metrics.per_task[task_key] = task_tokens + + return task_output + diff --git a/lib/crewai/src/crewai/crews/crew_output.py b/lib/crewai/src/crewai/crews/crew_output.py index 9f2f03185f..fef5646540 100644 --- a/lib/crewai/src/crewai/crews/crew_output.py +++ b/lib/crewai/src/crewai/crews/crew_output.py @@ -7,7 +7,7 @@ from crewai.tasks.output_format import OutputFormat from crewai.tasks.task_output import TaskOutput -from crewai.types.usage_metrics import UsageMetrics +from crewai.types.usage_metrics import UsageMetrics, WorkflowTokenMetrics class CrewOutput(BaseModel): @@ -26,6 +26,10 @@ class CrewOutput(BaseModel): token_usage: UsageMetrics = Field( description="Processed token summary", default_factory=UsageMetrics ) + token_metrics: WorkflowTokenMetrics | None = Field( + description="Detailed per-agent and per-task token metrics", + default=None + ) @property def json(self) -> str | None: # type: ignore[override] diff --git a/lib/crewai/src/crewai/tasks/task_output.py b/lib/crewai/src/crewai/tasks/task_output.py index 901604ac1d..1da3ef0c5c 100644 --- a/lib/crewai/src/crewai/tasks/task_output.py +++ b/lib/crewai/src/crewai/tasks/task_output.py @@ -6,6 +6,7 @@ from pydantic import BaseModel, Field, model_validator from crewai.tasks.output_format import OutputFormat +from crewai.types.usage_metrics import TaskTokenMetrics from crewai.utilities.types import LLMMessage @@ -22,6 +23,7 @@ class TaskOutput(BaseModel): json_dict: JSON dictionary output of the task agent: Agent that executed the task output_format: Output format of the task (JSON, PYDANTIC, or RAW) + usage_metrics: Token usage metrics for this specific task """ description: str = Field(description="Description of the task") @@ -42,6 +44,10 @@ class TaskOutput(BaseModel): description="Output format of the task", default=OutputFormat.RAW ) messages: list[LLMMessage] = Field(description="Messages of the task", default=[]) + usage_metrics: TaskTokenMetrics | None = Field( + description="Token usage metrics for this task", + default=None + ) @model_validator(mode="after") def set_summary(self): diff --git a/lib/crewai/src/crewai/types/usage_metrics.py b/lib/crewai/src/crewai/types/usage_metrics.py index 77e9ef5981..2bcdbdd76a 100644 --- a/lib/crewai/src/crewai/types/usage_metrics.py +++ b/lib/crewai/src/crewai/types/usage_metrics.py @@ -44,3 +44,74 @@ def add_usage_metrics(self, usage_metrics: Self) -> None: self.cached_prompt_tokens += usage_metrics.cached_prompt_tokens self.completion_tokens += usage_metrics.completion_tokens self.successful_requests += usage_metrics.successful_requests + + +class AgentTokenMetrics(BaseModel): + """Token usage metrics for a specific agent. + + Attributes: + agent_name: Name/role of the agent + agent_id: Unique identifier for the agent + total_tokens: Total tokens used by this agent + prompt_tokens: Prompt tokens used by this agent + completion_tokens: Completion tokens used by this agent + successful_requests: Number of successful LLM requests + """ + + agent_name: str = Field(description="Name/role of the agent") + agent_id: str | None = Field(default=None, description="Unique identifier for the agent") + total_tokens: int = Field(default=0, description="Total tokens used by this agent") + prompt_tokens: int = Field(default=0, description="Prompt tokens used by this agent") + cached_prompt_tokens: int = Field(default=0, description="Cached prompt tokens used by this agent") + completion_tokens: int = Field(default=0, description="Completion tokens used by this agent") + successful_requests: int = Field(default=0, description="Number of successful LLM requests") + + +class TaskTokenMetrics(BaseModel): + """Token usage metrics for a specific task. + + Attributes: + task_name: Name of the task + task_id: Unique identifier for the task + agent_name: Name of the agent that executed the task + total_tokens: Total tokens used for this task + prompt_tokens: Prompt tokens used for this task + completion_tokens: Completion tokens used for this task + successful_requests: Number of successful LLM requests + """ + + task_name: str = Field(description="Name of the task") + task_id: str | None = Field(default=None, description="Unique identifier for the task") + agent_name: str = Field(description="Name of the agent that executed the task") + total_tokens: int = Field(default=0, description="Total tokens used for this task") + prompt_tokens: int = Field(default=0, description="Prompt tokens used for this task") + cached_prompt_tokens: int = Field(default=0, description="Cached prompt tokens used for this task") + completion_tokens: int = Field(default=0, description="Completion tokens used for this task") + successful_requests: int = Field(default=0, description="Number of successful LLM requests") + + +class WorkflowTokenMetrics(BaseModel): + """Complete token usage metrics for a crew workflow. + + Attributes: + total_tokens: Total tokens used across entire workflow + prompt_tokens: Total prompt tokens used + completion_tokens: Total completion tokens used + successful_requests: Total successful requests + per_agent: Dictionary mapping agent names to their token metrics + per_task: Dictionary mapping task names to their token metrics + """ + + total_tokens: int = Field(default=0, description="Total tokens used across entire workflow") + prompt_tokens: int = Field(default=0, description="Total prompt tokens used") + cached_prompt_tokens: int = Field(default=0, description="Total cached prompt tokens used") + completion_tokens: int = Field(default=0, description="Total completion tokens used") + successful_requests: int = Field(default=0, description="Total successful requests") + per_agent: dict[str, AgentTokenMetrics] = Field( + default_factory=dict, + description="Token metrics per agent" + ) + per_task: dict[str, TaskTokenMetrics] = Field( + default_factory=dict, + description="Token metrics per task" + )