@deepagents/agent

中文文档
7.5.2发布于 2026/11/08MITTypeScriptES Module构建来源已验证近 30 天下载 2717

A framework for building multi-agent AI systems with TypeScript. Create agents that use tools, coordinate through handoffs, and work together to solve complex tasks.

原始使用文档
文档目录16 节
  1. Features
  2. Installation
  3. Quick Start
  4. Simple Agent
  5. Agent with Tools
  6. Multi-Agent with Handoffs
  7. Structured Output
  8. Context Variables
  9. API Reference
  10. agent(config)
  11. instructions({ purpose, routine })
  12. Execution Functions
  13. Utilities
  14. AI Model Providers
  15. Documentation
  16. Repository

@deepagents/agent

A framework for building multi-agent AI systems with TypeScript. Create agents that use tools, coordinate through handoffs, and work together to solve complex tasks.

Features

  • Agent Composition - Build modular agents with specific roles and capabilities
  • Tool Integration - Compatible with Vercel AI SDK tools
  • Handoffs - Agents can delegate to specialized agents automatically
  • Structured Output - Type-safe responses with Zod schemas
  • Streaming - Real-time streaming responses
  • Context Sharing - Type-safe state passed between agents

Installation

npm install @deepagents/agent

Requires Node.js LTS and a zod peer dependency.

Quick Start

Simple Agent

import { openai } from '@ai-sdk/openai';

import { agent, execute, user } from '@deepagents/agent';

const assistant = agent({
  name: 'assistant',
  model: openai('gpt-4o'),
  prompt: 'You are a helpful assistant.',
});

const stream = execute(assistant, 'Hello!', {});

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

Agent with Tools

import { openai } from '@ai-sdk/openai';
import { tool } from 'ai';
import { z } from 'zod';

import { agent, execute } from '@deepagents/agent';

const weatherTool = tool({
  description: 'Get weather for a location',
  parameters: z.object({
    location: z.string(),
  }),
  execute: async ({ location }) => {
    return { temperature: 72, condition: 'sunny', location };
  },
});

const weatherAgent = agent({
  name: 'weather_agent',
  model: openai('gpt-4o'),
  prompt: 'You help users check the weather.',
  tools: { weather: weatherTool },
});

const stream = execute(weatherAgent, 'What is the weather in Tokyo?', {});
console.log(await stream.text);

Multi-Agent with Handoffs

import { openai } from '@ai-sdk/openai';

import { agent, instructions, swarm } from '@deepagents/agent';

const researcher = agent({
  name: 'researcher',
  model: openai('gpt-4o'),
  prompt: 'You research topics and provide detailed information.',
  handoffDescription: 'Handles research and fact-finding tasks',
  tools: {/* research tools */},
});

const writer = agent({
  name: 'writer',
  model: openai('gpt-4o'),
  prompt: 'You write clear, engaging content based on research.',
  handoffDescription: 'Handles writing and content creation',
});

const coordinator = agent({
  name: 'coordinator',
  model: openai('gpt-4o'),
  prompt: instructions({
    purpose: ['Coordinate research and writing tasks'],
    routine: [
      'Analyze the request',
      'Use transfer_to_researcher for fact-finding',
      'Use transfer_to_writer for content creation',
    ],
  }),
  handoffs: [researcher, writer],
});

// Agents automatically transfer control via transfer_to_<agent_name> tools
const stream = swarm(coordinator, 'Write a blog post about AI agents', {});

Structured Output

import { openai } from '@ai-sdk/openai';
import { z } from 'zod';

import { agent, generate } from '@deepagents/agent';

const analyzer = agent({
  name: 'analyzer',
  model: openai('gpt-4o'),
  prompt: 'Analyze the sentiment of the given text.',
  output: z.object({
    sentiment: z.enum(['positive', 'negative', 'neutral']),
    confidence: z.number(),
    keywords: z.array(z.string()),
  }),
});

const result = await generate(analyzer, 'I love this product!', {});
console.log(result.output);
// { sentiment: 'positive', confidence: 0.95, keywords: ['love', 'product'] }

Context Variables

Share state between agents and tools:

import { type ToolExecutionOptions, tool } from 'ai';
import { z } from 'zod';

import { agent, execute } from '@deepagents/agent';

// A `type`, not an `interface`: the AI SDK requires a tool context to
// extend Record<string, unknown>, which an interface does not.
type AppContext = {
  userId: string;
  preferences: Record<string, string>;
};

