Vercel AI SDK 4.x: Streaming Text, Tool Calls, and Multi-Step Agents
The Vercel AI SDK unifies streaming text, structured output, tool calls, and multi-step agents across all major AI providers with a single consistent API.
Mahmudul Haque Qudrati
CEO & ML Engineer
One AI engineering post, weekly
LLM benchmarks, prompt techniques, and token-cost breakdowns — not another AI news roundup.
Every AI provider has a different streaming API, tool call format, and error shape. The Vercel AI SDK abstracts all of that into one consistent interface that works with OpenAI, Anthropic, Google, Mistral, Ollama, and dozens of others - and it integrates directly with React via hooks.
Streaming Text
// app/api/chat/route.ts
import { streamText } from "ai";
import { openai } from "@ai-sdk/openai";
export async function POST(req: Request) {
const { messages } = await req.json();
const result = await streamText({
model: openai("gpt-4o"),
messages,
onFinish({ usage, finishReason }) {
// cost tracking
console.log("Tokens used:", usage.totalTokens);
},
});
return result.toDataStreamResponse();
}
// components/Chat.tsx
"use client";
import { useChat } from "ai/react";
export function Chat() {
const { messages, input, handleInputChange, handleSubmit, isLoading } = useChat();
return (
<div>
{messages.map(m => (
<div key={m.id}><strong>{m.role}:</strong> {m.content}</div>
))}
<form onSubmit={handleSubmit}>
<input value={input} onChange={handleInputChange} disabled={isLoading} />
<button type="submit">Send</button>
</form>
</div>
);
}
Team workspace
Ship faster with chat, meetings, and projects in one place — Zlyqor.
Tool Calls
Tools let the model call functions during generation. The SDK handles the tool call → result → continue loop:
import { streamText, tool } from "ai";
import { z } from "zod";
const result = await streamText({
model: openai("gpt-4o"),
tools: {
getWeather: tool({
description: "Get current weather for a city",
parameters: z.object({ city: z.string() }),
execute: async ({ city }) => {
const data = await fetchWeather(city);
return { temperature: data.temp, condition: data.condition };
},
}),
},
prompt: "What is the weather in Tokyo?",
maxSteps: 3, // allow model to use tool then continue
});
Structured Output With generateObject()
When you need structured data instead of free text:
import { generateObject } from "ai";
import { z } from "zod";
const { object } = await generateObject({
model: openai("gpt-4o"),
schema: z.object({
tasks: z.array(z.object({
title: z.string(),
priority: z.enum(["low", "medium", "high"]),
estimatedHours: z.number(),
})),
}),
prompt: "Break down building a REST API into tasks",
});
// object.tasks is fully typed Task[]
Multi-Step Agents
Set maxSteps to let the model take multiple tool calls in a chain:
const result = await streamText({
model: anthropic("claude-3-5-sonnet-20241022"),
tools: {
searchWeb: tool({ /* ... */ }),
readPage: tool({ /* ... */ }),
writeFile: tool({ /* ... */ }),
},
system: "You are a research agent. Search for information and write a summary.",
prompt: "Research the current state of WebAssembly and write a 500-word summary.",
maxSteps: 10, // agent can take up to 10 tool calls
});
// Stream each step as it happens
for await (const delta of result.fullStream) {
if (delta.type === "tool-call") {
console.log("Tool called:", delta.toolName);
}
if (delta.type === "text-delta") {
process.stdout.write(delta.textDelta);
}
}
Switching Providers
The SDK's provider abstraction means switching models is one line:
import { openai } from "@ai-sdk/openai";
import { anthropic } from "@ai-sdk/anthropic";
import { google } from "@ai-sdk/google";
import { ollama } from "ollama-ai-provider"; // community
// Just swap the model - everything else stays the same
const model = openai("gpt-4o"); // OpenAI
const model = anthropic("claude-3-5-sonnet-20241022"); // Anthropic
const model = google("gemini-1.5-pro"); // Google
const model = ollama("llama3.2"); // Local via Ollama
References: Vercel AI SDK · GitHub · docs
Mahmudul Haque Qudrati
CEO & ML Engineer
Visionary leader with extensive experience in machine learning and software development. Drives strategic innovation and business growth.
More from Mahmudul
Related Articles
What is Codex starts encrypting sub-agent prompts? A Practical Overview
OpenAI Codex now encrypts sub-agent prompts by default. This change affects how agentic systems share context between sub-agents. Here's what it means for your AI pipelines.
Building reliable agentic AI systems: A Practical Overview
A practical guide to building reliable agentic AI systems covering structured outputs, observability, fallbacks, and cost controls with real code examples.
What is Harness engineering: Leveraging Codex in an agent-first world? A Practical Overview
Harness engineering is the practice of building structured, safe environments for AI agents to execute code. This post explains how to leverage OpenAI Codex in an agent-first world, with concrete examples, cost breakdowns, and honest tradeoffs.
// discussion
Comments