Skip to content

ENH: Add lifecycle hooks for CodeAgent execution flow #1883

@MkYacine

Description

@MkYacine

Problem
I'm working on a multi-agent computer use system for accessibility tech, and I need to execute callbacks at different points in the CodeAgent's execution flow, not just after step completion.
The current step_callbacks only fires after a step has fully completed (in _finalize_step()). For my particular use case, I needed to send the LLM's generated text to a TTS service before code execution. This is because code execution takes some time and I need to queue the TTS asap.
But I can think of a few other cases where having more life cycle hooks would be helpful, like custom code validation logic, human-in-the-loop approval for critical code, etc...
Currently, I'm subclassing CodeAgent and overriding step_stream() to work around this limitation and run callbacks at the right moment in the flow.

Proposed solution
I am not sure if there is a better way to do this, but my patch makes the class look like:
CodeAgent(
tools=[...],
model=...,
step_callbacks=[...], # Keep existing - backward compatible
pre_execution_callbacks=[Callable]
# more hooks?
)
And adds the appropriate hook in step_stream()
Would love to get your feedback here!

Is this not possible with the current options.
No, this is not possible with current step_callbacks, as this only runs callbacks at the end of the step.

Alternatives considered
Couldn't find anything, if I missed something do let me know!


Checklist

  • I have searched the existing issues and have not found a similar feature request.
  • I have verified that this feature is not already implemented in the latest version.
  • I am willing to work on this feature and submit a pull request. (optional)

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions