Building AI Agents · Part 2 of 7 · Vercel AI SDK, Mastra & Providers · Oct 2026

Quick answer: The Vercel AI SDK is a TypeScript toolkit for building AI agents and apps on Node.js, React, Vue, Svelte and similar stacks. Mastra builds on it with memory, workflows and evaluations. You connect to models either through a gateway with one API key, or directly with each provider's own key.

Choosing an Agent Development Tool

Agent development tools (ADTs) give you the building blocks for agents: model access, prompts, tools, memory and orchestration. Which one you pick mostly depends on your programming language and stack:

ToolLanguageNotes
Vercel AI SDKTypeScriptWorks with Node.js, React, Next.js, Vue, Svelte and more. A foundation for other tools.
MastraTypeScriptAn agent framework that adds memory, workflows, processors and evaluations.
Laravel AI SDKPHPFirst-party Laravel package with agents, tools, streaming and broadcasting.
Agent Development Kit (ADK)Python and othersGoogle's framework for building and deploying agents.

The Vercel AI SDK

The Vercel AI SDK is a TypeScript SDK for building AI agents and AI applications with Node.js, React, Vue, Svelte and other web technologies. It gives you one consistent API across many model providers, so switching from one model to another is usually a one-line change.

Generating vs Streaming

The core library offers two ways to get text from a model:

  • generateText waits for the full response, then returns it. Use it for background jobs and anything a user isn't watching.
  • streamText sends the response piece by piece as the model produces it. Use it for anything user-facing.

For user experience, streaming is the better default. Users see the answer forming immediately instead of staring at a spinner.

import { streamText } from 'ai';

const result = streamText({
  model: 'anthropic/claude-sonnet-5.5',
  prompt: 'Explain VAT registration in South Africa in three bullet points.',
});

for await (const chunk of result.textStream) {
  process.stdout.write(chunk);
}

Earlier versions of the SDK had separate generateObject and streamObject functions for structured data. In AI SDK 7 these are deprecated: you now pass an output setting to generateText or streamText instead. We cover structured output in Part 5. Streaming React components (generative UI) lives in a separate, experimental part of the SDK.

Mastra

Mastra is a TypeScript agent framework. Its core building blocks are agents, tools, workflows, memory and scorers, which evaluate the quality of an agent's responses.

Mastra works closely with the AI SDK. If you already use the AI SDK directly and want Mastra capabilities like processors or memory without switching to Mastra's full agent API, its withMastra() function wraps any AI SDK model with those features. Processors can transform messages before they are sent to the model and after the response comes back. Memory automatically loads previous messages before each call and saves new ones afterwards.

Advertisement

Connecting to AI Providers

There are two ways to connect the AI SDK to models:

  1. Through a gateway. You get one API key from the gateway (for example, the Vercel AI Gateway) and reach many providers' models with it. This is a good fit if you don't want to manage separate accounts and keys for each provider.
  2. Directly. You get an API key from each provider, such as Anthropic, OpenAI or Google, and install that provider's package.

Provider-Specific Tools

Providers offer different features and different built-in tools that run on their side. For example, Anthropic provides web search, web fetch, code execution, bash, text editor and memory tools. OpenAI provides web search and a file search tool that searches documents you have uploaded to a vector store. Check each provider's documentation, because the available tools and their capabilities differ.

Provider Settings

You can control how a model behaves on each call. Common settings in the AI SDK include:

SettingWhat it does
maxOutputTokensLimits how long the response can be.
temperatureControls randomness. Lower values give more predictable output.
maxRetriesHow many times to retry a failed call.
timeoutAborts the call if it takes longer than the given number of milliseconds.
reasoningControls how much the model reasons before answering, on models that support it.
providerOptionsProvider-specific settings that aren't shared across providers.
stopWhenFor multi-step agents: when to stop the loop (Part 5).
import { generateText } from 'ai';

const result = await generateText({
  model: 'anthropic/claude-sonnet-5.5',
  prompt: 'Summarise this month\'s cash flow position.',
  temperature: 0.2,
  maxOutputTokens: 800,
  timeout: 10000, // abort after 10 seconds
});

Choosing between a cheaper, faster model and a more capable, slower one isn't a setting. It's a choice of which model you pass in. A common pattern is to send simple, high-volume tasks to a small model and reserve the most capable model for complex reasoning.

What's Next

In Part 3 we look at prompts, the instructions that tell a model what to do, and how to structure them for reliable agent behaviour.

Cover photo: Igor Omilaev on Unsplash

Chainbook builds intelligent business systems for African professionals, and AI agents are at the heart of how we build them.