const preferenceTool = tool({
  description: 'Save user preference',
  inputSchema: z.object({
    key: z.string(),
    value: z.string(),
  }),
  execute: async (
    { key, value },
    { context }: ToolExecutionOptions<AppContext>,
  ) => {
    context.preferences[key] = value;
    return `Saved ${key}=${value}`;
  },
});

const assistant = agent<unknown, AppContext>({
  name: 'assistant',
  model: openai('gpt-4o'),
  prompt: (ctx) => `Help user ${ctx?.userId} manage their preferences.`,
  tools: { savePreference: preferenceTool },
});

const context: AppContext = { userId: 'user123', preferences: {} };
const stream = execute(assistant, 'Set my theme to dark', context);
await stream.text;

console.log(context.preferences); // { theme: 'dark' }

API Reference

agent(config)

Creates a new agent:

agent<Output, ContextIn, ContextOut>({
  name: string;                    // Agent identifier (required)
  model: LanguageModel;            // AI SDK model (required)
  prompt: string | string[] | ((ctx?) => string);  // Instructions
  tools?: ToolSet;                 // Available tools
  handoffs?: Agent[];              // Agents to delegate to
  handoffDescription?: string;     // When to use this agent
  output?: z.Schema<Output>;       // Structured output schema
  temperature?: number;            // LLM temperature
  toolChoice?: ToolChoice;         // 'auto' | 'none' | 'required'
})

instructions({ purpose, routine })

Helper for structured prompts:

instructions({
  purpose: string | string[],  // Agent's role
  routine: string[],           // Step-by-step workflow
})

// For swarm coordination
instructions.swarm({ purpose, routine })

Execution Functions

// Streaming execution
execute(agent, messages, context, config?)
stream(agent, messages, context, config?)  // alias

// Non-streaming execution
generate(agent, messages, context, config?)

// High-level UI streaming with handoff support
swarm(agent, messages, context, abortSignal?)

Utilities

// Create user message
user(message: string): UIMessage

// Extract structured output
toOutput<T>(result): Promise<T>

AI Model Providers

Works with any model provider supported by the Vercel AI SDK:

import { anthropic } from '@ai-sdk/anthropic';
import { google } from '@ai-sdk/google';
import { groq } from '@ai-sdk/groq';
import { openai } from '@ai-sdk/openai';

const agent1 = agent({ model: openai('gpt-4o') /* ... */ });
const agent2 = agent({
  model: anthropic('claude-sonnet-4-20250514') /* ... */,
});
const agent3 = agent({ model: google('gemini-1.5-pro') /* ... */ });

Documentation

Full documentation available at januarylabs.github.io/deepagents.

Repository

github.com/JanuaryLabs/deepagents

版本与发布记录

@deepagents/agent 共发布 82 个版本,首次发布于 2025/10/22;过去 12 个月发布了 79 个版本。最新版本附带 npm provenance 构建来源证明,可追溯到公开的 CI 构建记录。

版本发布日期距今
7.5.2latest2026/11/083 天前
7.5.12026/11/083 天前
7.5.02026/11/074 天前
7.4.02026/11/074 天前
7.3.02026/10/3112 天前
7.2.02026/10/2715 天前

关于 @deepagents/agent 的常见问题

@deepagents/agent 是什么?

A framework for building multi-agent AI systems with TypeScript. Create agents that use tools, coordinate through handoffs, and work together to solve complex tasks.

如何安装 @deepagents/agent?

在项目目录运行 npm install @deepagents/agent,或使用 pnpm add / yarn add / bun add。它还需要宿主项目提供 peer 依赖:zod(^4.4.3)。npm 7 及以上与 pnpm 8 及以上默认会自动安装必需的 peer 依赖,Yarn 需要手动安装。

@deepagents/agent 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@deepagents/agent 支持 ESM 还是 CommonJS?

以 ES Module 形式发布,推荐使用 import 引入。在 CommonJS 项目中,Node.js 20.19+ / 22.12+ 可以直接 require() 不含顶层 await 的 ES Module,更早的版本需要改用动态 import()。

@deepagents/agent 可以商用吗?

@deepagents/agent 使用 MIT 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

@deepagents/agent 的下载量有多少?

根据 npm 官方下载统计,@deepagents/agent 在 2026/10/12 – 2026/11/10 这 30 天内被下载 2,717 次,日均约 91 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@deepagents/agent 还在维护吗?

最新版本 7.5.2 发布于 2026/11/08(3 天前);至今共发布 82 个版本,其中过去 12 个月发布了 79 个;首次发布于 2025/10/22。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。