新闻详情

新闻详情

首页 / 资讯中心 / 详情

Agent Loop 拆解

发布时间:2026/9/29 3:46:50来源:尧图网络
Agent Loop 拆解
思考回答下面三个问题:Agent Loop 到底是什么?LangChain 是怎么把这个 Loop 实现出来的?LLM 明明只是输出,为什么它能驱动整个程序继续运行?一、LangChain 的 Agent Loop 到底是什么?LangChain 当前的create_agent()本质上创建的是一个LangGraphCompiledStateGraph。官方源码直接说明:Agent 会调用模型;如果AIMessage中存在tool_calls,就进入工具节点;工具结果作为ToolMessage写回消息状态;然后再次调用模型;如此重复,直到模型不再产生tool_calls。因此最核心的 Loop 可以抽象成:┌──────────────────┐ │ User │ └────────┬─────────┘ ↓ ┌──────────────────┐ │ State │ │ messages │ └────────┬─────────┘ ↓ ┌───────────┐ ┌────→│ LLM │ │ └─────┬─────┘ │ ↓ │ AIMessage │ │ │ 有 tool_calls? │ / \ │ Yes No │ ↓ ↓ │ ToolNode END │ ↓ │ ToolMessage │ ↓ └──── State一句话:LangChain Agent Loop = Model → 判断 Tool Call → Tool 执行 → Tool Result 写入 State → Model → …… → 无 Tool Call → End。这看似简单,但真正的工程实现远比这个 Loop 复杂。二、第一层:最原始的 Agent Loop如果完全不用 LangChain,其实只需要几十行代码。LangGraph 官方 Quickstart 给出的最基本实现,本质就是:whileTrue:model_response=call_llm(messages)ifnotmodel_response.tool_calls:breaktool_results=execute_tools(model_response.tool_calls)messages+=[model_response,*tool_results]LangGraph 的官方 Quickstart 就直接用这种while True形式展示 Agent Loop。所以 Agent 的本质并没有那么神秘。可以把它压缩成:whilenotfinished:response=LLM(state)ifresponse.tool_calls:results=Tools(response.tool_calls)state.update(results)else:finished=True这就是 Agent。三、那么 LangChain 为什么还需要 LangGraph?因为生产级 Agent 不是简单的while True。状态管理 工具并行 工具错误 重试 超时 Human-in-the-loop 中断恢复 Memory Streaming Middleware 动态模型 动态工具 结构化输出 多 Agent 持久化 Tracing如果全部塞进:whileTrue:代码会迅速失控。所以 LangChain 的设计变成:Agent │ create_agent() │ ↓ LangGraph Runtime │ ┌───────┴────────┐ ↓ ↓ StateGraph Runtime │ ├── model ├── tools ├── middleware ├── conditional edges └── state当前 LangChain v1 的create_agent()返回的就是CompiledStateGraph,而不是一个简单的 Python Agent 类。四、核心秘密:Agent Loop 实际上被“图”实现了这是理解 LangChain Agent 的关键。逻辑上:LLM ↓ Tool ↓ LLM ↓ Tool ↓ LLM程序实现上却变成:START │ ↓ ┌─────────┐ │ model │ └────┬────┘ │ ↓ tool_calls? / \ Yes No ↓ ↓ ┌──────┐ END │tools │ └──┬───┘ │ └────────→ model所以:Agent Loop 不是 LangChain 写了一个神秘的 Agent 循环,而是把循环表达成了 LangGraph 的 StateGraph + Conditional Edge。LangGraph 官方 Quickstart 的底层代码正是:builder.add_node("llm_call",llm_call)builder.add_node("tool_node",tool_node)builder.add_edge(START,"llm_call")builder.add_conditional_edges("llm_call",should_continue,["tool_node",END])builder.add_edge("tool_node","llm_call")这四行代码基本把 Agent Loop 的骨架完全暴露出来了。五、最关键的节点:Model Node现在来看第一次 LLM 调用。用户:“北京今天的天气怎么样?”State:{"messages":[HumanMessage(.
网站建设高端定制企业官网
RELATED

相关资讯

更多精彩内容,欢迎继续阅读

较早相关资讯

最新相关资讯

Ubuntu中文输入法配置指南:IBus与Fcitx5框架、引擎、前端模块排障 2026/9/29 4:40:18

Ubuntu中文输入法配置指南:IBus与Fcitx5框架、引擎、前端模块排障

/* MD / 富文本中的 .toc(含博客园搬家等嵌套结构);.toc-box 在侧栏,不受影响 */#content_views .toc,/* 编辑器常在目录前后插入空 p(:empty 仍占 20px),一并去掉避免顶空隙 */#content_views.markdown_views > p:empty:has(+ .toc),#content_views.markdown_views …

阅读更多 →
客户关系管理系统-springboot + vue 2026/9/29 4:40:18

客户关系管理系统-springboot + vue

本项目为前几天收费帮学妹做的一个项目,在工作环境中基本使用不到,但是很多学校把这个当作编程入门的项目来做,故分享出本项目供初学者参考。 一、项目描述 基于springboot vue的客户关系管理系统 登录网址: http://localhost:8081/ 管理员…

阅读更多 →
新冠病毒密接者跟踪系统-springboot + vue 2026/9/29 4:40:18

新冠病毒密接者跟踪系统-springboot + vue

本项目为前几天收费帮学妹做的一个项目,在工作环境中基本使用不到,但是很多学校把这个当作编程入门的项目来做,故分享出本项目供初学者参考。 一、项目描述 基于springboot vue的新冠病毒密接者跟踪系统 登录网址:http://localhost:8081/ …

阅读更多 →
DIAMOND序列比对:替代BLAST的高性能蛋白搜索工具 2026/9/29 4:40:18

DIAMOND序列比对:替代BLAST的高性能蛋白搜索工具

/* MD / 富文本中的 .toc(含博客园搬家等嵌套结构);.toc-box 在侧栏,不受影响 */#content_views .toc,/* 编辑器常在目录前后插入空 p(:empty 仍占 20px),一并去掉避免顶空隙 */#content_views.markdown_views > p:empty:has(+ .toc),#content_views.markdown_views …

阅读更多 →
AI视频生成工具怎么选?38个文生视频、图生视频与数字人网站评测 2026/9/29 4:40:12

AI视频生成工具怎么选?38个文生视频、图生视频与数字人网站评测

/* MD / 富文本中的 .toc(含博客园搬家等嵌套结构);.toc-box 在侧栏,不受影响 */#content_views .toc,/* 编辑器常在目录前后插入空 p(:empty 仍占 20px),一并去掉避免顶空隙 */#content_views.markdown_views > p:empty:has(+ .toc),#content_views.markdown_views …

阅读更多 →
只要13个单词就能给ChatGPT下毒?用TaoToken统一Key复现提示注入攻击链 2026/9/29 4:40:12

只要13个单词就能给ChatGPT下毒?用TaoToken统一Key复现提示注入攻击链

/* MD / 富文本中的 .toc(含博客园搬家等嵌套结构);.toc-box 在侧栏,不受影响 */#content_views .toc,/* 编辑器常在目录前后插入空 p(:empty 仍占 20px),一并去掉避免顶空隙 */#content_views.markdown_views > p:empty:has(+ .toc),#content_views.markdown_views …

阅读更多 →

今日资讯

本周资讯

本月资讯

看完文章仍有疑问?

联系尧图顾问,获取一对一建站咨询

立即免费咨询 📞 400-888-8888
📞 ✉