The Vercel AI SDK · Chapter 9 of 21
Tools in the chat
By the end of this chapter Mali's chat on the website can look things up: the route gives the model HariBhari's tools, the stream carries every tool call and result, and the page tells the customer what Mali is doing while it works. You will add the tools to the route, write a component for a tool part's states, and type the chat's messages so TypeScript knows every tool. Build it in your browser: a real editor and terminal, AI that explains, and tests that tell you when it works.
- Part: The chat UI
- 3 tasks
- TypeScript
- In the all labs plan
The first 2 chapters of The Vercel AI SDK are free. This one comes with the all labs plan.
Why you need this
Ask the chat from chapter 8 "Where is my order HB-1042?" and Mali guesses, the way chapter 3 measured: a model with no tool to look an order up answers from nothing.
Your order HB-1042 is on its way and should reach you today.
It sounds right, and it was made up: no request ever reached HariBhari's orders. Deepa has seen what a confident wrong answer does to a customer who is waiting for a delivery. In chapters 3 and 4 Mali learned to look things up in scripts: maliTools() and stopWhen. Now the website needs the same, and two more things a script never needed: the customer should see that Mali is checking (a lookup can take a moment, and a silent page looks broken), and the page must say so when a lookup fails.
What you build: 3 tasks
Each task has tests. You write the code in the lab's editor and press Check.
- Task 1
server.ts: Mali's tools on the route
Chapter 7's route in server.ts (POST, the 400 checks, Mali's instructions, the UI message stream), with tools: maliTools() (from ../../shared/mali.ts) and stopWhen: isStepCount(5) in streamText.
The Check asks about an order: all five tools offered, the events tool-input-start, tool-input-delta, tool-input-available and tool-output-available in the stream, 2 requests to the model (2 start-step events), the order's data as the tool's output, then the model's answer as text, and still a 400 for {}.
- Task 2
ToolStatus.tsx: what Mali is doing
Export default function ToolStatus({ part }) from ToolStatus.tsx (part: ToolUIPart<InferUITools<MaliTools>>). With doing = DOING[getToolName(part)] (given), or 'looking something up' for any other tool: for state 'input-streaming' or 'input-available' show <p>Mali is DOING...</p>; for 'output-error' show <p>Sorry, Mali couldn't finish DOING.</p>; for any other state show nothing.
The Check shows each of the five tools in each of those states, and a tool DOING does not know.
- Task 3
Chat.tsx: the chat, with tools at work
In Chat.tsx export type MaliMessage = UIMessage<unknown, UIDataTypes, InferUITools<MaliTools>> and a default Chat that calls useChat<MaliMessage>({ transport: new DefaultChatTransport({ api: '/api/chat' }) }). For each message show You or Mali, then each part: a text part's text; a tool part (isToolUIPart, not 'dynamic-tool') as <ToolStatus part={part} />; nothing for others. Then the given Composer, as in chapter 8.
The Check uses its own tool route at the real pace: "Mali is checking delivery..." shows before the answer and is gone at the end, no raw tool data on the page, a second message carries the first answer's tool part, and a failed tool shows "Sorry, Mali couldn't finish looking up your order."
What you will learn
- Tools on the route: tools and stopWhen in the route's streamText, exactly as in chapter 4.
- Tool events in the UI message stream, and tool parts in a message.
- A tool part's states: input-streaming, input-available, output-available, output-error.
- Showing a working line, and an error in words.
- Typed messages: InferUITools and UIMessage<...>, so each tool part knows its input and output.
More in The chat UI
Build "Tools in the chat" yourself
HariBhari, a Bengaluru plant shop, wants Mali, a plant helper on its website. You build it end to end, from the first call to a chat that streams cards, asks before it adds to the cart and stays within budget. A real editor and terminal in your browser, AI that explains, and a Check that says when it works.
The first 2 chapters of The Vercel AI SDK are free. This one comes with the all labs plan.