

Lifecycle hooks give you fine-grained control over the tool execution pipeline. Instead of a simple input → execute → output flow, you can intercept and transform data at every stage: before execution, after execution, and on error.
The AI SDK's tool() function accepts hook callbacks that run at specific points in the tool lifecycle. The onInputParsed hook fires after the model's tool call parameters are validated against your Zod schema but before the tool executes — giving you a chance to transform, enrich, or reject the input. The onOutput hook processes the result before it is returned to the model.
The practical use case is input sanitization and output formatting. A web search tool might normalize the query in onInputParsed, execute the search, then format the results in onOutput to be more useful for the model's next reasoning step. Error hooks can implement fallback logic or graceful degradation.
Use lifecycle hooks when you need to separate tool logic from data transformation. The tool itself stays focused on its core capability, while hooks handle cross-cutting concerns like logging, validation, rate limiting, and output formatting.











