@21st-sdk/agent

中文文档
0.0.18发布于 2026/04/29MITTypeScriptESM + CJS近 30 天下载 3264

Define AI agents and tools with full type inference for 21st Agents

原始使用文档
文档目录8 节
  1. Install
  2. Quick Start
  3. API
  4. agent(config)
  5. tool(definition)
  6. Hooks
  7. How It Works
  8. License

@21st-sdk/agent

Define AI agents with full type inference. The config layer for 21st Agents.

Install

npm install @21st-sdk/agent zod

Quick Start

Create src/agent.ts:

import { agent, tool } from "@21st-sdk/agent"
import { z } from "zod"

export default agent({
  model: "claude-sonnet-4-6",
  systemPrompt: "You are a helpful coding assistant.",
  tools: {
    greet: tool({
      description: "Greet a user by name",
      inputSchema: z.object({ name: z.string() }),
      execute: async ({ name }) => ({
        content: [{ type: "text", text: `Hello, ${name}!` }],
      }),
    }),
  },
})

Then deploy with the 21st SDK CLI:

npx @21st-sdk/cli login
npx @21st-sdk/cli deploy

API

agent(config)

Creates an agent configuration. Returns the config with a _type: "agent" tag for the runtime.

export default agent({
  // Model to use (default: "claude-sonnet-4-6")
  model: "claude-sonnet-4-6",

  // For "claude-code" runtime, string prompts are appended
  // to Claude Code's preset system prompt by default.
  systemPrompt: "You are a PR reviewer...",

  // Custom tools the agent can use
  tools: {
    lint: tool({ ... }),
    test: tool({ ... }),
  },

  // Runtime: "claude-code" (default) or "codex"
  runtime: "claude-code",

  // Permission mode: "default", "acceptEdits", or "bypassPermissions"
  permissionMode: "default",

  // Max turns before stopping (default: 50)
  maxTurns: 50,

  // Max budget in USD
  maxBudgetUsd: 5,

  // Lifecycle hooks
  onStart: async () => { ... },
  onToolCall: async ({ toolName, input }) => { ... },
  onToolResult: async ({ toolName, input, output }) => { ... },
  onStepFinish: async ({ step }) => { ... },
  onFinish: async ({ result }) => { ... },
  onError: async ({ error }) => { ... },
})

tool(definition)

Creates a tool definition with Zod schema validation.

import { tool } from "@21st-sdk/agent"
import { z } from "zod"

const myTool = tool({
  description: "What this tool does",
  inputSchema: z.object({
    path: z.string(),
    verbose: z.boolean().optional(),
  }),
  execute: async (args) => {
    // args is fully typed from the schema
    return {
      content: [{ type: "text", text: "result" }],
    }
  },
})

Hooks

onToolCall

Runs before a tool executes. Return false to block it.

onToolCall: async ({ toolName, input }) => {
  if (toolName === "Bash" && input.command?.includes("rm -rf")) {
    return false // blocked
  }
}

onFinish

Runs when the agent completes successfully.

onFinish: async ({ result }) => {
  await fetch("https://my-api.com/webhook", {
    method: "POST",
    body: JSON.stringify({ done: true }),
  })
}

How It Works

agent() and tool() are identity functions — they return exactly what you pass in, with type inference added. The actual execution happens in the 21st Agents runtime (E2B sandbox) using the Claude Agent SDK.

Your code runs in a secure cloud sandbox with full access to Node.js, git, and system tools.

For runtime: "claude-code", omitting systemPrompt uses Claude Code's default preset system prompt. If you pass a string, it is appended to that preset automatically. You can also pass the preset object directly:

systemPrompt: {
  type: "preset",
  preset: "claude_code",
  append: "Your extra instructions here",
}

License

MIT

版本与发布记录

@21st-sdk/agent 共发布 13 个版本,首次发布于 2026/03/06;过去 12 个月发布了 13 个版本。

版本发布日期距今
0.0.18latest2026/04/296 个月前
0.0.162026/04/087 个月前
0.0.152026/04/077 个月前
0.0.142026/03/197 个月前
0.0.132026/03/197 个月前
0.0.122026/03/187 个月前

关于 @21st-sdk/agent 的常见问题

@21st-sdk/agent 是什么?

Define AI agents and tools with full type inference for 21st Agents

如何安装 @21st-sdk/agent?

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

@21st-sdk/agent 支持 TypeScript 吗?

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

@21st-sdk/agent 支持 ESM 还是 CommonJS?

同时提供 ES Module 与 CommonJS 入口,import 和 require() 都可以使用,打包工具会自动选择合适的版本。

@21st-sdk/agent 可以商用吗?

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

@21st-sdk/agent 的下载量有多少?

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

@21st-sdk/agent 还在维护吗?

最新版本 0.0.18 发布于 2026/04/29(6 个月前);至今共发布 13 个版本,其中过去 12 个月发布了 13 个;首次发布于 2026/03/06。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。