An after-tool callback runs after a tool executes and can transform or replace its result without changing the agent’s core tool logic. It is useful for postprocessing such as compressing search output or masking sensitive information. section 5.5.1 section 5.6
The callback receives an ExecutionContext and ToolResult. Returning None preserves the original result; returning a value replaces it. With multiple callbacks, processing stops at the first non-None return. section 5.5.2
Implement the tool path in three stages:
- Run the tool and obtain its ToolResult. section 5.5.2
- Invoke each after_tool_callback, awaiting asynchronous results when necessary. section 5.5.2
- Replace the result when a callback returns a new ToolResult; otherwise pass the original result onward. section 5.5.2
Compressing web-search output
A search_compressor checks whether the tool is search_web and whether its content exceeds 2,000 characters. For long results, it matches the search query by tool_call_id, chunks the content, performs vector search, and returns the top three chunks in a new ToolResult. Other tools and short results pass through unchanged. section 5.5.4
Figure 5.7 Points where callbacks are inserted into the agent execution flow. LLM callbacks are called before and after think(), and tool callbacks are called before and after each tool execution within act().
Trace callback placement around each tool execution within the agent’s act phase, alongside the before- and after-LLM extension points. section 5.5.1
