选型 · 从零写之后,何时该用框架Choosing · After Scratch, When to Reach for a Framework.
你把 agent 从 20 行循环建到了能上生产。现在该问的不是「该早点用框架吗」,是「我懂了这些之后,何时该用、何时不必」。You built the agent from a 20-line loop to production-ready. The question now isn't 'should I have used a framework sooner' — it's 'now that you understand it, when to use one and when not.'
你把一个 agent 从 20 行循环,一路建到了能上生产。现在最该问的,不是「我是不是该早点用框架」,而是「我懂了这些之后,什么时候该用框架、什么时候不必」。这一章是全书的回报 —— 手写过一遍,框架对你不再是黑魔法,你能从「懂」的位置选,而不是从「跟风」的位置选。You built an agent from a 20-line loop all the way to production-ready. The question now isn't 'should I have used a framework sooner' — it's 'now that I understand this, when should I use one and when not.' This chapter is the book's payoff: having written it by hand, frameworks are no longer black magic, and you can choose from understanding rather than hype.
— I
手写完,框架不再是黑魔法Once You've Hand-Built It, Frameworks Stop Being Magic.
框架没有魔法 —— 它管的,就是你这十几章手写过的那个循环:loop、tools、memory、context。Frameworks aren't magic — they manage the very loop you spent this book hand-writing: loop, tools, memory, context.业界对 agent 的定义和你第一章一样:model + loop + tools + memory。1注 1Note 1Speakeasy · 「Choosing an agent framework: LangChain vs LangGraph vs CrewAI vs PydanticAI vs Mastra vs Vercel AI SDK」(2026)—— 2026 框架格局已碎片化、没有单一赢家,并逐一对比各家强项与取舍。Speakeasy · 'Choosing an agent framework: LangChain vs LangGraph vs CrewAI vs PydanticAI vs Mastra vs Vercel AI SDK' (2026) — the 2026 landscape is fragmented with no single winner, comparing each framework's strengths and trade-offs.框架的工作,就是替你管这个循环和它周边的管道 —— 状态、重试、并发、流式。你手写过整个研究助手,所以你能看穿任何框架的文档:它说的 agent、chain、graph,都对应你已经亲手实现过的东西。The industry defines an agent the same way chapter 1 did: model + loop + tools + memory.1注 1Note 1Speakeasy · 「Choosing an agent framework: LangChain vs LangGraph vs CrewAI vs PydanticAI vs Mastra vs Vercel AI SDK」(2026)—— 2026 框架格局已碎片化、没有单一赢家,并逐一对比各家强项与取舍。Speakeasy · 'Choosing an agent framework: LangChain vs LangGraph vs CrewAI vs PydanticAI vs Mastra vs Vercel AI SDK' (2026) — the 2026 landscape is fragmented with no single winner, comparing each framework's strengths and trade-offs. A framework's job is to manage that loop and its surrounding plumbing — state, retries, concurrency, streaming. Because you wrote the whole research assistant by hand, you can see through any framework's docs: its agent, chain, graph all map to things you've already implemented yourself.
从零:你管循环From scratch: you own the loop
while not done: # 循环、状态、错误都在你手里 resp = client.messages.create(model=M, tools=TOOLS, messages=messages) if resp.stop_reason != "tool_use": return final_text(resp) messages += run_tools(resp)
这就是从零写的真正回报:不是为了永远不用框架,是为了能看懂框架在做什么 —— 以及它在你身上漏的那一刻,你能接住。That's the real payoff of building from scratch: not to avoid frameworks forever, but to understand what one is doing — and to catch the moment it leaks on you.
— II
缺哪样能力,就按那样选Choose by the Capability You're Missing.
别问「哪个框架最好」,问「我手写的版本缺哪样,谁补得最好」。Don't ask 'which framework is best' — ask 'which capability is my hand-built version missing, and who supplies it best.'2026 年没有单一赢家,框架格局已经碎片化,而 vendor SDK 在起势。1注 1Note 1Speakeasy · 「Choosing an agent framework: LangChain vs LangGraph vs CrewAI vs PydanticAI vs Mastra vs Vercel AI SDK」(2026)—— 2026 框架格局已碎片化、没有单一赢家,并逐一对比各家强项与取舍。Speakeasy · 'Choosing an agent framework: LangChain vs LangGraph vs CrewAI vs PydanticAI vs Mastra vs Vercel AI SDK' (2026) — the 2026 landscape is fragmented with no single winner, comparing each framework's strengths and trade-offs.按你缺的那一项对号入座:要持久状态、checkpoint、断点恢复、人审 → LangGraph(生产 stateful 的首选,企业部署最多);3注 3Note 3LangGraph 文档(langchain-ai)—— 以持久状态、checkpoint、条件分支、人审为卖点的生产 stateful agent 框架,企业部署案例最多。作为「按缺口选框架」里『要持久状态』那一栏的代表。LangGraph docs (langchain-ai) — a production stateful-agent framework selling durable state, checkpointing, conditional branching, and human-in-the-loop, with the largest enterprise deployment list. Representative of the 'need durable state' column.要类型安全、FastAPI 式开发体验 → Pydantic AI;要在一家模型生态里最快上线、原生 sandbox + MCP → OpenAI Agents SDK;4注 4Note 4OpenAI Agents SDK —— vendor-native SDK,把 handoff、sandbox、MCP 做成一等能力;2026 起 vendor SDK 成为主流之一,代价是绑一家生态的 lock-in。OpenAI Agents SDK — a vendor-native SDK making handoffs, sandboxing, and MCP first-class; vendor SDKs became a major path in 2026, at the cost of single-ecosystem lock-in.要streaming UI、给用户看进度 → Vercel AI SDK;要最快出原型 → CrewAI。In 2026 there's no single winner — the landscape has fragmented, and vendor SDKs are rising.1注 1Note 1Speakeasy · 「Choosing an agent framework: LangChain vs LangGraph vs CrewAI vs PydanticAI vs Mastra vs Vercel AI SDK」(2026)—— 2026 框架格局已碎片化、没有单一赢家,并逐一对比各家强项与取舍。Speakeasy · 'Choosing an agent framework: LangChain vs LangGraph vs CrewAI vs PydanticAI vs Mastra vs Vercel AI SDK' (2026) — the 2026 landscape is fragmented with no single winner, comparing each framework's strengths and trade-offs. Match the capability you lack: need durable state, checkpoints, resumption, human review → LangGraph (the production stateful pick, most enterprise deployments);3注 3Note 3LangGraph 文档(langchain-ai)—— 以持久状态、checkpoint、条件分支、人审为卖点的生产 stateful agent 框架,企业部署案例最多。作为「按缺口选框架」里『要持久状态』那一栏的代表。LangGraph docs (langchain-ai) — a production stateful-agent framework selling durable state, checkpointing, conditional branching, and human-in-the-loop, with the largest enterprise deployment list. Representative of the 'need durable state' column. need type safety and a FastAPI-style DX → Pydantic AI; need fastest shipping within one model ecosystem, native sandbox + MCP → OpenAI Agents SDK;4注 4Note 4OpenAI Agents SDK —— vendor-native SDK,把 handoff、sandbox、MCP 做成一等能力;2026 起 vendor SDK 成为主流之一,代价是绑一家生态的 lock-in。OpenAI Agents SDK — a vendor-native SDK making handoffs, sandboxing, and MCP first-class; vendor SDKs became a major path in 2026, at the cost of single-ecosystem lock-in. need streaming UIs that show progress → Vercel AI SDK; need the fastest prototype → CrewAI.这是一张「按缺口选」的表,不是「功能越多越好」。你只缺 streaming,就别为它引入一个管所有东西的重框架 —— 你多引入的每一层,都是出事时多一层要调的别人的代码。This is a 'pick by the gap' table, not 'more features is better.' If all you lack is streaming, don't pull in a heavy do-everything framework for it — every extra layer you add is one more layer of someone else's code to debug when things break.
框架省的是管道代码,花的是控制权 —— 它漏的时候,你 debug 的是它的抽象,不是你的循环。A framework saves plumbing code and spends control — when it leaks, you debug its abstraction, not your loop.两个代价要算清。一是 lock-in,尤其 vendor SDK,把你绑在一家模型生态上。4注 4Note 4OpenAI Agents SDK —— vendor-native SDK,把 handoff、sandbox、MCP 做成一等能力;2026 起 vendor SDK 成为主流之一,代价是绑一家生态的 lock-in。OpenAI Agents SDK — a vendor-native SDK making handoffs, sandboxing, and MCP first-class; vendor SDKs became a major path in 2026, at the cost of single-ecosystem lock-in.二是抽象税:框架在 80% 的常规情况省事,却在剩下最难的 20% 挡在你和真实行为之间。Anthropic 自己的建议是 —— 可以用框架起步,但要理解底层代码,否则对抽象的错误假设会成为常见 bug 源。2注 2Note 2Anthropic · 「Building Effective Agents」(2024-12-19)—— 可以用框架起步,但建议理解底层代码:错误的抽象假设会成为常见 bug 源,框架在最难的 20% 反而挡在你和真实行为之间。Anthropic · 'Building Effective Agents' (2024-12-19) — you can start with a framework, but understand the underlying code: wrong assumptions about abstractions become a common source of bugs, and frameworks can stand between you and the real behavior in the hardest 20%.Two costs to count. One is lock-in, especially with vendor SDKs that tie you to one model ecosystem.4注 4Note 4OpenAI Agents SDK —— vendor-native SDK,把 handoff、sandbox、MCP 做成一等能力;2026 起 vendor SDK 成为主流之一,代价是绑一家生态的 lock-in。OpenAI Agents SDK — a vendor-native SDK making handoffs, sandboxing, and MCP first-class; vendor SDKs became a major path in 2026, at the cost of single-ecosystem lock-in. The other is the abstraction tax: a framework eases the routine 80% but stands between you and real behavior in the hardest 20%. Anthropic's own advice — you can start with a framework, but understand the underlying code, or wrong assumptions about its abstractions become a common source of bugs.2注 2Note 2Anthropic · 「Building Effective Agents」(2024-12-19)—— 可以用框架起步,但建议理解底层代码:错误的抽象假设会成为常见 bug 源,框架在最难的 20% 反而挡在你和真实行为之间。Anthropic · 'Building Effective Agents' (2024-12-19) — you can start with a framework, but understand the underlying code: wrong assumptions about abstractions become a common source of bugs, and frameworks can stand between you and the real behavior in the hardest 20%.选框架的前提,是你能在它漏的时候掉回手写 —— 而你现在能了,这正是这本书从零写的意义。所以判断很简单:有明确、单一的缺口(状态 / 类型 / 流式),且你认那份 lock-in,就上对应框架;没有明确缺口,或那缺口你自己几十行就能补,就留在从零 —— 依赖更少,控制更全。The prerequisite for choosing a framework is being able to drop back to hand-writing when it leaks — and now you can; that's the point of building from scratch. So the call is simple: a clear, single gap (state / types / streaming) you're willing to pay lock-in for → adopt the matching framework; no clear gap, or one you could fill in a few dozen lines → stay from scratch, with fewer dependencies and full control.动手 · 从你手写的 agent 出发,做一次选型:Hands-on · make a framework decision from your hand-built agent:
01
列出最难维护的 3 件事List the 3 hardest things to maintain
在你现在这个手写 agent 上,挑出维护起来最痛的 3 样:状态恢复?streaming?类型?并发?On your current hand-built agent, pick the 3 most painful to maintain: state recovery? streaming? types? concurrency?
02
给每件事对上「谁补得最好」Map each to 'who supplies it best'
用第二节那张表,把每个痛点对到补它最好的那一栏 —— 注意有没有一个框架同时补了两件以上。Use section II's table to map each pain to the column that fixes it best — and note whether one framework covers two or more.
03
拍板,并写下认 lock-in 的理由Decide, and write down why you'll accept lock-in
留在从零(自己补那 3 件),还是为某个明确缺口引入一个框架。写一句话结论,里面要有「这份 lock-in 我认了,因为___」。Stay from scratch (and fill those 3 yourself), or adopt one framework for a clear gap. Write a one-line verdict containing 'I'll accept this lock-in because ___.'
先手写一遍,
你才有资格选框架。.
Write it by hand once,
then you're fit to choose a framework..
Aklman Library
— 讨论Discussion
讨论Discussion.
评论区初始化中…Initializing comments…
01 / 01
没有匹配结果No matches.
换个关键词,或按 Esc 回到页面Try another keyword, or press Esc to return