Skip to content

on_llm_end hook not being called for agents with handoffs #1909

@muneebarshadd

Description

@muneebarshadd

Title

on_llm_end hook not being called for agents with handoffs

Description

The on_llm_end hook in RunHooks is not being triggered when an agent uses handoffs to transfer to another agent, even though the hook is properly defined and other hooks (on_agent_start, on_handoff) are being called successfully.

Environment

  • openai-agents version: 0.2.11
  • Python version: 3.11

Expected Behavior

When an agent completes an LLM call, the on_llm_end hook should be triggered with the ModelResponse object, allowing access to the LLM's output before any handoff occurs.

Actual Behavior

The on_llm_end hook is never called. Only on_agent_start and on_handoff hooks are triggered.

Code Example

from agents import Agent, RunHooks, RunContextWrapper, ModelResponse

class MyAgentHooks(RunHooks):
    async def on_agent_start(self, context, agent):
        print("✅ on_agent_start called")  # This IS called
    
    async def on_llm_start(self, context, agent):
        print("❌ on_llm_start called")  # This is NEVER called
    
    async def on_llm_end(self, context, agent, response: ModelResponse):
        print("❌ on_llm_end called")  # This is NEVER called
        print(f"Response: {response}")
    
    async def on_handoff(self, context, agent, source):
        print("✅ on_handoff called")  # This IS called

# Agent with handoff
next_agent = Agent(name="next_agent", instructions="...")

my_agent = Agent(
    name="my_agent",
    instructions="Output text then call handoff tool",
    hooks=MyAgentHooks(),
    handoffs=[next_agent],
)

# Run the agent
result = Runner.run_streamed(my_agent, input="test query", context=my_context)

Observed Hook Execution Order

  1. on_agent_start - Called
  2. on_llm_start - Not verified
  3. on_llm_end - NOT CALLED (this is the issue)
  4. on_handoff - Called

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions