欢迎光临
我们一直在努力

37|Agent 是什么:计划-执行-反馈循环与停止条件

欢迎来到 卷 5:Agent 系统:规划、执行、记忆与可靠性。

在前面的章节中,我们给了 AI 知识(RAG),也给了它工具(MCP/Skills)。但此时的 AI 依然像是一个**“算盘”或者“点读机”**——你拨一下,它动一下;你问一句,它答一句。

如果你的需求是:“去把昨天的报错日志查出来,分析一下原因,如果是个小 Bug 就直接写代码修复,然后提一个 PR(代码合并请求)。”
面对这种跨越多个步骤的复杂任务,只会“一问一答”的模型会瞬间宕机。

这就需要引入目前 AI 领域最火的概念:Agent(智能体)。
本篇,我们就来揭开 Agent 的神秘面纱,看看它究竟是怎么“自己动起来”的。


1. 核心概念:什么是 Agent?

Agent(智能体)不是一个特定的模型(比如 GPT-4),而是一种系统架构设计。
你可以把 Agent 理解为:让大模型(LLM)充当系统的“大脑”,赋予它自主规划任务、调用工具、并根据反馈不断调整策略的能力。

Copilot vs Agent

  • Copilot(副驾驶):人是司机,AI 是副驾。人决定什么时候踩油门,AI 只负责在旁边递递地图。
  • Agent(自动驾驶):AI 是司机。人只需要说“送我去机场”,AI 会自己看红绿灯、自己打方向盘、遇到修路自己绕道,直到把你送到目的地。

2. Agent 的灵魂:计划-执行-反馈循环(ReAct)

