组装 · 把每一格拼成一个能跑的 agentAssemble · Wire Every Slot into One Running Agent.
前面每章加一格,这一章把它们拼成一个你能真跑的 Python 工程 —— 离线一条命令,接上 key 就换真实 Claude。Each chapter added a slot; this one wires them into one Python project you can actually run — one command offline, real Claude the moment you add a key.
10 分钟 · 初稿 2026.0610 Min · Drafted 2026.06
前面每一章都在给那个研究助手加一格 —— 搜索、抓取、记忆、推理、沙箱、护栏、trace。这一章把它们拼成一个你能真跑的 Python 工程:离线一条命令就跑,接上 key 就换成真实的 Claude。读完你手里不再是十几段散落的片段,是一个核心部件齐全、能跑能改的基础版 agent。Every chapter so far added one slot to that research assistant — search, fetch, memory, reasoning, sandbox, guardrails, traces. This chapter wires them into one Python project you can actually run: one command offline, or real Claude the moment you add a key. You walk away holding not a dozen scattered snippets, but a basic agent whose core parts are all there — runnable and editable.工程在 content/books/build-agent/agent/,一个叫 research_agent 的小包。下面拆三件事:心脏为什么还是第二章那个循环、怎么跑、以及它为什么是「完备基础版」而不是生产级。The project lives at content/books/build-agent/agent/, a small package called research_agent. Three things below: why the heart is still chapter 2's loop, how to run it, and why it's the 'complete basic version' rather than production-grade.
— I
心脏没变,还是那个循环The Heart Hasn't Changed — It's Still the Loop.
加了这么多格,中心那个 while 一行没变。这正是全书的论点:agent 就是 LLM 在循环里用工具、读反馈、定下一步;其余都是挂在这个循环上的格子。For all the slots added, the central while loop hasn't changed a line. That is the book's thesis: an agent is an LLM using tools in a loop on feedback to decide the next step; everything else is a slot hung off that loop.Anthropic 把基本积木叫「augmented LLM」—— 检索、工具、记忆三样增强;agent 就是它在循环里跑。1注 1Note 1Anthropic · 「Building Effective Agents」(2024-12-19)—— 基本积木是「augmented LLM」(检索 / 工具 / 记忆);agent 就是「LLM 在循环里用工具、读环境反馈、决定下一步」。组装这一章正是把这句话落成代码。Anthropic · 'Building Effective Agents' (2024-12-19) — the basic block is the 'augmented LLM' (retrieval / tools / memory); an agent is just 'LLMs using tools based on environmental feedback in a loop.' This chapter turns that sentence into code.OpenAI 的说法一样直白:单 agent 是一个 run loop,跑到触发退出条件为止,「这个 while 循环是 agent 运转的核心」。2注 2Note 2OpenAI · 「A Practical Guide to Building Agents」(2025-04)—— 单 agent 就是一个 run loop,跑到触发退出条件(最终输出 / 无工具调用 / 出错 / 步数上限);原话「这个 while 循环是 agent 运转的核心」。OpenAI · 'A Practical Guide to Building Agents' (2025-04) — a single agent is a run loop until an exit condition (final output / no tool call / error / max turns); in its words, 'this concept of a while loop is central to the functioning of an agent.'我们的 loop.py 就是这句话的代码版,把后面各章的格子接上去:Anthropic calls the basic block the 'augmented LLM' — retrieval, tools, memory; an agent is that, run in a loop.1注 1Note 1Anthropic · 「Building Effective Agents」(2024-12-19)—— 基本积木是「augmented LLM」(检索 / 工具 / 记忆);agent 就是「LLM 在循环里用工具、读环境反馈、决定下一步」。组装这一章正是把这句话落成代码。Anthropic · 'Building Effective Agents' (2024-12-19) — the basic block is the 'augmented LLM' (retrieval / tools / memory); an agent is just 'LLMs using tools based on environmental feedback in a loop.' This chapter turns that sentence into code. OpenAI puts it the same way: a single agent is a run loop until an exit condition, and 'this while loop is central to the functioning of an agent.'2注 2Note 2OpenAI · 「A Practical Guide to Building Agents」(2025-04)—— 单 agent 就是一个 run loop,跑到触发退出条件(最终输出 / 无工具调用 / 出错 / 步数上限);原话「这个 while 循环是 agent 运转的核心」。OpenAI · 'A Practical Guide to Building Agents' (2025-04) — a single agent is a run loop until an exit condition (final output / no tool call / error / max turns); in its words, 'this concept of a while loop is central to the functioning of an agent.' Our loop.py is that sentence in code, with each later chapter's slot wired in:
loop.py 的核心:还是第二章那个循环,各格挂在上面The core of loop.py: still chapter 2's loop, with the slots hung off it
模型层是那把让它「能跑」的钥匙:model.py 把 provider 藏在一个接口后面,有 ANTHROPIC_API_KEY 就接真实的 Claude,没有就退回一个离线 stub 模型。4注 4Note 4anthropic-sdk-python —— 官方 Python SDK,真实后端用 messages.create;本章的 model.py 在「有 ANTHROPIC_API_KEY 时接 Claude、没有时退回离线 stub」之间切换。引用发布版本而非 main。anthropic-sdk-python — the official Python SDK; the real backend uses messages.create. This chapter's model.py switches between 'Claude when ANTHROPIC_API_KEY is set, an offline stub otherwise.' Cite a released version, not main.stub 不联网、不要 key,按一条 search → fetch → run_python → 报告 的固定轨迹走 —— 它存在的意义,是让你在零依赖下把整台机器跑起来、看清每一格,也让这套 harness 可回归测试。The model layer is the key that makes it runnable: model.py hides the provider behind one interface — real Claude when ANTHROPIC_API_KEY is set, an offline stub model when it isn't.4注 4Note 4anthropic-sdk-python —— 官方 Python SDK,真实后端用 messages.create;本章的 model.py 在「有 ANTHROPIC_API_KEY 时接 Claude、没有时退回离线 stub」之间切换。引用发布版本而非 main。anthropic-sdk-python — the official Python SDK; the real backend uses messages.create. This chapter's model.py switches between 'Claude when ANTHROPIC_API_KEY is set, an offline stub otherwise.' Cite a released version, not main. The stub needs no network and no key; it walks a fixed search → fetch → run_python → report trajectory — there so you can run the whole machine with zero dependencies, see every slot, and regression-test the harness.
— II
跑起来:离线一条命令,真实多两步Run It: One Command Offline, Two More for Real.
先证明它真能跑 —— 不要 key、不联网、不装任何包,离线 stub 模式一条命令就把整条链路走通。First, proof it runs — no key, no network, no installs: offline stub mode walks the whole chain in one command.
cd content/books/build-agent/agentpython -m research_agent --offline "比较 RAG 与长上下文,各自适合什么场景"
两种跑法:离线 stub(零依赖)与真实 ClaudeTwo ways to run: offline stub (zero deps) and real Claude
离线那条真跑出来是这样(进度走 stderr,每一步都看得见)—— 注意 run_python 是在沙箱里真执行的,不是装样子:The offline command really produces this (progress on stderr, every step visible) — note run_python truly executes in the sandbox, not for show:
离线 stub 模式的真实输出(节选)Real output from offline stub mode (excerpt)
工程的好处是每一格都是一个文件,名字就对着书里的章 —— 你能一眼看出哪段代码解决的是哪一章的问题:The point of the project is that each slot is a file, named for the chapter it came from — you can see at a glance which code solves which chapter's problem:
loop.py
循环(ch2)—— 心脏,其余都挂在它上面。The loop (ch2) — the heart; everything hangs off it.
model.py
模型层(ch2)—— 真实 Claude / 离线 stub,一个接口。Model layer (ch2) — real Claude / offline stub, one interface.
一张图看清它们怎么咬合 —— 中间是循环,各格挂在上面,trace 和预算闸贯穿每一步:One diagram for how they mesh — the loop in the middle, slots hung on it, traces and the budget breaker running through every step:
组装好的 agent:循环居中,各格在侧The assembled agent: loop at the center, slots on the sides
— III
它是完备基础版,不是生产级It's the Complete Basic Version, Not Production.
核心部件齐全、能跑能改 —— 但「能跑」不等于「能上生产」。把它的边界说清楚,每一条都在对应章里展开过,也都是你下一步该补的地方。The core parts are all here and it runs and edits — but 'runs' isn't 'production-ready.' Here are its boundaries plainly; each was opened up in its chapter, and each is where you'd harden next.
沙箱是 subprocess,不是真隔离The sandbox is a subprocess, not real isolation
真实提升在 contextual chunk + 重排的检索管线里,不在这个最小骨架(ch4)。Real gains live in a contextual-chunk + rerank pipeline, not this minimal skeleton (ch4).
循环是同步的The loop is synchronous
进度用回调流出;真要做流式 UI + 可中断,要上 async(streaming 章)。Progress streams via a callback; a real streaming UI + interruptibility needs async (the streaming chapter).
原则化自查默认关Principled self-check is off by default
那道护栏的裁判得是个真实模型,离线 demo 跑不了(ch9 · Constitutional AI)。That guardrail's judge must be a real model, so the offline demo can't run it (ch9 · Constitutional AI).
怎么知道哪一格该补、补了值不值?看 trace 摘要里那个 cost-of-pass(成本 ÷ 成功率)。Efficient Agents 的发现是很多模块边际递减,复杂度该按任务难度配。3注 3Note 3Wang et al. · 「Efficient Agents」(2025,arXiv:2508.02694)—— 用 cost-of-pass = 成本 ÷ 成功率 度量每一格值不值,复杂度按任务难度配。trace 摘要里那个数,就是给「加东西前先量」一个抓手。Wang et al. · 'Efficient Agents' (2025, arXiv:2508.02694) — uses cost-of-pass = cost ÷ success rate to weigh whether each module earns its keep, matching complexity to task difficulty. The number in the trace summary is the handle for 'measure before you add.'所以加一格之前先量一下:成功率涨了多少、成本涨了多少 —— 用数字判断,别凭感觉。这正是全书第一章那句话在工程上的兑现:先做最简的,只在被证明值得时才加复杂度。How do you know which slot to harden, and whether it's worth it? Look at cost-of-pass (cost ÷ success rate) in the trace summary. Efficient Agents found many modules give diminishing returns, and complexity should match task difficulty.3注 3Note 3Wang et al. · 「Efficient Agents」(2025,arXiv:2508.02694)—— 用 cost-of-pass = 成本 ÷ 成功率 度量每一格值不值,复杂度按任务难度配。trace 摘要里那个数,就是给「加东西前先量」一个抓手。Wang et al. · 'Efficient Agents' (2025, arXiv:2508.02694) — uses cost-of-pass = cost ÷ success rate to weigh whether each module earns its keep, matching complexity to task difficulty. The number in the trace summary is the handle for 'measure before you add.' So before adding a slot, measure: how much did success rise, how much did cost rise — judge by the number, not by feel. That's chapter 1's line cashed out in engineering: do the simplest thing, add complexity only when it's proven worth it.所以你现在手里有一个能跑的 agent。下一章把这套同样的机器对准最难、也最成熟的用例 —— 编码;再下一章回答:手写过一遍之后,何时该用框架、何时不必。So you now hold a running agent. The next chapter aims this same machine at the hardest and most mature use case — coding; the one after answers: having hand-written it once, when to reach for a framework and when not.动手 · 把这个 agent 接到你自己的一个真问题上:Hands-on · point this agent at one real question of your own:
01
先离线跑一遍,看清每一步Run it offline once, watch every step
在 agent/ 里 python -m research_agent --offline "你的问题",对着 stderr 看它搜、抓、算、报。In agent/, run python -m research_agent --offline "your question" and watch it search, fetch, compute, report on stderr.
02
设上 key,换真实模型Add a key, switch to the real model
pip install anthropic、设 ANTHROPIC_API_KEY,跑你自己真要查的问题,看真实 Claude 怎么自己决定调哪个工具。pip install anthropic, set ANTHROPIC_API_KEY, run a question you actually need answered, and watch real Claude decide which tools to call.
03
改一格,用 cost-of-pass 判值不值Change one slot, judge by cost-of-pass
在 tools.py 加一个你自己的工具,或在 config.py 收紧白名单 / 预算。改前改后各跑一次,对比 trace 摘要里的 cost-of-pass —— 用数字决定这一格留不留。Add your own tool in tools.py, or tighten the allowlist / budget in config.py. Run once before and after, compare cost-of-pass in the trace summary — let the number decide whether the slot stays.
把每一格拼起来,
心脏还是第二章那个循环。.
Wire every slot together,
and the heart is still chapter 2's loop..
Aklman Library
— 讨论Discussion
讨论Discussion.
评论区初始化中…Initializing comments…
01 / 01
没有匹配结果No matches.
换个关键词,或按 Esc 回到页面Try another keyword, or press Esc to return