Tools
Models write sentences well; they are weak at exact math and “what is the weather right now”. Tools are functions the model can call: a description, a Zod inputSchema, and an optional execute. The AI SDK runs execute on the server, feeds the result back, and lets the model continue.
Use the tool() helper so execute parameters infer correctly.
Defining a tool
tools is an object: keys are tool names.
Wire into streamText (chat route)
Default stopWhen is isStepCount(1): after one tool call the model stops, so the UI often shows JSON and no natural-language summary. Allow a loop:
Ask “What is the temperature in New York in celsius?” and the model should call weather, then convertFahrenheitToCelsius, then answer in text. Log toolResults in onStepEnd if you want.
The same tools + stopWhen work with generateText when there is no UI.
Render tool parts on the client
Tool parts are always typed tool-{toolName}:
State walks input-streaming → input-available → output-available. Show “Looking up weather…” before output-available.
Approval, safety, anti-patterns
Tools with execute run automatically. Dangerous actions (delete, pay) should use toolApproval (user-approval, etc.). Do not eval untrusted input in production.
Tools run on the server: they can hit databases and internal APIs. Never return secrets to the model or the browser. Write clear description and .describe() text to cut down on random calls.
stopWhen: isStepCount(n) is a cap, not a quota. Structured output also counts as a step—leave enough room when combining tools and output.