你大概已经写过一个套壳聊天框,调过几次 API,也许还试过 AutoGPT —— 看着它自己跟自己聊了二十轮、烧了五美元、绕回原地。现在「agent」是 AI 圈最被滥用的词:每个框架都说自己在做 agent,每条推都在发 agent。这一章不教你建,先帮你把线画清:agent 到底是什么,以及更有用的那一半 —— 它不是什么。You've probably built a chat wrapper, called an API a few times, maybe even tried AutoGPT — and watched it talk to itself for twenty rounds, burn five dollars, and loop back to where it started. 'Agent' is now the most overloaded word in AI: every framework claims to build one, every other tweet ships one. This chapter doesn't teach you to build — it draws the line first: what an agent actually is, and the more useful half — what it isn't.这本书是一个从零动手的项目:从一个只会聊天的 LLM 出发,一章加一种能力,直到它能在真实环境里干活。但动手之前,得先有判断 —— 不然你会把一个本该是一行函数的活,建成一个会绕圈的 agent。This book is a hands-on project from zero: start with an LLM that only chats, add one capability per chapter, until it can do real work. But before the hands, you need judgment — otherwise you'll build a looping agent for a job that should have been a one-line function.动手有动手的对象。这本书从头到尾建同一个东西:一个研究 / 分析助手 —— 你给它一个问题,它去搜、去抓网页、在沙箱里跑分析代码、记住中间结论,最后回一份带引用的报告。每一章给它加一种能力,到最后你手里是一个完整、能跑的 agent,不是一堆零散片段。Hands-on needs something to work on. From start to finish this book builds one thing: a research / analysis assistant — you give it a question, it searches, fetches pages, runs analysis code in a sandbox, remembers intermediate findings, and returns a cited report. Each chapter adds it one capability, and by the end you hold a complete, runnable agent — not a pile of scattered snippets.这本书分两部分。第一部分把 agent 从 20 行循环建到能上生产 —— 工具、记忆、推理、MCP、多 agent、安全、评估、部署、选型。到那时它「能跑」。第二部分(企业篇)回答一个不同的问题:怎么让它值得被放进一家公司。这两件事的差距,不是一个更强的模型,而是一整层工程 —— 让 agent 可信地、规模化地、在别人的数据上、留得下审计地行动。这一层有八根支柱:身份鉴权、带权限的检索、对抗性安全、治理审计、常设评测、失败工程、控制平面、多租户。它们不是功能清单,是「能跑」到「企业级」之间那道差集。This book has two parts. Part one builds the agent from a 20-line loop to production-ready — tools, memory, reasoning, MCP, multi-agent, safety, evaluation, deployment, framework choice. By then it runs. Part two, the enterprise part, answers a different question: what makes it worth placing inside a company. The gap between the two is not a stronger model but a whole layer of engineering — letting the agent act trustworthily, at scale, on other people's data, with an audit trail left behind. That layer has eight pillars: identity and auth, permission-aware retrieval, adversarial security, governance and audit, standing evaluation, failure engineering, a control plane, and multi-tenancy. Not a feature list — the difference set between 'runs' and 'enterprise-grade.'
— I
Agent 不是更大的 promptAn Agent Is Not a Bigger Prompt.
把 prompt 写长、把对话拉长,都不会变成 agent。分界只有一条:下一步是谁决定的。A longer prompt or a longer conversation never becomes an agent. There is one dividing line: who decides the next step.Anthropic 把这类系统分两种:workflow 是「LLM 和工具被预定义的代码路径编排」,agent 是「LLM 自己动态决定流程和工具用法」。1注 1Note 1Anthropic · 「Building Effective Agents」(Erik Schluntz、Barry Zhang,2024-12-19)—— 区分 workflow(LLM 与工具被预定义代码路径编排)与 agent(LLM 自己动态决定流程与工具);建议「find the simplest solution possible」,只在复杂度确实改善结果时才加。Anthropic · 'Building Effective Agents' (Erik Schluntz, Barry Zhang, 2024-12-19) — distinguishes workflows (LLMs and tools orchestrated through predefined code paths) from agents (LLMs dynamically directing their own process and tools); advises finding the simplest solution and adding complexity only when it demonstrably improves outcomes.区别不在规模,在自主权 —— 流程图是你写死的,还是模型在运行时自己挑的。Anthropic splits these systems in two: a workflow is one where 'LLMs and tools are orchestrated through predefined code paths,' while an agent is one where 'LLMs dynamically direct their own processes and tool usage.'1注 1Note 1Anthropic · 「Building Effective Agents」(Erik Schluntz、Barry Zhang,2024-12-19)—— 区分 workflow(LLM 与工具被预定义代码路径编排)与 agent(LLM 自己动态决定流程与工具);建议「find the simplest solution possible」,只在复杂度确实改善结果时才加。Anthropic · 'Building Effective Agents' (Erik Schluntz, Barry Zhang, 2024-12-19) — distinguishes workflows (LLMs and tools orchestrated through predefined code paths) from agents (LLMs dynamically directing their own process and tools); advises finding the simplest solution and adding complexity only when it demonstrably improves outcomes. The difference is not scale but autonomy — whether you hard-coded the flowchart, or the model picks the path at run time.多数人以为自己要的是 agent,其实要的是 workflow。Anthropic 的建议很直接:先找最简单的方案,只在复杂度确实改善结果时才加。1注 1Note 1Anthropic · 「Building Effective Agents」(Erik Schluntz、Barry Zhang,2024-12-19)—— 区分 workflow(LLM 与工具被预定义代码路径编排)与 agent(LLM 自己动态决定流程与工具);建议「find the simplest solution possible」,只在复杂度确实改善结果时才加。Anthropic · 'Building Effective Agents' (Erik Schluntz, Barry Zhang, 2024-12-19) — distinguishes workflows (LLMs and tools orchestrated through predefined code paths) from agents (LLMs dynamically directing their own process and tools); advises finding the simplest solution and adding complexity only when it demonstrably improves outcomes.一个能用固定步骤说清的活,写成 agent 只会更慢、更贵、更难调。Most people think they want an agent when they want a workflow. Anthropic's advice is blunt: find the simplest solution first, and add complexity only when it demonstrably improves outcomes.1注 1Note 1Anthropic · 「Building Effective Agents」(Erik Schluntz、Barry Zhang,2024-12-19)—— 区分 workflow(LLM 与工具被预定义代码路径编排)与 agent(LLM 自己动态决定流程与工具);建议「find the simplest solution possible」,只在复杂度确实改善结果时才加。Anthropic · 'Building Effective Agents' (Erik Schluntz, Barry Zhang, 2024-12-19) — distinguishes workflows (LLMs and tools orchestrated through predefined code paths) from agents (LLMs dynamically directing their own process and tools); advises finding the simplest solution and adding complexity only when it demonstrably improves outcomes. A job you can spell out in fixed steps only gets slower, costlier, and harder to debug as an agent.
— II
Agent 是一个循环An Agent Is a Loop.
剥掉所有框架,agent 就是一个循环:感知、决策、行动、观察,然后再来。Strip away every framework and an agent is a loop: perceive, decide, act, observe, then again.每一轮,模型看到目标和当前的观察,决定调哪个工具;工具的结果回灌成下一轮的观察,直到任务完成或触发停止条件。ReAct 把这个循环写成「推理和行动交替」。2注 2Note 2Yao et al. · 「ReAct: Synergizing Reasoning and Acting in Language Models」(2022,arXiv:2210.03629)—— 把推理(reason)与行动(act)交替成一个循环,是 Agent 循环的奠基论文之一。Yao et al. · 'ReAct: Synergizing Reasoning and Acting in Language Models' (2022, arXiv:2210.03629) — interleaves reasoning and acting into a loop; one of the founding papers behind the agent loop.用伪代码写出来就这么短:Each round, the model sees the goal and the current observation, and decides which tool to call; the tool's result feeds back as the next observation, until the task finishes or a stop condition fires. ReAct frames this loop as interleaving reasoning and acting.2注 2Note 2Yao et al. · 「ReAct: Synergizing Reasoning and Acting in Language Models」(2022,arXiv:2210.03629)—— 把推理(reason)与行动(act)交替成一个循环,是 Agent 循环的奠基论文之一。Yao et al. · 'ReAct: Synergizing Reasoning and Acting in Language Models' (2022, arXiv:2210.03629) — interleaves reasoning and acting into a loop; one of the founding papers behind the agent loop. In pseudocode it is this short:
while not done: decision = model(goal, observations) # 决策:下一步做什么 if decision.is_final: # 模型认为完成了 return decision.answer result = run_tool(decision.tool, decision.args) # 行动 observations.append(result) # 观察,喂回下一轮
Agent 的本质:一个循环The essence of an agent: a loop
循环本身很小 —— 这就是为什么第二章用 20 行代码就能写出一个能工作的 agent。难的不是循环,是循环里的每一格:工具怎么定义、记忆放哪、推理怎么不跑偏、安全怎么兜底。3注 3Note 3Lilian Weng · 「LLM Powered Autonomous Agents」(2023-06)—— 把 Agent 拆成 planning / memory / tool use 三块的高引用综述,作者时任 OpenAI 研究员。作为概念地图使用。Lilian Weng · 'LLM Powered Autonomous Agents' (2023-06) — a widely cited overview decomposing agents into planning / memory / tool use, by a then-OpenAI researcher. Used as a concept map.这本书剩下的章节,就是逐格把它做实。The loop itself is tiny — which is why chapter 2 builds a working agent in twenty lines. The hard part isn't the loop, it's each slot inside it: how tools are defined, where memory lives, how reasoning stays on track, how safety holds.3注 3Note 3Lilian Weng · 「LLM Powered Autonomous Agents」(2023-06)—— 把 Agent 拆成 planning / memory / tool use 三块的高引用综述,作者时任 OpenAI 研究员。作为概念地图使用。Lilian Weng · 'LLM Powered Autonomous Agents' (2023-06) — a widely cited overview decomposing agents into planning / memory / tool use, by a then-OpenAI researcher. Used as a concept map. The rest of this book fills those slots in, one at a time.
— III
什么时候不该建 AgentWhen Not to Build an Agent.
agent 不是默认选项,是最后选项。能用一次 LLM 调用解决的,别上循环。An agent is not the default option — it is the last one. If a single LLM call solves it, don't put it in a loop.循环是有代价的:每一轮都可能跑偏,多轮意味着更多 token、更高延迟、更难复现的 bug。判断标准是步骤数固不固定 —— 步骤固定、路径已知,那是 workflow 或一个函数;步骤数取决于中间结果(比如「修一个还不知道在哪的 bug」),才轮得到 agent。1注 1Note 1Anthropic · 「Building Effective Agents」(Erik Schluntz、Barry Zhang,2024-12-19)—— 区分 workflow(LLM 与工具被预定义代码路径编排)与 agent(LLM 自己动态决定流程与工具);建议「find the simplest solution possible」,只在复杂度确实改善结果时才加。Anthropic · 'Building Effective Agents' (Erik Schluntz, Barry Zhang, 2024-12-19) — distinguishes workflows (LLMs and tools orchestrated through predefined code paths) from agents (LLMs dynamically directing their own process and tools); advises finding the simplest solution and adding complexity only when it demonstrably improves outcomes.The loop has a cost: every round can drift, and more rounds mean more tokens, higher latency, and bugs that are harder to reproduce. The test is whether the number of steps is fixed — fixed steps on a known path is a workflow or a function; a step count that depends on intermediate results (say, 'fix a bug you can't yet locate') is where an agent earns its place.1注 1Note 1Anthropic · 「Building Effective Agents」(Erik Schluntz、Barry Zhang,2024-12-19)—— 区分 workflow(LLM 与工具被预定义代码路径编排)与 agent(LLM 自己动态决定流程与工具);建议「find the simplest solution possible」,只在复杂度确实改善结果时才加。Anthropic · 'Building Effective Agents' (Erik Schluntz, Barry Zhang, 2024-12-19) — distinguishes workflows (LLMs and tools orchestrated through predefined code paths) from agents (LLMs dynamically directing their own process and tools); advises finding the simplest solution and adding complexity only when it demonstrably improves outcomes.反过来,什么时候它才值得建?OpenAI 的实践指南给了三类信号:要细腻判断(带例外、看情境的决策)、规则多到难维护、或重度依赖非结构化数据 —— 它把规则引擎比作一张清单,把 agent 比作一个会权衡情境的老练调查员。三样都不沾,确定性方案就够了。4注 4Note 4OpenAI · 「A Practical Guide to Building Agents」(2025-04)—— 何时该建 agent 的三类信号:需细腻判断、规则多到难维护、重度依赖非结构化数据;并把规则引擎比作清单、把 agent 比作一个会权衡情境的老练调查员。OpenAI · 'A Practical Guide to Building Agents' (2025-04) — three signals that justify an agent: nuanced judgment, rulesets too tangled to maintain, heavy reliance on unstructured data; it likens a rules engine to a checklist and an agent to a seasoned investigator weighing context.Conversely, when is one worth building? OpenAI's practical guide names three signals: nuanced judgment (decisions with exceptions and context), rulesets too tangled to maintain, or heavy reliance on unstructured data — it likens a rules engine to a checklist and an agent to a seasoned investigator weighing context. If none apply, a deterministic solution suffices.4注 4Note 4OpenAI · 「A Practical Guide to Building Agents」(2025-04)—— 何时该建 agent 的三类信号:需细腻判断、规则多到难维护、重度依赖非结构化数据;并把规则引擎比作清单、把 agent 比作一个会权衡情境的老练调查员。OpenAI · 'A Practical Guide to Building Agents' (2025-04) — three signals that justify an agent: nuanced judgment, rulesets too tangled to maintain, heavy reliance on unstructured data; it likens a rules engine to a checklist and an agent to a seasoned investigator weighing context.所以这本书每一章都会先问一句「这一格值不值得建」,再教你怎么建。建得出来和该不该建,是两回事 —— 把这条记到每一章里。So every chapter here asks 'is this slot worth building' before showing how to build it. Being able to build something and whether you should are two different things — carry that into every chapter.这条「先问值不值」背后,是 AI 七十年的苦涩教训:能随算力扩展的通用方法(搜索与学习)终究大幅胜过人工堆进去的领域知识。落到 agent —— 拿不准时,把判断交给模型的通用推理加工具,别用一摞写死的逻辑去模拟它。Sutton 的原话是「我们要的是能像我们一样去发现的 AI,而不是装着我们已发现之物的 AI」。5注 5Note 5Richard Sutton · 「The Bitter Lesson」(2019-03-13)—— AI 七十年最大的教训:能随算力扩展的通用方法(搜索与学习)终究大幅胜过人工编入的领域知识;「我们要的是能像我们一样去发现的 AI,而不是装着我们已发现之物的 AI」。落到 agent:靠模型通用推理 + 工具,别堆手写逻辑。Richard Sutton · 'The Bitter Lesson' (2019-03-13) — AI's biggest lesson in 70 years: general methods that scale with computation (search and learning) ultimately beat hand-built domain knowledge by a large margin; 'we want AI agents that can discover like we can, not which contain what we have discovered.' For agents: lean on the model's general reasoning + tools, don't pile up hand-written logic.Behind that 'ask whether it's worth it' sits the bitter lesson of seventy years of AI research: general methods that scale with computation — search and learning — ultimately beat hand-built domain knowledge by a wide margin. For agents it means: when in doubt, hand the judgment to the model's general reasoning plus tools, rather than simulating it with a stack of hard-coded logic. In Sutton's words, 'we want AI agents that can discover like we can, not which contain what we have discovered.'5注 5Note 5Richard Sutton · 「The Bitter Lesson」(2019-03-13)—— AI 七十年最大的教训:能随算力扩展的通用方法(搜索与学习)终究大幅胜过人工编入的领域知识;「我们要的是能像我们一样去发现的 AI,而不是装着我们已发现之物的 AI」。落到 agent:靠模型通用推理 + 工具,别堆手写逻辑。Richard Sutton · 'The Bitter Lesson' (2019-03-13) — AI's biggest lesson in 70 years: general methods that scale with computation (search and learning) ultimately beat hand-built domain knowledge by a large margin; 'we want AI agents that can discover like we can, not which contain what we have discovered.' For agents: lean on the model's general reasoning + tools, don't pile up hand-written logic.动手 · 在写任何代码之前,先做一次分类:Hands-on · before writing any code, do one classification pass:
01
写下 3 个你想用「agent」解决的真实任务Write down 3 real tasks you'd want an 'agent' for
从你自己的工作里挑,越具体越好(不是「帮我处理邮件」,是「每天把 X 类邮件归类并起草回复」)。Pick from your own work, as specific as possible (not 'handle my email,' but 'classify X-type email daily and draft replies').
02
给每个任务贴一个标签Tag each task
三选一:(a) 其实一次 LLM 调用就够、(b) 是固定步骤的 workflow、(c) 步骤数不固定、真需要 agent。One of three: (a) actually one LLM call, (b) a fixed-step workflow, (c) variable steps, a real agent.
03
数一数有几个真落在 (c)Count how many truly land in (c)
大概率多数是 (a) 或 (b)。剩下真正属于 (c) 的,才是你读这本书要建的东西 —— 其余的,省下循环的钱和麻烦。Most will likely be (a) or (b). The few that truly land in (c) are what you'll build with this book — for the rest, save the loop's cost and trouble.
把 prompt 写长不会变成 agent,
让它自己决定下一步才会。.
A longer prompt isn't an agent;
letting it decide the next step is..
Aklman Library
— 讨论Discussion
讨论Discussion.
评论区初始化中…Initializing comments…
01 / 01
没有匹配结果No matches.
换个关键词,或按 Esc 回到页面Try another keyword, or press Esc to return