What is register_after_tool_call_hook()?
`register_after_tool_call_hook(fn)` is the imperative twin of `@after_tool_call_crew`. The callable receives `ToolCallHookContext` including the raw tool output, and may return a string that replaces what the agent reads — ideal for trimming giant HTTP payloads, masking secrets accidentally returned by APIs, or attaching canonical summaries before the LLM consumes JSON.
Treat this hook as part of your reliability boundary: if tools occasionally emit invalid UTF-8 or multi-megabyte blobs, fix or truncate here instead of letting the planner blow its context window. When you implement caching, key on `(tool_name, stable_hash(args))` and respect TTLs so stale cache entries do not survive data corrections.
Returning `None` leaves the original tool string intact; returning text swaps it. Avoid raising unless failure should abort the entire kickoff — prefer returning an explanatory error string the agent can reason about. When you log hook activity, record byte lengths and hashes instead of raw payloads so observability bills stay predictable.
Use Cases
- • Result caching plugin
- • Trim/sanitize plugin
Key Features
- ✓ Runtime registration
When NOT to Use
Static hooks — use the decorator.
Notes
Cache invalidation hazards
Caching tool output without versioning invites stale reads. Tie cache keys to dataset versions or ETags when tools wrap remote APIs.
Truncation vs structured tools
If downstream prompts expect JSON, truncating mid-stream produces invalid documents. Prefer summarizing with a structured template.
Security redaction order
Run redaction before logging hooks elsewhere in your stack; otherwise duplicate sinks might already have leaked secrets.
Interaction with streaming
Some tools stream chunks; after hooks still observe the final aggregated result in most configurations — verify behavior against your CrewAI version when mixing streaming tools.
Parameters
| Parameter | Type | Required | Purpose |
|---|---|---|---|
| fn | Callable[[ToolCallHookContext], str | None] | No | Hook function. |
Code Examples
Register
register_after_tool_call_hook(cache_results)
Truncate verbose HTTP bodies
MAX = 4000
def trim_http(ctx):
text = ctx.result or ""
if ctx.tool_name == "http_get" and len(text) > MAX:
return text[:MAX] + "…[truncated]"
return None
register_after_tool_call_hook(trim_http)
Memoize idempotent reads
_cache = {}
def memoize(ctx):
if ctx.tool_name != "fetch_spec":
return None
key = repr(ctx.tool_args)
if key in _cache:
return _cache[key]
_cache[key] = ctx.result
return None
register_after_tool_call_hook(memoize)
When to Use
Plugin-loaded result transformations.
Common Mistakes
❌ Returning empty string thinking it keeps the original
✅ Return None to preserve; empty string replaces with blank output.
❌ Caching non-idempotent tools
✅ Only memoize reads; never cache mutations without explicit invalidation.
Related: @tool decorator reference, Agent class reference, and the first Crew tutorial.
register_after_tool_call_hook() FAQ
What is register_after_tool_call_hook() in CrewAI?
Registers an after-tool-call hook programmatically. `register_after_tool_call_hook(fn)` is the imperative twin of `@after_tool_call_crew`. The callable receives `ToolCallHookContext` including the raw tool output, and may return a string that replaces what the agent reads — ideal for trimming giant HTTP payloads, masking secrets accidentally returned by APIs, or attaching canonical summaries before the LLM consumes JSON. Treat this hook as part of your reliability boundary: if tools occasionally emit invalid UTF-8 or multi-me…
Which CrewAI types expose the method register_after_tool_call_hook()?
DevShelfHub documents register_after_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_after_tool_call_hook()?
Plugin-loaded result transformations.
When should I avoid register_after_tool_call_hook()?
Static hooks — use the decorator.
How do I call register_after_tool_call_hook() from Python?
register_after_tool_call_hook(cache_fn)
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.