Chat with the AI SDK
A chat built from PromptBar, Message and StreamingText, driven by useChat from the AI SDK.
useChat keeps the conversation and its status; Satin draws it. PromptBar sends the text, every turn is a Message, and the reply that is still arriving goes through StreamingText, which writes it at an even pace however unevenly the model sends it.
This page has no model behind it. The demo below gives useChat a transport that picks a scripted reply and streams it word by word, as the same chunks a real /api/chat sends. Everything else is the code in the Code tab.
Ask anything. The replies are scripted, since this page has no model; try installing, dark mode or streaming.
Install
This recipe is written for AI SDK 7 (ai) and @ai-sdk/react 4. Messages there are lists of typed parts, and useChat talks to the server through a transport rather than an api option.
npm i ai @ai-sdk/reactThe route
DefaultChatTransport posts the conversation to /api/chat. The route turns the UI messages into model messages, streams the answer and sends it back as a UI message stream, the format useChat reads. The model id below goes through the Vercel AI Gateway; a provider package such as @ai-sdk/anthropic works the same way.
// app/api/chat/route.ts (Next.js; any server that returns a Response works)
import { convertToModelMessages, streamText, type UIMessage } from 'ai'
export async function POST(req: Request) {
const { messages }: { messages: UIMessage[] } = await req.json()
const result = streamText({
// A model through the Vercel AI Gateway (AI_GATEWAY_API_KEY), or any AI SDK provider's model.
model: 'anthropic/claude-opus-5.5',
instructions: 'You are a helpful assistant. Answer in short plain-text paragraphs.',
messages: await convertToModelMessages(messages),
})
return result.toUIMessageStreamResponse()
}How the parts fit
status is submitted until the first words arrive, then streaming, then ready. While it's submitted there's nothing to write yet, so AgentLoading fills the gap. Only the last assistant message streams: it gets streaming while the status says so, and older ones stay as they are.
StreamingText keeps writing for a moment after the stream ends, because it paces the text. Its onDone says when the reply is fully on screen; that's when the message's actions (copy, feedback) appear, so they never show under a half-written answer. Keep each message under its id as the key: StreamingText starts over when it mounts, and a stable key means a reply is written out once.
PromptBar is disabled while a reply arrives, and a Stop button calls stop(); what was received so far stays. StreamingText renders plain text; for Markdown answers, render the finished text with your own Markdown component once onDone fires.
The demo's transport
A ChatTransport is the one place useChat touches the network, so it's also where to swap the server for a script: in a demo, a test or a story. This one returns the chunks a route sends, spaced out with simulateReadableStream from ai.
import { simulateReadableStream, type ChatTransport, type UIMessage, type UIMessageChunk } from 'ai'
// The demo's stand-in for /api/chat: the same chunks, from a script.
const scriptedTransport: ChatTransport<UIMessage> = {
async sendMessages({ messages }) {
const reply = pickReply(messages.at(-1))
const words = reply.match(/\S+\s*/g) ?? []
return simulateReadableStream<UIMessageChunk>({
initialDelayInMs: 900,
chunkDelayInMs: 45,
chunks: [
{ type: 'start' },
{ type: 'text-start', id: 'text' },
...words.map((delta) => ({ type: 'text-delta' as const, id: 'text', delta })),
{ type: 'text-end', id: 'text' },
{ type: 'finish' },
],
})
},
async reconnectToStream() {
return null
},
}
const chat = useChat({ transport: scriptedTransport })Something wrong or missing on this page?
Report a problemSuggest an improvement