AI SDK Agents is an extension of the standard Vercel AI SDK API. It enables you to build advanced generative applications using patterns and agent compositions that are not supported by the Vercel AI SDK out of the box. With this library, you can define your own agents, compose them together, and call agents as tools within other agents, unlocking complex reasoning and orchestration flows.
- π Extends the Vercel AI SDK API to support new generative app patterns
- π€ Define and compose agents (agents can call each other as tools)
- π Flexible flow and step management
- π οΈ Extensible tool system
- π Context and memory between steps
- π Streaming response support
npm install ai-sdk-agents
# or
yarn add ai-sdk-agents
# or
pnpm add ai-sdk-agents
This package requires the Vercel AI SDK and zod as a peer dependency:
npm install ai zod
# or
yarn add ai zod
# or
pnpm add ai zod
Here's a basic example of how to use the library:
import { z } from 'zod';
import { generateId } from 'ai';
import { openai } from '@ai-sdk/openai';
import { agent, ChatFlow } from 'ai-sdk-agents';
// Math expert agent, exposed as a tool
const mathAgent = agent({
model: openai('gpt-4o'),
description: 'Math expert that can answer questions and help with tasks.',
system: 'You are a math expert that can answer questions and help with tasks.',
output: z.object({
answer: z.string().describe('The answer to the math problem'),
}),
asTool: {
input: z.object({
question: z.string().describe('The math problem to solve'),
}),
getPrompt: ({ question }) => ({
prompt: `Solve the following math problem: ${question}`,
}),
},
});
const assistantAgent = agent({
model: openai('gpt-4o'),
system: 'You are a helpful assistant that can answer questions and help with tasks.',
tools: { math: mathAgent },
maxSteps: 5,
toolChoice: 'auto',
});
const chat = new ChatFlow({ agent: assistantAgent });
const context = { history: [{ role: 'user', content: 'What is the square root of 144?' }] };
const { result } = await chat.run(context);
const { messages } = await result.response;
console.log(messages);
This example demonstrates how to build a streaming chat API where the assistant agent can automatically use the math agent as a tool to solve math problems in user queries. The response is streamed in real time to the client.
-
examples/contexts
Demonstrates how to extend the agent context with custom values (like dates) and inject them into system prompts. Shows how to use both regular tools and other agents as tools within your assistant, enabling dynamic, context-aware behavior and multi-step reasoning. -
examples/express
Shows how to build a streaming chat API using Express, powered by two agents: a main assistant and a math expert agent (used as a tool). Features real-time streaming responses, conversation history, and a/chat
endpoint for interactive queries.
src/
βββ agent.ts # Core agent implementation
βββ context.ts # Context management
βββ flow.ts # Flow control and management
βββ flows/ # Predefined flow implementations
β βββ chat-flow.ts # Chat flow implementation
βββ index.ts # Main entry point
βββ tools.ts # Tool definitions
βββ prompt.ts # Prompt handling
- Clone the repository
- Install dependencies:
pnpm install
pnpm test
- Run tests using Vitestpnpm lint:fix
- Run ESLint with auto-fixpnpm format
- Format code with Prettier
Example projects are located in the examples/
directory.
Contributions are welcome! Please feel free to submit a Pull Request.
This project is licensed under the MIT License - see the LICENSE.md file for details.
- Built on top of the Vercel AI SDK
- Heavily inspired by openai agent framework
- Uses Vitest for testing
- Inspired by Anthropic's "Building effective agents" for agentic patterns and best practices
- Add next.js examples
- Document public API and use cases
- Inter-Agent memory