Description
When an after_llm_call hook is registered, an agent that would otherwise call a tool stops executing that tool. The agent finishes as if the model's tool call were the final answer. Removing the hook restores normal tool execution.
Reproduction
Requires an OpenAI API key (uses a real LLM call).
from crewai import Agent
from crewai.hooks import after_llm_call
from crewai.tools import tool
executed = []
@tool("calculate_sum")
def calculate_sum(a: int, b: int) -> int:
"""Return the sum of two integers a and b."""
executed.append((a, b))
return a + b
@after_llm_call
def observe(context):
return None # observe only, no modification
agent = Agent(
role="Math Assistant",
goal="Perform calculations using tools",
backstory="You always use the calculate_sum tool for addition.",
tools=[calculate_sum],
verbose=True,
)
result = agent.kickoff(
messages="What is 5 + 3? Use the calculate_sum tool, then report the number."
)
print("tool executed:", bool(executed))
To see the contrast, comment out the @after_llm_call def observe(...) hook and run again.
Expected
tool executed: True — the calculate_sum tool runs, exactly as it does when no hook is registered.
Actual
tool executed: False — with the after_llm_call hook registered, the tool is never executed and the agent returns the model's raw tool call as its result. Removing the hook makes the tool execute normally.
Description
When an
after_llm_callhook is registered, an agent that would otherwise call a tool stops executing that tool. The agent finishes as if the model's tool call were the final answer. Removing the hook restores normal tool execution.Reproduction
Requires an OpenAI API key (uses a real LLM call).
To see the contrast, comment out the
@after_llm_call def observe(...)hook and run again.Expected
tool executed: True— thecalculate_sumtool runs, exactly as it does when no hook is registered.Actual
tool executed: False— with theafter_llm_callhook registered, the tool is never executed and the agent returns the model's raw tool call as its result. Removing the hook makes the tool execute normally.