Agent 之所以能“自动驾驶”,核心秘密在于一个被称为 ReAct(Reasoning and Acting,推理与行动) 的底层循环机制。
当 Agent 接收到一个复杂任务时,它会在后台开启一个死循环,直到任务完成。这个循环包含三个关键动作:

  • Thought(思考 / 计划 – Planner)
    • 大脑开始转动:“用户让我修 Bug。我第一步应该先去读日志,看看是什么错误。”
  • Action(执行 – Executor)
    • 决定好之后,立刻去工具箱(MCP)里拿出 read_logs 工具,执行动作。
  • Observation(观察 / 反馈 – Feedback)
    • 工具执行完后,把结果拿回来看看。
    • 观察结果:“哦,日志里说是一个空指针异常(NullPointer)。那我下一步该去修改对应的代码文件了。”
  • 然后,Agent 再次进入 Thought(思考第二步该干嘛),接着 Action,接着 Observation……如此反复,一步步逼近最终目标。


    3. 悬崖勒马:致命的“停止条件(Stop Condition)”

    Agent 的这种死循环机制非常强大,但也极其危险。
    想象一下,如果 Agent 遇到一个解不开的 Bug,它可能会:修改代码 -> 运行测试 -> 报错 -> 继续修改 -> 继续报错……
    如果不加以限制,它会在后台疯狂循环几千次,一晚上的时间就能把你的大模型 API 账额刷掉几万块钱!

    因此,每一个合格的 Agent 系统,必须设置严格的 停止条件(Stop Condition)。

  • 成功停止:大模型自己判断任务已经圆满完成,输出 Finish 信号,把最终结果交给用户。
  • 最大步数(Max Steps / Max Iterations):硬性规定,比如最多只能循环 10 次。如果 10 步还没干完,强制停止并求助人类。
  • 硬失败(Hard Failure):调用的工具连续崩溃报错超过 3 次,Agent 会判定“此路不通”,主动放弃。
  • 人类干预(Human-in-the-loop):遇到高危操作(比如删除数据库),Agent 必须暂停,发消息让人类点“同意”才能继续。

  • 4. 本篇产出:通用 Agent 状态机图

    为了让你更直观地理解 Agent 是如何在后台运行的,这里提供一张标准的 Agent 状态机(State Machine)流转图。
    这也是目前业界主流 Agent 框架(如 LangChain 的 AgentExecutor、AutoGPT 等)的底层运行逻辑:

    #mermaid-svg-2NIvEcdST7miwjqL{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-2NIvEcdST7miwjqL .edge-animation-slow{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 50s linear infinite;stroke-linecap:round;}#mermaid-svg-2NIvEcdST7miwjqL .edge-animation-fast{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 20s linear infinite;stroke-linecap:round;}#mermaid-svg-2NIvEcdST7miwjqL .error-icon{fill:#552222;}#mermaid-svg-2NIvEcdST7miwjqL .error-text{fill:#552222;stroke:#552222;}#mermaid-svg-2NIvEcdST7miwjqL .edge-thickness-normal{stroke-width:1px;}#mermaid-svg-2NIvEcdST7miwjqL .edge-thickness-thick{stroke-width:3.5px;}#mermaid-svg-2NIvEcdST7miwjqL .edge-pattern-solid{stroke-dasharray:0;}#mermaid-svg-2NIvEcdST7miwjqL .edge-thickness-invisible{stroke-width:0;fill:none;}#mermaid-svg-2NIvEcdST7miwjqL .edge-pattern-dashed{stroke-dasharray:3;}#mermaid-svg-2NIvEcdST7miwjqL .edge-pattern-dotted{stroke-dasharray:2;}#mermaid-svg-2NIvEcdST7miwjqL .marker{fill:#333333;stroke:#333333;}#mermaid-svg-2NIvEcdST7miwjqL .marker.cross{stroke:#333333;}#mermaid-svg-2NIvEcdST7miwjqL svg{font-family:\”trebuchet ms\”,verdana,arial,sans-serif;font-size:16px;}#mermaid-svg-2NIvEcdST7miwjqL p{margin:0;}#mermaid-svg-2NIvEcdST7miwjqL defs #statediagram-barbEnd{fill:#333333;stroke:#333333;}#mermaid-svg-2NIvEcdST7miwjqL g.stateGroup text{fill:#9370DB;stroke:none;font-size:10px;}#mermaid-svg-2NIvEcdST7miwjqL g.stateGroup text{fill:#333;stroke:none;font-size:10px;}#mermaid-svg-2NIvEcdST7miwjqL g.stateGroup .state-title{font-weight:bolder;fill:#131300;}#mermaid-svg-2NIvEcdST7miwjqL g.stateGroup rect{fill:#ECECFF;stroke:#9370DB;}#mermaid-svg-2NIvEcdST7miwjqL g.stateGroup line{stroke:#333333;stroke-width:1;}#mermaid-svg-2NIvEcdST7miwjqL .transition{stroke:#333333;stroke-width:1;fill:none;}#mermaid-svg-2NIvEcdST7miwjqL .stateGroup .composit{fill:white;border-bottom:1px;}#mermaid-svg-2NIvEcdST7miwjqL .stateGroup .alt-composit{fill:#e0e0e0;border-bottom:1px;}#mermaid-svg-2NIvEcdST7miwjqL .state-note{stroke:#aaaa33;fill:#fff5ad;}#mermaid-svg-2NIvEcdST7miwjqL .state-note text{fill:black;stroke:none;font-size:10px;}#mermaid-svg-2NIvEcdST7miwjqL .stateLabel .box{stroke:none;stroke-width:0;fill:#ECECFF;opacity:0.5;}#mermaid-svg-2NIvEcdST7miwjqL .edgeLabel .label rect{fill:#ECECFF;opacity:0.5;}#mermaid-svg-2NIvEcdST7miwjqL .edgeLabel{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-2NIvEcdST7miwjqL .edgeLabel p{background-color:rgba(232,232,232, 0.8);}#mermaid-svg-2NIvEcdST7miwjqL .edgeLabel rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-2NIvEcdST7miwjqL .edgeLabel .label text{fill:#333;}#mermaid-svg-2NIvEcdST7miwjqL .label div .edgeLabel{color:#333;}#mermaid-svg-2NIvEcdST7miwjqL .stateLabel text{fill:#131300;font-size:10px;font-weight:bold;}#mermaid-svg-2NIvEcdST7miwjqL .node circle.state-start{fill:#333333;stroke:#333333;}#mermaid-svg-2NIvEcdST7miwjqL .node .fork-join{fill:#333333;stroke:#333333;}#mermaid-svg-2NIvEcdST7miwjqL .node circle.state-end{fill:#9370DB;stroke:white;stroke-width:1.5;}#mermaid-svg-2NIvEcdST7miwjqL .end-state-inner{fill:white;stroke-width:1.5;}#mermaid-svg-2NIvEcdST7miwjqL .node rect{fill:#ECECFF;stroke:#9370DB;stroke-width:1px;}#mermaid-svg-2NIvEcdST7miwjqL .node polygon{fill:#ECECFF;stroke:#9370DB;stroke-width:1px;}#mermaid-svg-2NIvEcdST7miwjqL #statediagram-barbEnd{fill:#333333;}#mermaid-svg-2NIvEcdST7miwjqL .statediagram-cluster rect{fill:#ECECFF;stroke:#9370DB;stroke-width:1px;}#mermaid-svg-2NIvEcdST7miwjqL .cluster-label,#mermaid-svg-2NIvEcdST7miwjqL .nodeLabel{color:#131300;}#mermaid-svg-2NIvEcdST7miwjqL .statediagram-cluster rect.outer{rx:5px;ry:5px;}#mermaid-svg-2NIvEcdST7miwjqL .statediagram-state .divider{stroke:#9370DB;}#mermaid-svg-2NIvEcdST7miwjqL .statediagram-state .title-state{rx:5px;ry:5px;}#mermaid-svg-2NIvEcdST7miwjqL .statediagram-cluster.statediagram-cluster .inner{fill:white;}#mermaid-svg-2NIvEcdST7miwjqL .statediagram-cluster.statediagram-cluster-alt .inner{fill:#f0f0f0;}#mermaid-svg-2NIvEcdST7miwjqL .statediagram-cluster .inner{rx:0;ry:0;}#mermaid-svg-2NIvEcdST7miwjqL .statediagram-state rect.basic{rx:5px;ry:5px;}#mermaid-svg-2NIvEcdST7miwjqL .statediagram-state rect.divider{stroke-dasharray:10,10;fill:#f0f0f0;}#mermaid-svg-2NIvEcdST7miwjqL .note-edge{stroke-dasharray:5;}#mermaid-svg-2NIvEcdST7miwjqL .statediagram-note rect{fill:#fff5ad;stroke:#aaaa33;stroke-width:1px;rx:0;ry:0;}#mermaid-svg-2NIvEcdST7miwjqL .statediagram-note rect{fill:#fff5ad;stroke:#aaaa33;stroke-width:1px;rx:0;ry:0;}#mermaid-svg-2NIvEcdST7miwjqL .statediagram-note text{fill:black;}#mermaid-svg-2NIvEcdST7miwjqL .statediagram-note .nodeLabel{color:black;}#mermaid-svg-2NIvEcdST7miwjqL .statediagram .edgeLabel{color:red;}#mermaid-svg-2NIvEcdST7miwjqL #dependencyStart,#mermaid-svg-2NIvEcdST7miwjqL #dependencyEnd{fill:#333333;stroke:#333333;stroke-width:1;}#mermaid-svg-2NIvEcdST7miwjqL .statediagramTitleText{text-anchor:middle;font-size:18px;fill:#333;}#mermaid-svg-2NIvEcdST7miwjqL :root{–mermaid-font-family:\”trebuchet ms\”,verdana,arial,sans-serif;}

    核心循环_ReAct

    用户输入模糊需求

    LLM 评估当前状态

    还没完成,决定调用工具 X

    执行工具并获取返回结果

    将结果喂给 LLM,重新思考

    任务成功,输出最终结果

    循环次数超过 Max_Steps

    遇到严重系统错误

    触发高危动作 (如发布/删除)

    审批通过

    审批驳回

    抛出异常或半成品,请求人类介入

    接收任务

    Thought_思考

    判断是否完成

    Action_执行

    Observation_观察

    强制终止

    人类审批

    看懂这张图的关键点:

    • 整个 Agent 的核心就在中间那个不断旋转的**“核心循环_ReAct”**里。大模型在这里面既当“军师”又当“总司令”。
    • 左下角那排灰色的**“安全停止机制”**,是区分一个 Agent 是“实验室玩具”还是“工业级产品”的试金石。

    总结与复盘

    • Agent 不是一个新模型,而是一种让大模型“做循环”的工程架构。
    • 它通过 Thought(思考) -> Action(执行) -> Observation(观察) 的反馈循环,一步步拆解并解决复杂问题。
    • 设计 Agent 时,最考验功力的不是怎么让它跑起来,而是怎么让它安全地停下来(停止条件)。

    下一步路线提示:
    有了这个底层循环机制,Agent 看起来似乎无所不能了。
    但是,如果你给它一个极其模糊的任务,比如“帮我开发一个贪吃蛇游戏”,如果它不做详细的规划,一上来就直接写代码,大概率会写成一团乱麻。
    如何让 Agent 学会像人类架构师一样,先把大目标拆成小任务?下一篇,我们将探讨:《任务分解:从模糊目标到可执行子任务》。

    赞(0)
    未经允许不得转载:171主机测评 » 37|Agent 是什么:计划-执行-反馈循环与停止条件
    分享到: 更多 (0)

    评论 抢沙发

    • 昵称 (必填)
    • 邮箱 (必填)
    • 网址