Build a streaming React chat with the AI SDK
Connect a React chat interface to the Vercel AI SDK. Stream messages, stop a reply, show sources and ask for approval before a tool runs.
On this page
Conversation preview
Weekly report
Local preview
Connect AI SDK
Set up a server-side chat endpoint with the AI SDK and install the React client in your app. Keep your model provider key on the server.
npm install ai @ai-sdk/reactFollow the AI SDK chatbot guide to connect /api/chat to your model. Configure your Individual or Team license using the registry setup guide, then add the chat components.
npx shadcn@latest add @interfaces-ds/conversation @interfaces-ds/message @interfaces-ds/prompt-input @interfaces-ds/suggestion @interfaces-ds/reasoning @interfaces-ds/tool @interfaces-ds/sources @interfaces-ds/confirmationRender a streaming reply
Keep one Message per message ID. As text arrives, render the updated text parts inside the same assistant turn. Use each tool call’s ID to keep its status attached to the right message.
Pass the status from useChat to Prompt Input. Supply stop as its onStop callback so the reader can interrupt a submitted or streaming reply. Keep the partial text visible when they stop. If the request fails, give them a retry action.
Handle tool approvals and sources
When a tool part reaches approval-requested, show the proposed action in Confirmation. Pass its approval ID and the user’s decision to addToolApprovalResponse. Use Tool to display the result or error, and Sources for source parts supplied by your provider.