Skip to content

after_llm_call hook prevents agents from executing tools #6529

Description

@lucasgomide

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Fields

    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions