Required prerequisites
Motivation
Background
Applications (e.g., eigent) that need to observe the agent execution lifecycle currently have no framework-level way to do so. The required observations include:
- Agent step started / completed / failed
- Tool call started / completed / failed
- Runtime metadata (request ID, task ID, etc.) attached to each event
No callback/event API exists. Applications must subclass ChatAgent and override internal methods (step, astep, _execute_tool, _aexecute_tool, clone) to inject lifecycle hooks. This couples downstream code to internal implementation details.
contextvars context is lost in async-to-sync tool execution. When ChatAgent dispatches sync tools to a thread pool from an async path (astep → _aexecute_tool → run_in_executor), contextvars.ContextVar values set by the caller are silently dropped. The same applies to FunctionTool.async_call and the step() timeout executor path. This breaks any middleware or instrumentation that relies on ContextVar propagation.
clone() loses runtime configuration. Several fields set at construction time are not carried over to the cloned agent, including mask_tool_output, retry_attempts, retry_delay, step_timeout, enable_snapshot_clean, stream_accumulate, and summary_window_ratio. Applications must patch these manually after cloning. Additionally:
- There is no way to pass a session ID or execution context to the clone.
response_terminators are shared by reference — mutating one agent's terminator state affects the other.
- Stateful toolkits that expose
clone_for_new_session(session_id) have no way to receive a caller-specified session ID.
Solution
Extend ChatAgent with the following capabilities:
1. Lifecycle callbacks
Introduce a standard callback/event interface (e.g., AgentCallback / AgentEvent) with:
step_started
step_completed
step_failed
tool_started
tool_completed
tool_failed
These should be triggered by the framework without requiring subclassing.
2. Execution context propagation
Ensure execution-local context is preserved during tool execution, especially:
- when sync tools are invoked from async paths
- when execution is delegated to thread pools / executors
3. Improved clone semantics
Enhance clone() to:
- preserve full runtime configuration
- support optional propagation of execution / session context
- better support stateful toolkits without requiring local patches
Alternatives
No response
Additional context
No response
Required prerequisites
Motivation
Background
Applications (e.g., eigent) that need to observe the agent execution lifecycle currently have no framework-level way to do so. The required observations include:
No callback/event API exists. Applications must subclass
ChatAgentand override internal methods (step,astep,_execute_tool,_aexecute_tool,clone) to inject lifecycle hooks. This couples downstream code to internal implementation details.contextvarscontext is lost in async-to-sync tool execution. WhenChatAgentdispatches sync tools to a thread pool from an async path (astep→_aexecute_tool→run_in_executor),contextvars.ContextVarvalues set by the caller are silently dropped. The same applies toFunctionTool.async_calland thestep()timeout executor path. This breaks any middleware or instrumentation that relies onContextVarpropagation.clone()loses runtime configuration. Several fields set at construction time are not carried over to the cloned agent, includingmask_tool_output,retry_attempts,retry_delay,step_timeout,enable_snapshot_clean,stream_accumulate, andsummary_window_ratio. Applications must patch these manually after cloning. Additionally:response_terminatorsare shared by reference — mutating one agent's terminator state affects the other.clone_for_new_session(session_id)have no way to receive a caller-specified session ID.Solution
Extend
ChatAgentwith the following capabilities:1. Lifecycle callbacks
Introduce a standard callback/event interface (e.g.,
AgentCallback/AgentEvent) with:step_startedstep_completedstep_failedtool_startedtool_completedtool_failedThese should be triggered by the framework without requiring subclassing.
2. Execution context propagation
Ensure execution-local context is preserved during tool execution, especially:
3. Improved clone semantics
Enhance
clone()to:Alternatives
No response
Additional context
No response