A before-tool callback is an interception point before tool execution. It can approve, validate, or replace a tool call; returning a value skips the tool and uses that value as the result, while returning None allows execution. section 5.5.1 section 5.5.2
Implement the callback flow in three stages:
- Run registered callbacks in order, passing ExecutionContext and the ToolCall. section 5.5.2
- Stop at the first non-None result, applying the Early Exit pattern; otherwise execute the tool. section 5.5.2
- Support asynchronous callbacks by checking awaitable results before continuing. section 5.5.2
Approval and cache interception
Register an approval callback in before_tool_callbacks alongside tools such as search_web, list_files, read_file, and delete_file. The callback can request approval for a dangerous operation or return a cached result, preventing execution. Pair it with an after-tool callback when postprocessing is also required. section 5.5.1 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().
Use the execution-flow diagram to locate before_tool_callback between the agent's reasoning step and each tool execution. section 5.5.1
