Alejandro Rioja.
AI Agents

如何在15分钟内构建你的第一个AI智能体

Alejandro Rioja
Alejandro Rioja
3 分钟阅读
TL;DR

你不需要框架、课程或博士学位。你需要的是Node.js、Anthropic SDK,以及25行TypeScript。本教程将构建一个真实可用的智能体——一个结构化内容摘要器,你可以在同一次会话中将它部署到Cloudflare。唯一的前提条件是一个免费的API密钥。

免费新闻通讯

每周三。28,400+ 读者。纯干货。

目录

2026年6月更新。

TL;DR: 你不需要框架、课程或博士学位。你需要的是Node.js、Anthropic SDK,以及25行TypeScript。本教程将构建一个真实可用的智能体——一个结构化内容摘要器,你可以在同一次会话中将它部署到Cloudflare。唯一的前提条件是一个免费的API密钥。

[运营者视角] 我从想用AI做自动化的创始人那里最常听到的一句话是”我得先多学一些”。其实不必。智能体的模式很简单,而理解它最快的方式就是亲手做一个。下面是如果我今天从零开始会走的确切路径。

为什么大多数”构建AI智能体”教程帮不到你

它们要么使用Python(对ML工程师没问题,但对其他所有人都是阻力),要么把真正的代码藏在LangChain之类的框架背后,要么构建出过于抽象、无法与你的实际工作相连的东西。

本教程在三个方面有所不同:

  1. 仅用TypeScript —— 如果你写过JavaScript,就能跟上这篇教程
  2. 不用框架 —— 你将看到每一行接触模型的代码
  3. 有用的输出 —— 你将构建一个结构化摘要器,可以真正用在客户邮件、评论或会议记录上

你将构建什么

一个内容摘要智能体:粘贴任意一段文本,返回格式一致的结构化摘要。一个HTTP请求进,一份干净的摘要出。

为什么把它作为第一个项目:这个模式——系统提示 + 用户输入 → 结构化输出——是我运行的每一个智能体的基础。替换系统提示,你就得到一个问答器、一个语气改写器、一个分类器或一个草稿生成器。学会这一次,你就掌握了生产环境智能体实际工作内容的80%。

前提条件(2分钟)

不用Docker。不用虚拟环境。不用pip install任何东西。

第1步:创建项目(2分钟)

bash
mkdir my-first-agent && cd my-first-agent
npm init -y
npm install @anthropic-ai/sdk
npm install -D tsx typescript

package.json中添加一个脚本,以便轻松运行智能体:

json
{
  "scripts": {
    "agent": "tsx agent.ts"
  }
}

第2步:编写智能体(5分钟)

创建agent.ts并粘贴以下内容:

typescript
import Anthropic from "@anthropic-ai/sdk";

const client = new Anthropic({
  apiKey: process.env.ANTHROPIC_API_KEY,
});

const SYSTEM_PROMPT = `You are a precise content summarizer. When given any block of text, return a structured summary in this exact format:

**One-line summary:** <the core point in one sentence>

**Key points:**
- <point 1>
- <point 2>
- <point 3 if applicable>

**Action item (if any):** <one concrete next step, or "None">

Be specific. No filler. Under 150 words total.`;

async function summarize(text: string): Promise<string> {
  const message = await client.messages.create({
    model: "claude-haiku-4-5",
    max_tokens: 512,
    system: SYSTEM_PROMPT,
    messages: [{ role: "user", content: text }],
  });

  const block = message.content[0];
  if (block.type !== "text") throw new Error("Unexpected response type");
  return block.text;
}

const sample = `
  Hey team — following up on the Q2 review meeting.
  We agreed to push the launch to July 15th instead of June 30th
  due to the payment integration delay. Marketing needs the new
  landing page copy by June 20th or we can't start the email campaign.
  Budget for the launch campaign is confirmed at $8,000.
  Please confirm receipt.
`;

const result = await summarize(sample);
console.log(result);

第3步:运行它(1分钟)

bash
ANTHROPIC_API_KEY=sk-ant-... npm run agent

预期输出:

code
**One-line summary:** Launch pushed to July 15th due to payment delay; landing page copy needed by June 20th to unblock email campaign.

**Key points:**
- Launch date moved from June 30th to July 15th
- Landing page copy deadline: June 20th (blocks email campaign)
- Campaign budget confirmed at $8,000

