Quick start
This chapter runs AI SDK Core once without a UI, then wires Next.js App Router to useChat. The default model string is Gateway openai/gpt-4.1-mini. The official quickstart currently demos xai/grok-4.6. IDs change—use whatever your account allows.
Finish Installation first: Node 22+, pnpm add ai @ai-sdk/react zod, and AI_GATEWAY_API_KEY in .env.local.
1. No UI: generateText
Use this for scripts, cron jobs, and one-shot server summaries. No React required.
Direct OpenAI instead:
generateText also returns usage, finishReason, toolCalls, and more—start there when debugging.
2. Streaming chat: route handler
Create app/api/chat/route.ts:
What happens:
- The client sends
UIMessage[](UI metadata such as timestamps included). convertToModelMessagesis async; it strips UI fields intoModelMessage[].streamTextstarts immediately. You must consumeresult.streamor generation stalls.toUIMessageStream+createUIMessageStreamResponsewrap the stream as SSE thatuseChatunderstands.
Do not copy toDataStreamResponse() from old blog posts.
3. Page: useChat
app/page.tsx must be a client component (React hooks):
The default POST target is /api/chat. Send with sendMessage({ text }) and keep the input in useState. Render message.parts; do not assume a single content string.
Open http://localhost:3000, type a message, and tokens should appear as they stream.
First checklist
pnpm create next-app→pnpm add ai @ai-sdk/react zod- Set
AI_GATEWAY_API_KEY - Run the
generateTextsnippet (a temporary.mtsfile is fine) - Add
route.ts+page.tsxand confirm streaming chat - Switch
modeltogateway('anthropic/claude-sonnet-4.5')oropenai('gpt-5.1')and confirm it still works
Next, attach a weather tool and status—see useChat and Tools. Timeouts and deploy: Next.js.