DS DevShelfHub Projects · AI tools
Tutorials / CrewAI / Reference / Methods / register_before_tool_call_hook()
Method Programmatic hook registration

register_before_tool_call_hook(): Reference Guide

By DevShelfHub

Registers a before-tool-call hook programmatically.

See the CrewAI methods catalog, CrewAI introduction, kickoff() reference, and core concepts for surrounding context.

What is register_before_tool_call_hook()?

`register_before_tool_call_hook(fn)` exposes the same veto semantics as `@before_tool_call_crew`: inspect `ToolCallHookContext` for tool name, serialized arguments, and caller identity, then return `False` to block the invocation, or `None` to allow it to proceed. This is how policy engines wire allow-lists at runtime without editing source — read YAML, map environments to hook callables, and register once per process during startup.

Because tool hooks run synchronously around potentially high-latency tools, keep checks cheap: regex over URLs, hash comparisons on payloads, or cache lookups — not remote HTTP unless you accept the stall on every tool call. When you need asynchronous human approval, prefer Flow `@human_feedback` or an external orchestrator that gates the kickoff instead of blocking inside the hook.

Remember that returning truthy values other than `None` does not mean allow; only `False` denies and `None` continues. Document that contract for every engineer touching your safety stack. Keep allow-lists versioned next to tool releases so renamed tools do not silently bypass policy.

Use Cases

  • Configurable policy hooks
  • Plugin systems

Key Features

  • Runtime registration

When NOT to Use

Inline static guards — use the decorator.

Notes

False positives block user workflows

Aggressive deny lists frustrate operators. Pair denials with actionable error strings surfaced back to the agent via tool responses.

Argument shape drift

Tool args follow args_schema serialization; schema upgrades can rename fields. Version your policies alongside tool releases.

Multi-tenant isolation

Global hooks see every crew in the process. Thread tenant identifiers through ctx metadata or wrap registrations per request scope when feasible.

Ordering with after-tool hooks

Before hooks always run prior to execution; after hooks observe results. Do not rely on cross-hook mutable globals without locks.

Parameters

Parameter Type Required Purpose
fn Callable[[ToolCallHookContext], bool | None] No Hook function.

Code Examples

Register

python
register_before_tool_call_hook(audit_hook)

Deny destructive tools in prod

python
DENY = {"drop_database", "delete_bucket"}

def production_gate(ctx):
    if ctx.tool_name in DENY:
        return False

register_before_tool_call_hook(production_gate)

Structured audit trail

python
def audit(ctx):
    audit_log.append({"tool": ctx.tool_name, "args": ctx.tool_args})
    return None

register_before_tool_call_hook(audit)

When to Use

Plugin-loaded tool guards.

Common Mistakes

❌ Returning True expecting allow semantics

✅ Return None to allow, False to deny.

❌ Performing network I/O synchronously on every call

✅ Cache policy decisions or move approvals outside the hook.

Related: @tool decorator reference, Agent class reference, and the first Crew tutorial.

register_before_tool_call_hook() FAQ

What is register_before_tool_call_hook() in CrewAI?

Registers a before-tool-call hook programmatically. `register_before_tool_call_hook(fn)` exposes the same veto semantics as `@before_tool_call_crew`: inspect `ToolCallHookContext` for tool name, serialized arguments, and caller identity, then return `False` to block the invocation, or `None` to allow it to proceed. This is how policy engines wire allow-lists at runtime without editing source — read YAML, map environments to hook callables, and register once per process during startup. Because tool hooks run synchronously arou…

Which CrewAI types expose the method register_before_tool_call_hook()?

DevShelfHub documents register_before_tool_call_hook() on Programmatic hook registration. The reference maps it to Python module crewai.hooks — pin your installed crewai version and match imports to the snippet on this page.

When should I use register_before_tool_call_hook()?

Plugin-loaded tool guards.

When should I avoid register_before_tool_call_hook()?

Inline static guards — use the decorator.

How do I call register_before_tool_call_hook() from Python?

register_before_tool_call_hook(my_guard)

Where can I explore more CrewAI API reference pages?

Open the CrewAI API reference index on DevShelfHub to search classes, methods, and decorators, each with runnable examples, parameters, common mistakes, and cross-links.