Your chat assistant goes silent for several seconds while it calls tools. How would you render tool calls and their status in the UI?
Advertisement
🧩 Scenario
Architecture Walkthrough
The message is a list of parts
With useChat from @ai-sdk/react, each message has a parts array. Text arrives as { type: 'text' }, and each tool call arrives as a part whose type is tool-<toolName>, such as tool-searchTrains. A reply that searches, then fetches fares, then answers looks like: tool part, tool part, text part. Render them in order and the user sees the story of the reply, the same way you would render blocks from a CMS.
Every tool part has a state
The AI SDK gives each tool part a state field. Treat it like the status of a request in React Query:
input-streaming: the model is still writing the arguments.inputmay be partial.input-available: arguments are complete and the tool is running (or waiting for the client).approval-requested: a human must approve before it runs.approval-responded: the user answered and the request is being resent.output-available: done,outputholds the result.output-error: it failed,errorTextsays why.output-denied: approval was refused, the tool never ran.
Map each state to one clear UI state. A spinner without a label is not enough. "Searching trains from Pune to Mumbai..." tells the user what the wait is for.
Labels come from your code, not the model
Keep a small map from tool name to a function that builds a label from the input. That keeps wording consistent, translatable, and safe from odd model text. Show the result as a compact card. Hide raw JSON behind a toggle.
Accessibility and layout
Wrap the status line in an element with role="status" (a polite live region) so screen readers hear "Searching trains" and "Found 6 trains" without being interrupted on every token. Reserve space for the card so the message does not jump when the result arrives.
Key Code Explained
'use client';
import { useChat } from '@ai-sdk/react';
import type { ToolUIPart } from 'ai';
// Human labels per tool, built from the (possibly partial) input
const labels: Record<string, (input: any) => string> = {
searchTrains: (i) => `Searching trains${i?.from ? ` from ${i.from}` : ''}${i?.to ? ` to ${i.to}` : ''}`,
getFares: () => 'Checking fares',
};
function ToolStatus({ part }: { part: ToolUIPart }) {
const name = part.type.replace('tool-', '');
const label = labels[name]?.(part.input) ?? `Running ${name}`;
switch (part.state) {
case 'input-streaming':
case 'input-available':
return <p role="status" className="tool tool--running">{label}...</p>;
case 'approval-requested':
return <p role="status" className="tool">Waiting for your approval: {label}</p>;
case 'approval-responded':
return <p role="status" className="tool">{label}...</p>;
case 'output-available':
return (
<div className="tool tool--done">
<p role="status">{label}: done</p>
<details>
<summary>Details</summary>
<pre>{JSON.stringify(part.output, null, 2)}</pre>
</details>
</div>
);
case 'output-error':
return <p role="status" className="tool tool--error">Could not finish: {part.errorText}</p>;
case 'output-denied':
return <p role="status" className="tool">Skipped: you declined this action.</p>;
}
}
export function Chat() {
const { messages } = useChat();
return messages.map((message) => (
<article key={message.id} aria-label={message.role}>
{message.parts.map((part, i) => {
if (part.type === 'text') return <p key={i}>{part.text}</p>;
if (part.type.startsWith('tool-')) {
return <ToolStatus key={(part as ToolUIPart).toolCallId} part={part as ToolUIPart} />;
}
return null; // reasoning, sources, step-start and so on
})}
</article>
));
}
Keying tool parts by toolCallId keeps React from remounting them as their state changes. The switch covers every state, so TypeScript narrows part.output and part.errorText correctly in each branch. In a real app, swap the <pre> for a component per tool, such as a train table.
Tradeoffs
| Approach | Pros | Cons |
|---|---|---|
| Generic spinner for all tools | Very quick to build | User has no idea what is happening |
| Label per tool and state | Clear, consistent, translatable | Needs a label map to maintain |
| Rich result card per tool | Best UX, results are scannable | More components, more design work |
| Raw JSON visible | Great for debugging | Noisy and confusing for normal users |
| Hide tool calls entirely | Clean chat | Long silent waits, less trust |
What Interviewers Actually Check
- Whether you render
message.partsinstead of a single content string - Whether you know the tool part states and handle every one, including errors and denial
- Whether you give the user a meaningful label during the wait
- Whether you think about accessibility, not just visuals
- Whether you separate the friendly view from debug details
Follow-Up Questions
- How would you collapse a long chain of tool calls into one summary line once the answer arrives?
- How do you render dynamic tools (from an MCP server, for example) whose names you do not know at build time?
- How would you show a tool that streams preliminary results before the final one?
- How would you persist tool parts so a reloaded conversation shows the same cards?
- How do you stop layout shift when a tool card replaces a status line?
Common Candidate Mistakes
- Only mapping
part.type === 'text', so tool activity is invisible. - Showing the model's raw arguments as the label, including odd casing or internal ids.
- Using
role="alert"for routine status, which interrupts screen reader users. - Using the array index as the key for tool parts that change state.
- Forgetting the denied state, so a declined action looks like it is still loading forever.
Interview Readiness Checklist
Before you leave this question, make sure you can answer:
- Can you render text and tool parts in order from
message.parts? - Can you list all the tool part states and what each one shows?
- Can you build a label map that works with partial input?
- Can you explain your accessibility choices for tool status?
- Can you explain when to show raw JSON and when to hide it?
Summary
Tool calls are part of the assistant's reply, so render them inline as parts, in order, next to the text. Give each tool part a small component that switches on its state and shows a human label while it runs, a compact result when it finishes, and a clear message on error or denial. Build labels from your own code, keep raw JSON behind a toggle, key parts by toolCallId, and announce changes through a polite live region. The user stops seeing a frozen chat and starts seeing an assistant that explains what it is doing.
Why does a tool show up as a part and not as a separate message?
One assistant reply can contain text, several tool calls and more text after them. Keeping them as ordered parts of one message preserves that order, so you can render "Checking your order..." and then the final sentence in the same bubble.
Should I show the raw tool input and output to users?
Usually not in the main view. Show a friendly label and a formatted result, and put raw JSON behind a "Details" toggle that is useful for power users and for debugging.
What is the difference between output-error and output-denied?
output-error means the tool ran (or tried to) and failed, and errorText explains why. output-denied means a human or an automatic rule refused the call during approval, so the tool never ran.
Advertisement
Stay Updated
Get the latest frontend challenges, interview questions and tutorials delivered to your inbox.
Advertisement