Skip to content

[FEATURE] Add tool execution result to ToolUsageFinishedEvent #2440

Closed
@uladkaminski

Description

@uladkaminski

Feature Area

Core functionality

Is your feature request related to a an existing bug? Please link it here.

Feature Request: Include Tool Results in ToolUsageFinishedEvent

Current Behavior

Currently, when a tool execution is completed, CrewAI emits a ToolUsageFinishedEvent that includes various metadata about the execution (agent info, tool name, arguments, timing, etc.) but doesn't include the actual result returned by the tool.

This means that event listeners need to implement complex workarounds to access the tool results, typically by accessing the agent's tools_results array and searching for matching entries.

Proposed Change

Add a result field to the ToolUsageFinishedEvent class and ensure it's populated with the tool's execution result when the event is emitted.

Benefits

  1. Improved developer experience when working with tool execution events
  2. Reduced coupling between event listeners and CrewAI's internal structure
  3. More complete event information - events should contain all relevant data
  4. Simplifies external integrations and monitoring systems that rely on events

Implementation

The change would be minimal and backward compatible:

  1. Modify the ToolUsageFinishedEvent class in crewai/utilities/events/tool_usage_events.py:
class ToolUsageFinishedEvent(ToolUsageEvent):
    """Event emitted when a tool execution is completed"""
    started_at: datetime
    finished_at: datetime
    from_cache: bool = False
    result: Any = None  # Add this field
    type: str = "tool_usage_finished"

Add the tool execution result directly to ToolUsageFinishedEvent:

  1. Add a result: Any = None field to the ToolUsageFinishedEvent class

  2. Modify the ToolUsage.on_tool_use_finished method to include the result in the event data:

    • Update the method signature to accept the result parameter
    • Add the result to the event_data dictionary
  3. Update the call to on_tool_use_finished in the _use method to pass the result parameter

This would allow event listeners to directly access the tool's result output through event.result without having to implement complex workarounds.

Current workarounds and alternatives I've explored:

  1. Current workaround: Access agent.tools_results from the event's source

    • Requires searching through a list of results to find the matching entry
    • Creates tight coupling between listeners and CrewAI's internal structure
    • Brittle if CrewAI changes how tool results are stored
  2. Alternative: Create a custom subclass of ToolUsageFinishedEvent

    • Would require monkey-patching the event bus to intercept events
    • Complexity of maintaining this external to CrewAI codebase
    • Risk of breaking when CrewAI is updated
  3. Alternative: Add a tool results caching system to event listeners

    • Additional complexity and state management
    • Would still need to correlate events with cached results

Willingness to Contribute

Yes, I'd be happy to submit a pull request

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions