DEV Community

Alex Spinov
Alex Spinov

Posted on

Vercel AI SDK Has a Free AI Toolkit — Stream LLM Responses in React with 3 Lines

Vercel AI SDK is a TypeScript toolkit for building AI applications — stream LLM responses, manage conversations, and call tools with type safety.

What You Get for Free

  • Streaming — stream tokens to the UI as they're generated
  • useChat hook — complete chat UI state management in React
  • useCompletion — text completion with streaming
  • Tool calling — type-safe function calling with any LLM
  • Multi-provider — OpenAI, Anthropic, Google, Mistral, Groq, Ollama
  • Structured output — Zod schema validation on LLM responses
  • Middleware — logging, caching, rate limiting
  • Edge-ready — works on Vercel Edge, Cloudflare Workers

Quick Start

npm install ai @ai-sdk/openai
Enter fullscreen mode Exit fullscreen mode
// app/api/chat/route.ts (Next.js)
import { openai } from '@ai-sdk/openai'
import { streamText } from 'ai'

export async function POST(req: Request) {
  const { messages } = await req.json()
  const result = streamText({
    model: openai('gpt-4o'),
    messages,
  })
  return result.toDataStreamResponse()
}
Enter fullscreen mode Exit fullscreen mode
// app/page.tsx
'use client'
import { useChat } from 'ai/react'

export default function Chat() {
  const { messages, input, handleInputChange, handleSubmit } = useChat()
  return (
    <form onSubmit={handleSubmit}>
      {messages.map(m => <div key={m.id}>{m.role}: {m.content}</div>)}
      <input value={input} onChange={handleInputChange} />
    </form>
  )
}
Enter fullscreen mode Exit fullscreen mode

Why Developers Switch from Rolling Their Own

Building AI chat from scratch = managing SSE, parsing chunks, handling errors:

  • 3 linesuseChat() handles messages, loading, errors
  • Multi-provider — swap OpenAI for Anthropic by changing one line
  • Tool calling — type-safe function execution
  • Streaming — automatic token-by-token rendering

A developer spent 3 days building custom SSE streaming, message management, and error handling. After Vercel AI SDK: 20 lines of code, same features, works out of the box.

Need Custom Data Solutions?

I build production-grade scrapers and data pipelines for startups, agencies, and research teams.

Browse 88+ ready-made scrapers on Apify → — Reddit, HN, LinkedIn, Google, Amazon, and more.

Custom project? Email me: spinov001@gmail.com — fast turnaround, fair pricing.

Top comments (0)