SKILL PROCEDURE

AI SDK

Vercel AI SDK — provider-agnostic TypeScript toolkit for building streaming AI applications and agents. Use when generating or streaming text with LLMs, wiring model providers (OpenAI, Anthropic, Google), implementing tool calling, producing structured output with Zod, building chat UIs with useChat, or composing multi-step agents with ToolLoopAgent. Published by HardGraph, a curated graph of provenance-backed knowledge for AI agents.

aillmstreamingtool-callingtypescript
BEGINNER GUIDE

Understand AI SDK before using it

CATEGORY

AI SDK is catalogued under AI and agents.

START HERE WHEN

Your work repeatedly involves the concepts tagged above. Open the full procedure below when the current task matches them.

Compare related skills

SKILLCATEGORYSHARED CONCEPTSEXPLANATION
AI SDKAI and agentsCurrent skillVercel AI SDK — provider-agnostic TypeScript toolkit for building streaming AI applications and agents. Use when generating or streaming text with LLMs, wiring model providers (OpenAI, Anthropic, Google), implementing tool calling, producing structured output with Zod, building chat UIs with useChat, or composing multi-step agents with ToolLoopAgent. Published by HardGraph, a curated graph of provenance-backed knowledge for AI agents.
Assistant UIAI and agents
aistreaming
assistant-ui — React components for building AI chat interfaces. Use when building a chat UI (thread list, message composer, markdown rendering, tool-call parts, streaming), wiring it to a model runtime (Vercel AI SDK, LangChain, custom), using the CLI to scaffold and add components, adapting a shadcn/Radix/Base UI style, or running it on web, React Native, or the terminal. Published by HardGraph, a curated graph of provenance-backed knowledge for AI agents.
ComposioAI and agents
tool-calling
Composio — a tool-calling platform that sits between an LLM/agent framework and external apps, giving an agent connected tools, managed authentication, tool search, triggers, a sandboxed workbench, and an MCP bridge. Use when an agent needs to take real actions in 1000+ SaaS/developer apps (Gmail, GitHub, Slack, Notion, Linear, etc.) without hand-writing each integration, when you want OAuth/API-key auth handled per connected account, when you need the same tools reachable over Model Context Protocol, or when wiring tools into OpenAI, Anthropic, LangChain, Autogen, CrewAI, LlamaIndex, Mastra, or the Vercel AI SDK.
eveAI and agents
typescript
Vercel eve — a filesystem-first framework for durable backend AI agents. Use when creating, editing, debugging, or operating an eve agent project: authoring instructions, defining typed tools, on-demand skills, message channels (Slack/Discord/HTTP), scheduled cron jobs, subagents, sandboxes, human-in-the-loop prompts, model/runtime config, and spend guards. Covers project layout, agent.ts, defineAgent/defineTool, extensions, and the bundled docs. Published by HardGraph, a curated graph of provenance-backed knowledge for AI agents.

Vercel AI SDK

What is HardGraph? HardGraph publishes curated, provenance-backed agent skills grounded in reproducible vendor documentation.

The AI SDK is a provider-agnostic TypeScript toolkit for building AI-powered applications and agents. It runs on React, Next.js, Vue, Svelte, Node.js, and other JavaScript runtimes. A model call stays the same shape whether the provider is OpenAI, Anthropic, Google, Mistral, or a custom one.

Mental model

Two layers, kept deliberately separate:

| Layer | Package | What it does | | --------------- | ------------------------------------------------ | ---------------------------------------------------------------------------------------------------------------------- | | AI SDK Core | ai | Server-side model calls: generateText, streamText, generateObject, streamObject, tools, embeddings, providers. | | AI SDK UI | @ai-sdk/react, @ai-sdk/svelte, @ai-sdk/vue | Framework hooks for chat and generative UI: useChat, useCompletion, useObject. |

A provider package (@ai-sdk/openai, @ai-sdk/anthropic, …) plugs a vendor's models into Core. UI talks to a route handler that calls Core; it never imports a provider directly.

Resolving versions

Resolve the current SDK and model versions from the registry rather than recalling one. Training-data versions are reliably wrong.

npm view ai version          # Core
npm view @ai-sdk/react version
npm view @ai-sdk/openai version

The major version of ai and the provider/adapter packages move together on a release. When upgrading, bump ai, the provider packages, and the UI adapter as a set — a mixed set is the most common source of breakage.

Core calls

import { generateText, streamText } from "ai";
import { openai } from "@ai-sdk/openai";

// One-shot
const { text } = await generateText({
  model: openai("gpt-4o"),
  prompt: "Summarise the AI SDK in one sentence.",
});

// Streaming
const result = streamText({
  model: openai("gpt-4o"),
  prompt: "Tell me a story.",
});
for await (const delta of result.textStream) {
  process.stdout.write(delta);
}

Structured output

Constrain output to a schema with generateObject / streamObject and a Zod definition:

import { generateObject } from "ai";
import { z } from "zod";

const { object } = await generateObject({
  model: openai("gpt-4o"),
  schema: z.object({
    name: z.string(),
    rating: z.number().min(0).max(10),
  }),
  prompt: "Describe the AI SDK.",
});

Tool calling

Define tools with tool and a Zod parameters schema; the model decides when to call them and you return a result it consumes:

import { generateText, tool } from "ai";
import { z } from "zod";

const { text } = await generateText({
  model: openai("gpt-4o"),
  tools: {
    weather: tool({
      description: "Get the weather for a city",
      parameters: z.object({ city: z.string() }),
      execute: async ({ city }) => fetchWeather(city),
    }),
  },
  maxSteps: 5, // lets the model call a tool and use the result
  prompt: "What is the weather in Hanoi?",
});

maxSteps (previously maxToolRoundtrips) enables multi-step loops where the model calls a tool, reads the result, and continues.

Chat UI

useChat from @ai-sdk/react connects a React component to a route handler that streams Core output. Use DefaultChatTransport to point it at your endpoint:

import { useChat } from "@ai-sdk/react";
import { DefaultChatTransport } from "ai";

const { messages, sendMessage, status } = useChat({
  transport: new DefaultChatTransport({ api: "/api/chat" }),
});

Messages are UIMessage objects made of typed parts (TextUIPart, ToolUIPart, FileUIPart, …) — render by part type rather than treating message.content as a plain string.

Agents

ToolLoopAgent runs a model that can call tools across multiple steps with stop conditions, human-in-the-loop approval, and an MCP client for external tool servers. Reach for it when a single maxSteps loop is not enough — you need guarded execution, sub-agents, or dynamic tool selection.

Error handling

Catch provider errors by type rather than sniffing strings:

  • APICallError — the provider rejected the request (auth, quota, bad input).
  • NoSuchToolError — the model returned a tool you did not define.
  • UIMessageStreamError — a UI stream surfaced an error part.

Current vs deprecated

  • Use maxSteps for multi-step tool calls; maxToolRoundtrips is the old name.
  • UI messages are part-based (UIMessage); the legacy string content model is gone in v4+.
  • Prefer streamText over manual chunked generation; prefer generateObject over prompt-engineering JSON.

References

Hardgraph / curated knowledge for agents.

STATIC EXPORT · CANONICAL SOURCE