How to Build an AI-Powered Streaming Chat with Vercel AI SDK and Next.js 16
How to Build an AI-Powered Streaming Chat with Vercel AI SDK and Next.js 16
The "chat" interface is the new standard for AI applications. But a static "loading" spinner is no longer enough. Users expect a fluid, streaming experience where the AI responds in real-time.
In this tutorial, we'll build a production-ready streaming chat using the Vercel AI SDK and Next.js 16.
Prerequisites
- Node.js 20+
- Next.js 16 Project
- An API Key from OpenAI, Anthropic, or Google Gemini
Step 1: Installing the SDK
First, install the necessary packages from the Vercel AI ecosystem.
npm install ai @ai-sdk/openai
Step 2: Creating the Route Handler
In Next.js 16, we can leverage the App Router to create a clean API endpoint for our AI logic. Create a file at app/api/chat/route.ts.
import { openai } from '@ai-sdk/openai';
import { streamText } from 'ai';
export async function POST(req: Request) {
const { messages } = await req.json();
const result = await streamText({
model: openai('gpt-4o'),
messages,
});
return result.toDataStreamResponse();
}
Step 3: Implementing the Frontend Hook
The Vercel AI SDK provides a powerful useChat hook that handles the state, streaming, and input automatically.
'use client';
import { useChat } from 'ai/react';
export default function Chat() {
const { messages, input, handleInputChange, handleSubmit } = useChat();
return (
<div className="flex flex-col w-full max-w-md py-24 mx-auto stretch">
{messages.map(m => (
<div key={m.id} className={`whitespace-pre-wrap ${m.role === 'user' ? 'text-blue-600' : 'text-gray-800'}`}>
<strong>{m.role === 'user' ? 'User: ' : 'AI: '}</strong>
{m.content}
</div>
))}
<form onSubmit={handleSubmit}>
<input
className="fixed bottom-0 w-full max-w-md p-2 mb-8 border border-gray-300 rounded shadow-xl"
value={input}
placeholder="Say something..."
onChange={handleInputChange}
/>
</form>
</div>
);
}
Key Optimization Tips for AI Chats
- Streaming UI: Always use streaming to reduce the "perceived latency."
- Context Windows: Manage your message history effectively to avoid hitting token limits.
- Error Handling: Implement robust error boundaries for API timeouts or rate limits.
Conclusion
Combining Next.js 16 with the Vercel AI SDK allows you to move from idea to production in minutes. The streaming response doesn't just look better—it fundamentally improves the user experience.
Looking to scale your AI application? Get a professional project estimate today.
需要专业的全栈建站与 SEO 流量代运营?
无论是重构老旧系统、开发全新微信小程序,还是从零搭建高权重的技术博客。JayApp (WebMaster 团队) 提供从底层架构到顶层运营的一站式闭环服务。
立即免费咨询您的增长方案