深入理解 Agent Loop:AI Agent 的核心运行机制与工程实现
本文从底层原理到工程实现,全面拆解 Agent Loop(智能体循环)这一 AI Agent 的核心能力。内容涵盖传统 LLM 的局限、Agent Loop 的完整执行流程、Tool Calling、Memory 机制、与 Workflow/ReAct 的关系、主流框架对比,并提供基于 TypeScript 的 Mini Agent Loop 完整实现。读完本文,你将真正理解 OpenCode、Claude Code、Cursor 这些 AI Coding Agent 背后的运转逻辑。
第一章:为什么 AI 都开始讲 Agent?
1.1 从 2025 年的爆发说起
如果你在 2024 年还停留在「用 ChatGPT 问答」的阶段,那么到 2025~2026 年,整个 AI 开发者圈子的叙事已经彻底变了------所有人都在讲 Agent。
一个标志性事件是:Anthropic 推出的 Claude Code 让开发者第一次体验到「AI 自己读项目、改代码、跑测试、修 Bug」的全流程自动化;紧接着 OpenAI Codex CLI 、开源的 OpenCode 、Cursor Agent 模式 纷纷跟进。这些工具的共同特点是:它们不再是「你问我答」,而是「你给目标,它自己干」。
为什么会发生这样的转变?核心原因是三点:
第一,模型能力跨越了临界点。 GPT-4o、Claude 3.5/4、Gemini 2 这一代模型不仅推理能力强,更重要的是 tool use(工具调用)的稳定性大幅提升。以前让模型可靠地调用一个函数并解析返回结果,成功率可能只有 60%;现在可以稳定在 95% 以上。这是 Agent 能真正「干活」的技术基础。
第二,上下文窗口足够大了。 从早期的 4K、8K,到现在的 128K、200K 甚至 1M token。Agent 需要在循环中不断把「工具执行结果」塞回上下文,窗口太小根本循环不了几轮。大窗口让 Agent 能「记住」自己在循环中做过什么。
第三,工程框架成熟了。 LangGraph、OpenAI Agents SDK、各家自研的 Agent Runtime,让开发者不用从零造轮子就能跑起一个 Agent。
1.2 为什么以前 ChatGPT 没有 Agent Loop?
回忆一下 2023 年初的 ChatGPT(GPT-3.5 时代):
- 它不能主动读取你电脑上的文件
- 它不能执行命令行
- 它不能联网搜索(后来有了 Browse,但也是受限的)
- 它不会自己「想一步、做一步、再想一步」
那时的 ChatGPT 本质上是一个 单轮对话补全器(Completer) :你输入 Prompt,它输出文本,结束。如果你说「帮我修这个 Bug」,它只能基于你贴在对话里的代码「猜测性」地给建议,它无法主动去验证自己的修改对不对。
#mermaid-svg-IX6jKxyAmYGE9o0e{font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;fill:#333;}@keyframes edge-animation-frame{from{stroke-dashoffset:0;}}@keyframes dash{to{stroke-dashoffset:0;}}#mermaid-svg-IX6jKxyAmYGE9o0e .edge-animation-slow{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 50s linear infinite;stroke-linecap:round;}#mermaid-svg-IX6jKxyAmYGE9o0e .edge-animation-fast{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 20s linear infinite;stroke-linecap:round;}#mermaid-svg-IX6jKxyAmYGE9o0e .error-icon{fill:#552222;}#mermaid-svg-IX6jKxyAmYGE9o0e .error-text{fill:#552222;stroke:#552222;}#mermaid-svg-IX6jKxyAmYGE9o0e .edge-thickness-normal{stroke-width:1px;}#mermaid-svg-IX6jKxyAmYGE9o0e .edge-thickness-thick{stroke-width:3.5px;}#mermaid-svg-IX6jKxyAmYGE9o0e .edge-pattern-solid{stroke-dasharray:0;}#mermaid-svg-IX6jKxyAmYGE9o0e .edge-thickness-invisible{stroke-width:0;fill:none;}#mermaid-svg-IX6jKxyAmYGE9o0e .edge-pattern-dashed{stroke-dasharray:3;}#mermaid-svg-IX6jKxyAmYGE9o0e .edge-pattern-dotted{stroke-dasharray:2;}#mermaid-svg-IX6jKxyAmYGE9o0e .marker{fill:#333333;stroke:#333333;}#mermaid-svg-IX6jKxyAmYGE9o0e .marker.cross{stroke:#333333;}#mermaid-svg-IX6jKxyAmYGE9o0e svg{font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;}#mermaid-svg-IX6jKxyAmYGE9o0e p{margin:0;}#mermaid-svg-IX6jKxyAmYGE9o0e .edge{stroke-width:3;}#mermaid-svg-IX6jKxyAmYGE9o0e .section--1 rect,#mermaid-svg-IX6jKxyAmYGE9o0e .section--1 path,#mermaid-svg-IX6jKxyAmYGE9o0e .section--1 circle,#mermaid-svg-IX6jKxyAmYGE9o0e .section--1 polygon,#mermaid-svg-IX6jKxyAmYGE9o0e .section--1 path{fill:hsl(240, 100%, 76.2745098039%);}#mermaid-svg-IX6jKxyAmYGE9o0e .section--1 text{fill:#ffffff;}#mermaid-svg-IX6jKxyAmYGE9o0e .node-icon--1{font-size:40px;color:#ffffff;}#mermaid-svg-IX6jKxyAmYGE9o0e .section-edge--1{stroke:hsl(240, 100%, 76.2745098039%);}#mermaid-svg-IX6jKxyAmYGE9o0e .edge-depth--1{stroke-width:17;}#mermaid-svg-IX6jKxyAmYGE9o0e .section--1 line{stroke:hsl(60, 100%, 86.2745098039%);stroke-width:3;}#mermaid-svg-IX6jKxyAmYGE9o0e .disabled,#mermaid-svg-IX6jKxyAmYGE9o0e .disabled circle,#mermaid-svg-IX6jKxyAmYGE9o0e .disabled text{fill:lightgray;}#mermaid-svg-IX6jKxyAmYGE9o0e .disabled text{fill:#efefef;}#mermaid-svg-IX6jKxyAmYGE9o0e .section-0 rect,#mermaid-svg-IX6jKxyAmYGE9o0e .section-0 path,#mermaid-svg-IX6jKxyAmYGE9o0e .section-0 circle,#mermaid-svg-IX6jKxyAmYGE9o0e .section-0 polygon,#mermaid-svg-IX6jKxyAmYGE9o0e .section-0 path{fill:hsl(60, 100%, 73.5294117647%);}#mermaid-svg-IX6jKxyAmYGE9o0e .section-0 text{fill:black;}#mermaid-svg-IX6jKxyAmYGE9o0e .node-icon-0{font-size:40px;color:black;}#mermaid-svg-IX6jKxyAmYGE9o0e .section-edge-0{stroke:hsl(60, 100%, 73.5294117647%);}#mermaid-svg-IX6jKxyAmYGE9o0e .edge-depth-0{stroke-width:14;}#mermaid-svg-IX6jKxyAmYGE9o0e .section-0 line{stroke:hsl(240, 100%, 83.5294117647%);stroke-width:3;}#mermaid-svg-IX6jKxyAmYGE9o0e .disabled,#mermaid-svg-IX6jKxyAmYGE9o0e .disabled circle,#mermaid-svg-IX6jKxyAmYGE9o0e .disabled text{fill:lightgray;}#mermaid-svg-IX6jKxyAmYGE9o0e .disabled text{fill:#efefef;}#mermaid-svg-IX6jKxyAmYGE9o0e .section-1 rect,#mermaid-svg-IX6jKxyAmYGE9o0e .section-1 path,#mermaid-svg-IX6jKxyAmYGE9o0e .section-1 circle,#mermaid-svg-IX6jKxyAmYGE9o0e .section-1 polygon,#mermaid-svg-IX6jKxyAmYGE9o0e .section-1 path{fill:hsl(80, 100%, 76.2745098039%);}#mermaid-svg-IX6jKxyAmYGE9o0e .section-1 text{fill:black;}#mermaid-svg-IX6jKxyAmYGE9o0e .node-icon-1{font-size:40px;color:black;}#mermaid-svg-IX6jKxyAmYGE9o0e .section-edge-1{stroke:hsl(80, 100%, 76.2745098039%);}#mermaid-svg-IX6jKxyAmYGE9o0e .edge-depth-1{stroke-width:11;}#mermaid-svg-IX6jKxyAmYGE9o0e .section-1 line{stroke:hsl(260, 100%, 86.2745098039%);stroke-width:3;}#mermaid-svg-IX6jKxyAmYGE9o0e .disabled,#mermaid-svg-IX6jKxyAmYGE9o0e .disabled circle,#mermaid-svg-IX6jKxyAmYGE9o0e .disabled text{fill:lightgray;}#mermaid-svg-IX6jKxyAmYGE9o0e .disabled text{fill:#efefef;}#mermaid-svg-IX6jKxyAmYGE9o0e .section-2 rect,#mermaid-svg-IX6jKxyAmYGE9o0e .section-2 path,#mermaid-svg-IX6jKxyAmYGE9o0e .section-2 circle,#mermaid-svg-IX6jKxyAmYGE9o0e .section-2 polygon,#mermaid-svg-IX6jKxyAmYGE9o0e .section-2 path{fill:hsl(270, 100%, 76.2745098039%);}#mermaid-svg-IX6jKxyAmYGE9o0e .section-2 text{fill:#ffffff;}#mermaid-svg-IX6jKxyAmYGE9o0e .node-icon-2{font-size:40px;color:#ffffff;}#mermaid-svg-IX6jKxyAmYGE9o0e .section-edge-2{stroke:hsl(270, 100%, 76.2745098039%);}#mermaid-svg-IX6jKxyAmYGE9o0e .edge-depth-2{stroke-width:8;}#mermaid-svg-IX6jKxyAmYGE9o0e .section-2 line{stroke:hsl(90, 100%, 86.2745098039%);stroke-width:3;}#mermaid-svg-IX6jKxyAmYGE9o0e .disabled,#mermaid-svg-IX6jKxyAmYGE9o0e .disabled circle,#mermaid-svg-IX6jKxyAmYGE9o0e .disabled text{fill:lightgray;}#mermaid-svg-IX6jKxyAmYGE9o0e .disabled text{fill:#efefef;}#mermaid-svg-IX6jKxyAmYGE9o0e .section-3 rect,#mermaid-svg-IX6jKxyAmYGE9o0e .section-3 path,#mermaid-svg-IX6jKxyAmYGE9o0e .section-3 circle,#mermaid-svg-IX6jKxyAmYGE9o0e .section-3 polygon,#mermaid-svg-IX6jKxyAmYGE9o0e .section-3 path{fill:hsl(300, 100%, 76.2745098039%);}#mermaid-svg-IX6jKxyAmYGE9o0e .section-3 text{fill:black;}#mermaid-svg-IX6jKxyAmYGE9o0e .node-icon-3{font-size:40px;color:black;}#mermaid-svg-IX6jKxyAmYGE9o0e .section-edge-3{stroke:hsl(300, 100%, 76.2745098039%);}#mermaid-svg-IX6jKxyAmYGE9o0e .edge-depth-3{stroke-width:5;}#mermaid-svg-IX6jKxyAmYGE9o0e .section-3 line{stroke:hsl(120, 100%, 86.2745098039%);stroke-width:3;}#mermaid-svg-IX6jKxyAmYGE9o0e .disabled,#mermaid-svg-IX6jKxyAmYGE9o0e .disabled circle,#mermaid-svg-IX6jKxyAmYGE9o0e .disabled text{fill:lightgray;}#mermaid-svg-IX6jKxyAmYGE9o0e .disabled text{fill:#efefef;}#mermaid-svg-IX6jKxyAmYGE9o0e .section-4 rect,#mermaid-svg-IX6jKxyAmYGE9o0e .section-4 path,#mermaid-svg-IX6jKxyAmYGE9o0e .section-4 circle,#mermaid-svg-IX6jKxyAmYGE9o0e .section-4 polygon,#mermaid-svg-IX6jKxyAmYGE9o0e .section-4 path{fill:hsl(330, 100%, 76.2745098039%);}#mermaid-svg-IX6jKxyAmYGE9o0e .section-4 text{fill:black;}#mermaid-svg-IX6jKxyAmYGE9o0e .node-icon-4{font-size:40px;color:black;}#mermaid-svg-IX6jKxyAmYGE9o0e .section-edge-4{stroke:hsl(330, 100%, 76.2745098039%);}#mermaid-svg-IX6jKxyAmYGE9o0e .edge-depth-4{stroke-width:2;}#mermaid-svg-IX6jKxyAmYGE9o0e .section-4 line{stroke:hsl(150, 100%, 86.2745098039%);stroke-width:3;}#mermaid-svg-IX6jKxyAmYGE9o0e .disabled,#mermaid-svg-IX6jKxyAmYGE9o0e .disabled circle,#mermaid-svg-IX6jKxyAmYGE9o0e .disabled text{fill:lightgray;}#mermaid-svg-IX6jKxyAmYGE9o0e .disabled text{fill:#efefef;}#mermaid-svg-IX6jKxyAmYGE9o0e .section-5 rect,#mermaid-svg-IX6jKxyAmYGE9o0e .section-5 path,#mermaid-svg-IX6jKxyAmYGE9o0e .section-5 circle,#mermaid-svg-IX6jKxyAmYGE9o0e .section-5 polygon,#mermaid-svg-IX6jKxyAmYGE9o0e .section-5 path{fill:hsl(0, 100%, 76.2745098039%);}#mermaid-svg-IX6jKxyAmYGE9o0e .section-5 text{fill:black;}#mermaid-svg-IX6jKxyAmYGE9o0e .node-icon-5{font-size:40px;color:black;}#mermaid-svg-IX6jKxyAmYGE9o0e .section-edge-5{stroke:hsl(0, 100%, 76.2745098039%);}#mermaid-svg-IX6jKxyAmYGE9o0e .edge-depth-5{stroke-width:-1;}#mermaid-svg-IX6jKxyAmYGE9o0e .section-5 line{stroke:hsl(180, 100%, 86.2745098039%);stroke-width:3;}#mermaid-svg-IX6jKxyAmYGE9o0e .disabled,#mermaid-svg-IX6jKxyAmYGE9o0e .disabled circle,#mermaid-svg-IX6jKxyAmYGE9o0e .disabled text{fill:lightgray;}#mermaid-svg-IX6jKxyAmYGE9o0e .disabled text{fill:#efefef;}#mermaid-svg-IX6jKxyAmYGE9o0e .section-6 rect,#mermaid-svg-IX6jKxyAmYGE9o0e .section-6 path,#mermaid-svg-IX6jKxyAmYGE9o0e .section-6 circle,#mermaid-svg-IX6jKxyAmYGE9o0e .section-6 polygon,#mermaid-svg-IX6jKxyAmYGE9o0e .section-6 path{fill:hsl(30, 100%, 76.2745098039%);}#mermaid-svg-IX6jKxyAmYGE9o0e .section-6 text{fill:black;}#mermaid-svg-IX6jKxyAmYGE9o0e .node-icon-6{font-size:40px;color:black;}#mermaid-svg-IX6jKxyAmYGE9o0e .section-edge-6{stroke:hsl(30, 100%, 76.2745098039%);}#mermaid-svg-IX6jKxyAmYGE9o0e .edge-depth-6{stroke-width:-4;}#mermaid-svg-IX6jKxyAmYGE9o0e .section-6 line{stroke:hsl(210, 100%, 86.2745098039%);stroke-width:3;}#mermaid-svg-IX6jKxyAmYGE9o0e .disabled,#mermaid-svg-IX6jKxyAmYGE9o0e .disabled circle,#mermaid-svg-IX6jKxyAmYGE9o0e .disabled text{fill:lightgray;}#mermaid-svg-IX6jKxyAmYGE9o0e .disabled text{fill:#efefef;}#mermaid-svg-IX6jKxyAmYGE9o0e .section-7 rect,#mermaid-svg-IX6jKxyAmYGE9o0e .section-7 path,#mermaid-svg-IX6jKxyAmYGE9o0e .section-7 circle,#mermaid-svg-IX6jKxyAmYGE9o0e .section-7 polygon,#mermaid-svg-IX6jKxyAmYGE9o0e .section-7 path{fill:hsl(90, 100%, 76.2745098039%);}#mermaid-svg-IX6jKxyAmYGE9o0e .section-7 text{fill:black;}#mermaid-svg-IX6jKxyAmYGE9o0e .node-icon-7{font-size:40px;color:black;}#mermaid-svg-IX6jKxyAmYGE9o0e .section-edge-7{stroke:hsl(90, 100%, 76.2745098039%);}#mermaid-svg-IX6jKxyAmYGE9o0e .edge-depth-7{stroke-width:-7;}#mermaid-svg-IX6jKxyAmYGE9o0e .section-7 line{stroke:hsl(270, 100%, 86.2745098039%);stroke-width:3;}#mermaid-svg-IX6jKxyAmYGE9o0e .disabled,#mermaid-svg-IX6jKxyAmYGE9o0e .disabled circle,#mermaid-svg-IX6jKxyAmYGE9o0e .disabled text{fill:lightgray;}#mermaid-svg-IX6jKxyAmYGE9o0e .disabled text{fill:#efefef;}#mermaid-svg-IX6jKxyAmYGE9o0e .section-8 rect,#mermaid-svg-IX6jKxyAmYGE9o0e .section-8 path,#mermaid-svg-IX6jKxyAmYGE9o0e .section-8 circle,#mermaid-svg-IX6jKxyAmYGE9o0e .section-8 polygon,#mermaid-svg-IX6jKxyAmYGE9o0e .section-8 path{fill:hsl(150, 100%, 76.2745098039%);}#mermaid-svg-IX6jKxyAmYGE9o0e .section-8 text{fill:black;}#mermaid-svg-IX6jKxyAmYGE9o0e .node-icon-8{font-size:40px;color:black;}#mermaid-svg-IX6jKxyAmYGE9o0e .section-edge-8{stroke:hsl(150, 100%, 76.2745098039%);}#mermaid-svg-IX6jKxyAmYGE9o0e .edge-depth-8{stroke-width:-10;}#mermaid-svg-IX6jKxyAmYGE9o0e .section-8 line{stroke:hsl(330, 100%, 86.2745098039%);stroke-width:3;}#mermaid-svg-IX6jKxyAmYGE9o0e .disabled,#mermaid-svg-IX6jKxyAmYGE9o0e .disabled circle,#mermaid-svg-IX6jKxyAmYGE9o0e .disabled text{fill:lightgray;}#mermaid-svg-IX6jKxyAmYGE9o0e .disabled text{fill:#efefef;}#mermaid-svg-IX6jKxyAmYGE9o0e .section-9 rect,#mermaid-svg-IX6jKxyAmYGE9o0e .section-9 path,#mermaid-svg-IX6jKxyAmYGE9o0e .section-9 circle,#mermaid-svg-IX6jKxyAmYGE9o0e .section-9 polygon,#mermaid-svg-IX6jKxyAmYGE9o0e .section-9 path{fill:hsl(180, 100%, 76.2745098039%);}#mermaid-svg-IX6jKxyAmYGE9o0e .section-9 text{fill:black;}#mermaid-svg-IX6jKxyAmYGE9o0e .node-icon-9{font-size:40px;color:black;}#mermaid-svg-IX6jKxyAmYGE9o0e .section-edge-9{stroke:hsl(180, 100%, 76.2745098039%);}#mermaid-svg-IX6jKxyAmYGE9o0e .edge-depth-9{stroke-width:-13;}#mermaid-svg-IX6jKxyAmYGE9o0e .section-9 line{stroke:hsl(0, 100%, 86.2745098039%);stroke-width:3;}#mermaid-svg-IX6jKxyAmYGE9o0e .disabled,#mermaid-svg-IX6jKxyAmYGE9o0e .disabled circle,#mermaid-svg-IX6jKxyAmYGE9o0e .disabled text{fill:lightgray;}#mermaid-svg-IX6jKxyAmYGE9o0e .disabled text{fill:#efefef;}#mermaid-svg-IX6jKxyAmYGE9o0e .section-10 rect,#mermaid-svg-IX6jKxyAmYGE9o0e .section-10 path,#mermaid-svg-IX6jKxyAmYGE9o0e .section-10 circle,#mermaid-svg-IX6jKxyAmYGE9o0e .section-10 polygon,#mermaid-svg-IX6jKxyAmYGE9o0e .section-10 path{fill:hsl(210, 100%, 76.2745098039%);}#mermaid-svg-IX6jKxyAmYGE9o0e .section-10 text{fill:black;}#mermaid-svg-IX6jKxyAmYGE9o0e .node-icon-10{font-size:40px;color:black;}#mermaid-svg-IX6jKxyAmYGE9o0e .section-edge-10{stroke:hsl(210, 100%, 76.2745098039%);}#mermaid-svg-IX6jKxyAmYGE9o0e .edge-depth-10{stroke-width:-16;}#mermaid-svg-IX6jKxyAmYGE9o0e .section-10 line{stroke:hsl(30, 100%, 86.2745098039%);stroke-width:3;}#mermaid-svg-IX6jKxyAmYGE9o0e .disabled,#mermaid-svg-IX6jKxyAmYGE9o0e .disabled circle,#mermaid-svg-IX6jKxyAmYGE9o0e .disabled text{fill:lightgray;}#mermaid-svg-IX6jKxyAmYGE9o0e .disabled text{fill:#efefef;}#mermaid-svg-IX6jKxyAmYGE9o0e .section-root rect,#mermaid-svg-IX6jKxyAmYGE9o0e .section-root path,#mermaid-svg-IX6jKxyAmYGE9o0e .section-root circle,#mermaid-svg-IX6jKxyAmYGE9o0e .section-root polygon{fill:hsl(240, 100%, 46.2745098039%);}#mermaid-svg-IX6jKxyAmYGE9o0e .section-root text{fill:#ffffff;}#mermaid-svg-IX6jKxyAmYGE9o0e .section-root span{color:#ffffff;}#mermaid-svg-IX6jKxyAmYGE9o0e .section-2 span{color:#ffffff;}#mermaid-svg-IX6jKxyAmYGE9o0e .icon-container{height:100%;display:flex;justify-content:center;align-items:center;}#mermaid-svg-IX6jKxyAmYGE9o0e .edge{fill:none;}#mermaid-svg-IX6jKxyAmYGE9o0e .mindmap-node-label{dy:1em;alignment-baseline:middle;text-anchor:middle;dominant-baseline:middle;text-align:center;}#mermaid-svg-IX6jKxyAmYGE9o0e :root{--mermaid-font-family:"trebuchet ms",verdana,arial,sans-serif;} 为什么以前没有Agent Loop
模型能力不足
Tool调用不稳定
推理深度不够
上下文窗口小
4K-8K token
循环几轮就爆
缺少工程框架
没有统一Tool协议
无Memory机制
实践验证不足
Prompt Engineering主导
尚未形成Agent范式
所以,Agent Loop 不是某个人「发明」的,而是 当模型能力 + 上下文窗口 + 工程框架三者同时就位时,自然演化出来的产物。
1.3 提出核心问题
读到这里,你可能会问:
Agent Loop 到底是什么?它和普通的 ChatGPT 对话有什么本质区别?为什么同样的模型,加一个「循环」就能让 AI 从「回答问题」变成「完成任务」?
这正是本文要彻底讲清楚的问题。接下来我们从最基础的传统 LLM 工作方式讲起,一步步带你理解 Agent Loop 的全貌。
第二章:传统 LLM 是如何工作的?
2.1 最朴素的三个概念:Prompt、Context、Completion
在理解 Agent Loop 之前,我们必须先回到原点,搞清楚 传统 LLM 到底是怎么工作的。所有大语言模型(无论 GPT、Claude、Gemini),其最底层的工作模式都可以用三个概念概括:
#mermaid-svg-ZdenaU3N2me5YD7D{font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;fill:#333;}@keyframes edge-animation-frame{from{stroke-dashoffset:0;}}@keyframes dash{to{stroke-dashoffset:0;}}#mermaid-svg-ZdenaU3N2me5YD7D .edge-animation-slow{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 50s linear infinite;stroke-linecap:round;}#mermaid-svg-ZdenaU3N2me5YD7D .edge-animation-fast{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 20s linear infinite;stroke-linecap:round;}#mermaid-svg-ZdenaU3N2me5YD7D .error-icon{fill:#552222;}#mermaid-svg-ZdenaU3N2me5YD7D .error-text{fill:#552222;stroke:#552222;}#mermaid-svg-ZdenaU3N2me5YD7D .edge-thickness-normal{stroke-width:1px;}#mermaid-svg-ZdenaU3N2me5YD7D .edge-thickness-thick{stroke-width:3.5px;}#mermaid-svg-ZdenaU3N2me5YD7D .edge-pattern-solid{stroke-dasharray:0;}#mermaid-svg-ZdenaU3N2me5YD7D .edge-thickness-invisible{stroke-width:0;fill:none;}#mermaid-svg-ZdenaU3N2me5YD7D .edge-pattern-dashed{stroke-dasharray:3;}#mermaid-svg-ZdenaU3N2me5YD7D .edge-pattern-dotted{stroke-dasharray:2;}#mermaid-svg-ZdenaU3N2me5YD7D .marker{fill:#333333;stroke:#333333;}#mermaid-svg-ZdenaU3N2me5YD7D .marker.cross{stroke:#333333;}#mermaid-svg-ZdenaU3N2me5YD7D svg{font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;}#mermaid-svg-ZdenaU3N2me5YD7D p{margin:0;}#mermaid-svg-ZdenaU3N2me5YD7D .label{font-family:"trebuchet ms",verdana,arial,sans-serif;color:#333;}#mermaid-svg-ZdenaU3N2me5YD7D .cluster-label text{fill:#333;}#mermaid-svg-ZdenaU3N2me5YD7D .cluster-label span{color:#333;}#mermaid-svg-ZdenaU3N2me5YD7D .cluster-label span p{background-color:transparent;}#mermaid-svg-ZdenaU3N2me5YD7D .label text,#mermaid-svg-ZdenaU3N2me5YD7D span{fill:#333;color:#333;}#mermaid-svg-ZdenaU3N2me5YD7D .node rect,#mermaid-svg-ZdenaU3N2me5YD7D .node circle,#mermaid-svg-ZdenaU3N2me5YD7D .node ellipse,#mermaid-svg-ZdenaU3N2me5YD7D .node polygon,#mermaid-svg-ZdenaU3N2me5YD7D .node path{fill:#ECECFF;stroke:#9370DB;stroke-width:1px;}#mermaid-svg-ZdenaU3N2me5YD7D .rough-node .label text,#mermaid-svg-ZdenaU3N2me5YD7D .node .label text,#mermaid-svg-ZdenaU3N2me5YD7D .image-shape .label,#mermaid-svg-ZdenaU3N2me5YD7D .icon-shape .label{text-anchor:middle;}#mermaid-svg-ZdenaU3N2me5YD7D .node .katex path{fill:#000;stroke:#000;stroke-width:1px;}#mermaid-svg-ZdenaU3N2me5YD7D .rough-node .label,#mermaid-svg-ZdenaU3N2me5YD7D .node .label,#mermaid-svg-ZdenaU3N2me5YD7D .image-shape .label,#mermaid-svg-ZdenaU3N2me5YD7D .icon-shape .label{text-align:center;}#mermaid-svg-ZdenaU3N2me5YD7D .node.clickable{cursor:pointer;}#mermaid-svg-ZdenaU3N2me5YD7D .root .anchor path{fill:#333333!important;stroke-width:0;stroke:#333333;}#mermaid-svg-ZdenaU3N2me5YD7D .arrowheadPath{fill:#333333;}#mermaid-svg-ZdenaU3N2me5YD7D .edgePath .path{stroke:#333333;stroke-width:2.0px;}#mermaid-svg-ZdenaU3N2me5YD7D .flowchart-link{stroke:#333333;fill:none;}#mermaid-svg-ZdenaU3N2me5YD7D .edgeLabel{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-ZdenaU3N2me5YD7D .edgeLabel p{background-color:rgba(232,232,232, 0.8);}#mermaid-svg-ZdenaU3N2me5YD7D .edgeLabel rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-ZdenaU3N2me5YD7D .labelBkg{background-color:rgba(232, 232, 232, 0.5);}#mermaid-svg-ZdenaU3N2me5YD7D .cluster rect{fill:#ffffde;stroke:#aaaa33;stroke-width:1px;}#mermaid-svg-ZdenaU3N2me5YD7D .cluster text{fill:#333;}#mermaid-svg-ZdenaU3N2me5YD7D .cluster span{color:#333;}#mermaid-svg-ZdenaU3N2me5YD7D div.mermaidTooltip{position:absolute;text-align:center;max-width:200px;padding:2px;font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:12px;background:hsl(80, 100%, 96.2745098039%);border:1px solid #aaaa33;border-radius:2px;pointer-events:none;z-index:100;}#mermaid-svg-ZdenaU3N2me5YD7D .flowchartTitleText{text-anchor:middle;font-size:18px;fill:#333;}#mermaid-svg-ZdenaU3N2me5YD7D rect.text{fill:none;stroke-width:0;}#mermaid-svg-ZdenaU3N2me5YD7D .icon-shape,#mermaid-svg-ZdenaU3N2me5YD7D .image-shape{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-ZdenaU3N2me5YD7D .icon-shape p,#mermaid-svg-ZdenaU3N2me5YD7D .image-shape p{background-color:rgba(232,232,232, 0.8);padding:2px;}#mermaid-svg-ZdenaU3N2me5YD7D .icon-shape .label rect,#mermaid-svg-ZdenaU3N2me5YD7D .image-shape .label rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-ZdenaU3N2me5YD7D .label-icon{display:inline-block;height:1em;overflow:visible;vertical-align:-0.125em;}#mermaid-svg-ZdenaU3N2me5YD7D .node .label-icon path{fill:currentColor;stroke:revert;stroke-width:revert;}#mermaid-svg-ZdenaU3N2me5YD7D :root{--mermaid-font-family:"trebuchet ms",verdana,arial,sans-serif;} 用户
Prompt
大语言模型
LLM
模型输出
Completion
- Prompt(提示词):你输入给模型的文本。比如「帮我写一个 Vue 登录页面」。
- Context(上下文):模型在生成回复时参考的全部信息。包括系统提示词、历史对话、当前 Prompt 等。这些信息会被拼接成一个 token 序列喂给模型。
- Completion(补全):模型输出的文本。本质上是模型基于 Context 预测「下一个最可能出现的 token」。
这里有一个很多人没意识到的本质:LLM 从来不是在「思考」,它是在「预测」。它根据上下文,一个 token 一个 token 地生成最符合统计规律的文本。当你问它「1+1等于几」,它回答「2」,不是因为它会算术,而是因为在它的训练数据里,「1+1=」后面跟着「2」的概率最高。
2.2 One-shot Inference:一次推理就结束
传统 LLM 的工作模式,学术界称为 One-shot Inference(单次推理) 。它的特点是:输入一次 Prompt,模型输出一次 Completion,整个交互就结束了。
我们用一段 TypeScript 代码来模拟这个最基础的过程:
typescript
// 传统 LLM 的单次推理模式(One-shot Inference)
interface LLMResponse {
content: string;
tokensUsed: number;
}
// 模拟一次 LLM 调用
async function callLLM(prompt: string): Promise<LLMResponse> {
// 实际工程中,这里会调用 OpenAI / Anthropic 的 API
console.log(`📤 发送 Prompt: "${prompt}"`);
// 模拟模型返回
const completion: LLMResponse = {
content: '这是一个 Vue 登录页面的代码:\n```vue\n<template>...</template>\n```',
tokensUsed: prompt.length + 150
};
return completion;
}
// 传统模式:一次 Prompt → 一次 Completion,结束
async function traditionalChat() {
const userPrompt = '帮我写一个 Vue 登录页面';
const response = await callLLM(userPrompt);
console.log(`📥 收到回复: ${response.content.slice(0, 50)}...`);
console.log(`💰 Token 消耗: ${response.tokensUsed}`);
// ⚠️ 注意:到这里交互就结束了
// 模型不会知道这段代码能不能跑通、有没有 Bug
}
traditionalChat();
运行这段代码,你会看到:
text
📤 发送 Prompt: "帮我写一个 Vue 登录页面"
📥 收到回复: 这是一个 Vue 登录页面的代码:```vue<template>...
💰 Token 消耗: 164
交互到此结束。 这就是传统 LLM 的全部工作模式。
2.3 这种模式为什么「够用」又「不够用」?
对于简单任务,One-shot Inference 是够用的:
| 任务类型 | 例子 | One-shot 是否够用 |
|---|---|---|
| 文本生成 | 「写一首关于秋天的诗」 | ✅ 够用 |
| 知识问答 | 「Vue 3 的响应式原理是什么?」 | ✅ 够用 |
| 代码片段 | 「写一个防抖函数」 | ✅ 够用 |
| 翻译润色 | 「把这段英文翻译成中文」 | ✅ 够用 |
| 复杂工程 | 「修复这个项目的 Bug」 | ❌ 完全不够 |
| 多步任务 | 「部署这个应用到生产环境」 | ❌ 完全不够 |
问题的关键在于:很多真实世界的任务,根本不是「一句话能完成的」 。它们需要多步骤、需要反馈、需要根据中间结果调整策略。这正是下一章要剖析的问题。
第三章:传统 LLM 为什么无法完成复杂任务?
3.1 一个真实的「修 Bug」场景
让我们看一个对开发者来说再熟悉不过的场景:修复项目中的 Bug。
假设你接到一个任务:「前端项目登录后偶尔会白屏,帮忙修复一下」。如果交给传统的 ChatGPT,会发生什么?
你可能会把报错信息贴给它,它给你一段「可能是这样、可能是那样」的建议。然后呢?然后没有了 。你还得自己手动去验证、去试错。如果它的建议不对,你得再贴新的信息,再来一轮------而且它不记得之前做了什么(除非你在对话里手动带上历史)。
真正修一个 Bug,完整的工作流是这样的:
#mermaid-svg-wmdSV7fUApVkCTWP{font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;fill:#333;}@keyframes edge-animation-frame{from{stroke-dashoffset:0;}}@keyframes dash{to{stroke-dashoffset:0;}}#mermaid-svg-wmdSV7fUApVkCTWP .edge-animation-slow{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 50s linear infinite;stroke-linecap:round;}#mermaid-svg-wmdSV7fUApVkCTWP .edge-animation-fast{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 20s linear infinite;stroke-linecap:round;}#mermaid-svg-wmdSV7fUApVkCTWP .error-icon{fill:#552222;}#mermaid-svg-wmdSV7fUApVkCTWP .error-text{fill:#552222;stroke:#552222;}#mermaid-svg-wmdSV7fUApVkCTWP .edge-thickness-normal{stroke-width:1px;}#mermaid-svg-wmdSV7fUApVkCTWP .edge-thickness-thick{stroke-width:3.5px;}#mermaid-svg-wmdSV7fUApVkCTWP .edge-pattern-solid{stroke-dasharray:0;}#mermaid-svg-wmdSV7fUApVkCTWP .edge-thickness-invisible{stroke-width:0;fill:none;}#mermaid-svg-wmdSV7fUApVkCTWP .edge-pattern-dashed{stroke-dasharray:3;}#mermaid-svg-wmdSV7fUApVkCTWP .edge-pattern-dotted{stroke-dasharray:2;}#mermaid-svg-wmdSV7fUApVkCTWP .marker{fill:#333333;stroke:#333333;}#mermaid-svg-wmdSV7fUApVkCTWP .marker.cross{stroke:#333333;}#mermaid-svg-wmdSV7fUApVkCTWP svg{font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;}#mermaid-svg-wmdSV7fUApVkCTWP p{margin:0;}#mermaid-svg-wmdSV7fUApVkCTWP .label{font-family:"trebuchet ms",verdana,arial,sans-serif;color:#333;}#mermaid-svg-wmdSV7fUApVkCTWP .cluster-label text{fill:#333;}#mermaid-svg-wmdSV7fUApVkCTWP .cluster-label span{color:#333;}#mermaid-svg-wmdSV7fUApVkCTWP .cluster-label span p{background-color:transparent;}#mermaid-svg-wmdSV7fUApVkCTWP .label text,#mermaid-svg-wmdSV7fUApVkCTWP span{fill:#333;color:#333;}#mermaid-svg-wmdSV7fUApVkCTWP .node rect,#mermaid-svg-wmdSV7fUApVkCTWP .node circle,#mermaid-svg-wmdSV7fUApVkCTWP .node ellipse,#mermaid-svg-wmdSV7fUApVkCTWP .node polygon,#mermaid-svg-wmdSV7fUApVkCTWP .node path{fill:#ECECFF;stroke:#9370DB;stroke-width:1px;}#mermaid-svg-wmdSV7fUApVkCTWP .rough-node .label text,#mermaid-svg-wmdSV7fUApVkCTWP .node .label text,#mermaid-svg-wmdSV7fUApVkCTWP .image-shape .label,#mermaid-svg-wmdSV7fUApVkCTWP .icon-shape .label{text-anchor:middle;}#mermaid-svg-wmdSV7fUApVkCTWP .node .katex path{fill:#000;stroke:#000;stroke-width:1px;}#mermaid-svg-wmdSV7fUApVkCTWP .rough-node .label,#mermaid-svg-wmdSV7fUApVkCTWP .node .label,#mermaid-svg-wmdSV7fUApVkCTWP .image-shape .label,#mermaid-svg-wmdSV7fUApVkCTWP .icon-shape .label{text-align:center;}#mermaid-svg-wmdSV7fUApVkCTWP .node.clickable{cursor:pointer;}#mermaid-svg-wmdSV7fUApVkCTWP .root .anchor path{fill:#333333!important;stroke-width:0;stroke:#333333;}#mermaid-svg-wmdSV7fUApVkCTWP .arrowheadPath{fill:#333333;}#mermaid-svg-wmdSV7fUApVkCTWP .edgePath .path{stroke:#333333;stroke-width:2.0px;}#mermaid-svg-wmdSV7fUApVkCTWP .flowchart-link{stroke:#333333;fill:none;}#mermaid-svg-wmdSV7fUApVkCTWP .edgeLabel{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-wmdSV7fUApVkCTWP .edgeLabel p{background-color:rgba(232,232,232, 0.8);}#mermaid-svg-wmdSV7fUApVkCTWP .edgeLabel rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-wmdSV7fUApVkCTWP .labelBkg{background-color:rgba(232, 232, 232, 0.5);}#mermaid-svg-wmdSV7fUApVkCTWP .cluster rect{fill:#ffffde;stroke:#aaaa33;stroke-width:1px;}#mermaid-svg-wmdSV7fUApVkCTWP .cluster text{fill:#333;}#mermaid-svg-wmdSV7fUApVkCTWP .cluster span{color:#333;}#mermaid-svg-wmdSV7fUApVkCTWP div.mermaidTooltip{position:absolute;text-align:center;max-width:200px;padding:2px;font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:12px;background:hsl(80, 100%, 96.2745098039%);border:1px solid #aaaa33;border-radius:2px;pointer-events:none;z-index:100;}#mermaid-svg-wmdSV7fUApVkCTWP .flowchartTitleText{text-anchor:middle;font-size:18px;fill:#333;}#mermaid-svg-wmdSV7fUApVkCTWP rect.text{fill:none;stroke-width:0;}#mermaid-svg-wmdSV7fUApVkCTWP .icon-shape,#mermaid-svg-wmdSV7fUApVkCTWP .image-shape{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-wmdSV7fUApVkCTWP .icon-shape p,#mermaid-svg-wmdSV7fUApVkCTWP .image-shape p{background-color:rgba(232,232,232, 0.8);padding:2px;}#mermaid-svg-wmdSV7fUApVkCTWP .icon-shape .label rect,#mermaid-svg-wmdSV7fUApVkCTWP .image-shape .label rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-wmdSV7fUApVkCTWP .label-icon{display:inline-block;height:1em;overflow:visible;vertical-align:-0.125em;}#mermaid-svg-wmdSV7fUApVkCTWP .node .label-icon path{fill:currentColor;stroke:revert;stroke-width:revert;}#mermaid-svg-wmdSV7fUApVkCTWP :root{--mermaid-font-family:"trebuchet ms",verdana,arial,sans-serif;} 是
否
步骤1:查看项目结构
了解这是什么项目
步骤2:分析相关代码
定位可能的 Bug 位置
步骤3:运行 Build / 测试
复现问题
步骤4:读取报错信息
理解根本原因
步骤5:修改代码
步骤6:再次 Build / 测试
是否还有错误?
步骤7:修复完成
请仔细看这张图,它揭示了一个关键事实:这是一个「循环」结构 。第 4 步(读错误)→ 第 5 步(改)→ 第 6 步(测试)→ 如果还有错误 → 回到第 4 步。
3.2 传统 LLM 的三大致命缺陷
对比上面的完整工作流,传统 One-shot Inference 模式有 三个无法逾越的致命缺陷:
缺陷一:无法主动获取信息(缺少 Act 能力)
传统 LLM 只能被动接收你给它的信息。但修 Bug 需要主动去 读文件、跑命令、看日志。它没有「手」,无法操作。
typescript
// ❌ 传统 LLM 无法做到这样
function traditionalLLM() {
// LLM 只能基于用户贴过来的文本"猜"
// 它不能主动执行:
// - fs.readFile() 读源码
// - exec('npm run build') 跑构建
// - 看构建输出来判断对不对
// 它只能输出"建议",然后等待用户手动验证后反馈
}
缺陷二:无法根据反馈调整(缺少 Observe 能力)
假设你手动帮它跑了 Build,把错误信息贴回来了。它给你新的建议。但问题是:它不知道这已经是第几次修改了 。它把每一条消息都当成独立事件,缺乏「我上次改了 A,报错了,说明 A 不对,我这次试试 B」的基于观察的迭代能力。
缺陷三:无法持续执行(缺少 Loop 能力)
这是最根本的。传统 LLM 的交互是 请求-响应 模式:你发一次,它回一次。它不会自己决定「我还要再做一步」。修一个 Bug 可能需要 5 次迭代,传统 LLM 无法自己维持这个迭代过程,必须靠人类一户户驱动。
3.3 用一张图总结传统 LLM 的「无力感」
#mermaid-svg-yDSEL2KCVsl12FQ2{font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;fill:#333;}@keyframes edge-animation-frame{from{stroke-dashoffset:0;}}@keyframes dash{to{stroke-dashoffset:0;}}#mermaid-svg-yDSEL2KCVsl12FQ2 .edge-animation-slow{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 50s linear infinite;stroke-linecap:round;}#mermaid-svg-yDSEL2KCVsl12FQ2 .edge-animation-fast{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 20s linear infinite;stroke-linecap:round;}#mermaid-svg-yDSEL2KCVsl12FQ2 .error-icon{fill:#552222;}#mermaid-svg-yDSEL2KCVsl12FQ2 .error-text{fill:#552222;stroke:#552222;}#mermaid-svg-yDSEL2KCVsl12FQ2 .edge-thickness-normal{stroke-width:1px;}#mermaid-svg-yDSEL2KCVsl12FQ2 .edge-thickness-thick{stroke-width:3.5px;}#mermaid-svg-yDSEL2KCVsl12FQ2 .edge-pattern-solid{stroke-dasharray:0;}#mermaid-svg-yDSEL2KCVsl12FQ2 .edge-thickness-invisible{stroke-width:0;fill:none;}#mermaid-svg-yDSEL2KCVsl12FQ2 .edge-pattern-dashed{stroke-dasharray:3;}#mermaid-svg-yDSEL2KCVsl12FQ2 .edge-pattern-dotted{stroke-dasharray:2;}#mermaid-svg-yDSEL2KCVsl12FQ2 .marker{fill:#333333;stroke:#333333;}#mermaid-svg-yDSEL2KCVsl12FQ2 .marker.cross{stroke:#333333;}#mermaid-svg-yDSEL2KCVsl12FQ2 svg{font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;}#mermaid-svg-yDSEL2KCVsl12FQ2 p{margin:0;}#mermaid-svg-yDSEL2KCVsl12FQ2 .label{font-family:"trebuchet ms",verdana,arial,sans-serif;color:#333;}#mermaid-svg-yDSEL2KCVsl12FQ2 .cluster-label text{fill:#333;}#mermaid-svg-yDSEL2KCVsl12FQ2 .cluster-label span{color:#333;}#mermaid-svg-yDSEL2KCVsl12FQ2 .cluster-label span p{background-color:transparent;}#mermaid-svg-yDSEL2KCVsl12FQ2 .label text,#mermaid-svg-yDSEL2KCVsl12FQ2 span{fill:#333;color:#333;}#mermaid-svg-yDSEL2KCVsl12FQ2 .node rect,#mermaid-svg-yDSEL2KCVsl12FQ2 .node circle,#mermaid-svg-yDSEL2KCVsl12FQ2 .node ellipse,#mermaid-svg-yDSEL2KCVsl12FQ2 .node polygon,#mermaid-svg-yDSEL2KCVsl12FQ2 .node path{fill:#ECECFF;stroke:#9370DB;stroke-width:1px;}#mermaid-svg-yDSEL2KCVsl12FQ2 .rough-node .label text,#mermaid-svg-yDSEL2KCVsl12FQ2 .node .label text,#mermaid-svg-yDSEL2KCVsl12FQ2 .image-shape .label,#mermaid-svg-yDSEL2KCVsl12FQ2 .icon-shape .label{text-anchor:middle;}#mermaid-svg-yDSEL2KCVsl12FQ2 .node .katex path{fill:#000;stroke:#000;stroke-width:1px;}#mermaid-svg-yDSEL2KCVsl12FQ2 .rough-node .label,#mermaid-svg-yDSEL2KCVsl12FQ2 .node .label,#mermaid-svg-yDSEL2KCVsl12FQ2 .image-shape .label,#mermaid-svg-yDSEL2KCVsl12FQ2 .icon-shape .label{text-align:center;}#mermaid-svg-yDSEL2KCVsl12FQ2 .node.clickable{cursor:pointer;}#mermaid-svg-yDSEL2KCVsl12FQ2 .root .anchor path{fill:#333333!important;stroke-width:0;stroke:#333333;}#mermaid-svg-yDSEL2KCVsl12FQ2 .arrowheadPath{fill:#333333;}#mermaid-svg-yDSEL2KCVsl12FQ2 .edgePath .path{stroke:#333333;stroke-width:2.0px;}#mermaid-svg-yDSEL2KCVsl12FQ2 .flowchart-link{stroke:#333333;fill:none;}#mermaid-svg-yDSEL2KCVsl12FQ2 .edgeLabel{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-yDSEL2KCVsl12FQ2 .edgeLabel p{background-color:rgba(232,232,232, 0.8);}#mermaid-svg-yDSEL2KCVsl12FQ2 .edgeLabel rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-yDSEL2KCVsl12FQ2 .labelBkg{background-color:rgba(232, 232, 232, 0.5);}#mermaid-svg-yDSEL2KCVsl12FQ2 .cluster rect{fill:#ffffde;stroke:#aaaa33;stroke-width:1px;}#mermaid-svg-yDSEL2KCVsl12FQ2 .cluster text{fill:#333;}#mermaid-svg-yDSEL2KCVsl12FQ2 .cluster span{color:#333;}#mermaid-svg-yDSEL2KCVsl12FQ2 div.mermaidTooltip{position:absolute;text-align:center;max-width:200px;padding:2px;font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:12px;background:hsl(80, 100%, 96.2745098039%);border:1px solid #aaaa33;border-radius:2px;pointer-events:none;z-index:100;}#mermaid-svg-yDSEL2KCVsl12FQ2 .flowchartTitleText{text-anchor:middle;font-size:18px;fill:#333;}#mermaid-svg-yDSEL2KCVsl12FQ2 rect.text{fill:none;stroke-width:0;}#mermaid-svg-yDSEL2KCVsl12FQ2 .icon-shape,#mermaid-svg-yDSEL2KCVsl12FQ2 .image-shape{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-yDSEL2KCVsl12FQ2 .icon-shape p,#mermaid-svg-yDSEL2KCVsl12FQ2 .image-shape p{background-color:rgba(232,232,232, 0.8);padding:2px;}#mermaid-svg-yDSEL2KCVsl12FQ2 .icon-shape .label rect,#mermaid-svg-yDSEL2KCVsl12FQ2 .image-shape .label rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-yDSEL2KCVsl12FQ2 .label-icon{display:inline-block;height:1em;overflow:visible;vertical-align:-0.125em;}#mermaid-svg-yDSEL2KCVsl12FQ2 .node .label-icon path{fill:currentColor;stroke:revert;stroke-width:revert;}#mermaid-svg-yDSEL2KCVsl12FQ2 :root{--mermaid-font-family:"trebuchet ms",verdana,arial,sans-serif;} 能力缺口
传统 LLM(无循环)
❌ 无法继续
等待人类驱动
接收Prompt
输出建议
结束
真实任务:修复 Bug(需要循环)
否
是
读代码
改代码
测试
通过?
完成
这张图说明了一切:真实任务是循环的,而传统 LLM 是线性的 。两者之间存在巨大的「能力缺口」。这个缺口,正是 Agent Loop 要填平的东西。
💡 小结 :传统 LLM 的本质是「单次补全器」,它缺少三个关键能力:主动行动(Act)、观察反馈(Observe)、持续循环(Loop)。Agent Loop 就是为了补齐这三个能力而生的。在下一章,我们将正式揭开 Agent Loop 的面纱。
本文为系列第一篇。下篇将深入 Agent Loop 执行流程与内部实现。