Tool Execution: The _run Method, Error Handling, and Permission Prompts
~15 min read
Every custom tool needs a _run method executed when the agent wants to use it — the book's example fetches live exchange rates and explicitly handles failed requests and invalid currency codes rather than letting them crash the agent.
Tool Execution: The _run Method, Error Handling, and Permission Prompts is a Pro topic
Sign in, then upgrade to Pro or Power to unlock this topic and the full AI Engineering curriculum.
Key points
- •The _run method is where a tool's actual logic executes, invoked whenever the agent decides to use the tool
- •Explicit error handling (failed requests, invalid input) matters especially for agents — a clean error message lets the agent recover; a raw crash doesn't
- •Since tools can have real side effects (network calls, file I/O), agent frameworks often gate execution behind explicit user permission
- •The book's example: Claude's client asking 'allow yes/no?' the first time a tool is used, keeping the human in control of powerful actions
- •Error handling (recovery after failure) and permission gates (safety before execution) are two distinct, complementary concerns