Lifecycle
RunHooksBase
Bases:Generic[TContext,TAgent]
A class that receives callbacks on various lifecycle events in an agent run. Subclass andoverride the methods you need.
on_llm_startasync
on_llm_start(context:RunContextWrapper[TContext],agent:Agent[TContext],system_prompt:Optional[str],input_items:list[TResponseInputItem],)->NoneCalled just before invoking the LLM for this agent.
on_llm_endasync
on_llm_end(context:RunContextWrapper[TContext],agent:Agent[TContext],response:ModelResponse,)->NoneCalled immediately after the LLM call returns for this agent.
on_agent_startasync
on_agent_start(context:RunContextWrapper[TContext],agent:TAgent)->NoneCalled before the agent is invoked. Called each time the current agent changes.
on_agent_endasync
on_agent_end(context:RunContextWrapper[TContext],agent:TAgent,output:Any,)->NoneCalled when the agent produces a final output.
on_handoffasync
on_handoff(context:RunContextWrapper[TContext],from_agent:TAgent,to_agent:TAgent,)->NoneCalled when a handoff occurs.
on_tool_startasync
on_tool_start(context:RunContextWrapper[TContext],agent:TAgent,tool:Tool,)->NoneCalled immediately before a local tool is invoked.
on_tool_endasync
on_tool_end(context:RunContextWrapper[TContext],agent:TAgent,tool:Tool,result:str,)->NoneCalled immediately after a local tool is invoked.
AgentHooksBase
Bases:Generic[TContext,TAgent]
A class that receives callbacks on various lifecycle events for a specific agent. You canset this onagent.hooks to receive events for that specific agent.
Subclass and override the methods you need.
on_startasync
on_start(context:RunContextWrapper[TContext],agent:TAgent)->NoneCalled before the agent is invoked. Called each time the running agent is changed to thisagent.
on_endasync
on_end(context:RunContextWrapper[TContext],agent:TAgent,output:Any,)->NoneCalled when the agent produces a final output.
on_handoffasync
on_handoff(context:RunContextWrapper[TContext],agent:TAgent,source:TAgent,)->NoneCalled when the agent is being handed off to. Thesource is the agent that is handingoff to this agent.
on_tool_startasync
on_tool_start(context:RunContextWrapper[TContext],agent:TAgent,tool:Tool,)->NoneCalled immediately before a local tool is invoked.
on_tool_endasync
on_tool_end(context:RunContextWrapper[TContext],agent:TAgent,tool:Tool,result:str,)->NoneCalled immediately after a local tool is invoked.
on_llm_startasync
on_llm_start(context:RunContextWrapper[TContext],agent:Agent[TContext],system_prompt:Optional[str],input_items:list[TResponseInputItem],)->NoneCalled immediately before the agent issues an LLM call.
on_llm_endasync
on_llm_end(context:RunContextWrapper[TContext],agent:Agent[TContext],response:ModelResponse,)->NoneCalled immediately after the agent receives the LLM response.