**Action item (if any):** Confirm receipt and deliver landing page copy by June 20th.

这就是一个可用的AI智能体。真实输入、自定义系统提示、结构化输出。整个东西只有30行代码。

第4步:根据你的使用场景进行定制

系统提示是让这个智能体成为你专属智能体的唯一要素。下面是三个可以直接替换使用的方案:

客户评论分类器:

code
Classify this customer review as POSITIVE, NEGATIVE, or MIXED.
Then extract the main complaint or praise in one sentence.
Format: SENTIMENT: <label>
RESUME: <one sentence>

会议记录 → 行动项:

code
Extract all action items from this meeting transcript.
Format each as: [OWNER if mentioned] [ACTION] by [DEADLINE if mentioned]
If owner or deadline is not stated, leave those fields blank.
Return a numbered list. No preamble.

面向社交媒体的语气改写器:

code
Rewrite this text in a direct, confident, first-person tone for LinkedIn.
Remove hedging language ("I think", "maybe", "sort of").
Keep it under 200 words. Return only the rewritten text, no commentary.

这些都是同一个30行的智能体,只是换了不同的系统提示。你不需要为每一个都新建一个项目——只需更新SYSTEM_PROMPT并重新运行即可。

第5步:部署到生产环境

本地脚本是原型。生产环境的智能体会按计划运行,或响应事件而运行。我采用的路径:Cloudflare Workers。

Workers每天10万次请求以内免费,几秒钟即可部署,并且在你无需管理任何基础设施的情况下,为你提供cron触发器、KV存储和队列。

安装Wrangler并搭建一个Worker脚手架:

bash
npm install -g wrangler
wrangler init my-agent-worker --template worker-typescript
cd my-agent-worker
npm install @anthropic-ai/sdk

将你的API密钥存储为密文(绝不要写在代码里):

bash
wrangler secret put ANTHROPIC_API_KEY

src/index.ts替换为:

typescript
import Anthropic from "@anthropic-ai/sdk";

export default {
  async fetch(request: Request, env: Env): Promise<Response> {
    const { text } = await request.json<{ text: string }>();

    const client = new Anthropic({ apiKey: env.ANTHROPIC_API_KEY });
    const message = await client.messages.create({
      model: "claude-haiku-4-5",
      max_tokens: 512,
      system: YOUR_SYSTEM_PROMPT,
      messages: [{ role: "user", content: text }],
    });

    const block = message.content[0];
    if (block.type !== "text") return Response.json({ error: "Bad response" }, { status: 500 });
    return Response.json({ summary: block.text });
  },
};

部署:

bash
wrangler deploy

现在你在https://my-agent-worker.<your-subdomain>.workers.dev上有了一个实时的API端点。可以从Zapier、Google Sheets脚本、移动应用——任何能发起HTTP请求的地方调用它。

接下来构建什么

一旦基本模式跑通,复杂度会以三种形式出现:

工具使用(tool use) —— 让模型决定何时调用外部API。模型不再总是运行相同的逻辑,而是根据输入选择调用哪个工具。这就是你构建能够搜索、写入数据库或发送通知的智能体的方式。

链式调用(chaining) —— 把一个智能体的输出作为另一个智能体的输入。一个分类器路由到一个应答器;一个摘要器喂给一个翻译器。我在生产环境中运行的大多数智能体都是两步或三步的链条,而非单次调用。

cron触发器 —— 在wrangler.toml中添加[triggers] crons = ["0 8 * * *"],你的智能体就会按计划运行。无需轮询,无需常开服务器。这正是我用来运营这个网站的内容流水线的工作方式。

我在我用来运行30多个生产环境智能体的智能体技术栈中用完整的TypeScript代码讲解了这三种模式——那篇文章正好从本文结束之处接续下去。

运营者的底线结论

从”考虑构建一个智能体”到”拥有一个已部署的智能体”之间的差距,大约是15分钟和30行代码。系统提示就是产品本身——把你的精力投入在那里,而不是花在挑选框架上。如果这个智能体每周为你节省一小时,那就是每年50小时。先构建简单版本,看它是否物有所值,然后只在使用场景确有需要时再增加复杂度。

继续阅读

将AI实战手册发送到您的邮箱

每周三。28,400+ 读者。纯干货。

↵ 查看全部结果 esc esc 关闭