
如果你用过 ChatGPT 或 Claude的话对标准聊天机器人的工作方式应该不陌生:提问然后得到一个回答。但如果交给它一个多步骤任务呢?比如:“帮我找到最便宜航班,查询我的常旅客积分,并预订最佳选项”。
这时标准聊天机器人就会卡住,因为它没有在单次响应之后继续推进的机制。
智能体循环架构(agentic loop architecture)就是为了解决这一局限而出现的。下面这篇介绍会说明它是什么、如何工作,以及如何把它实现出来。
聊天机器人以单次通过(single pass)的方式运行他的交互流程是:用户发送消息,LLM 生成响应,流程结束。一次输入对应一次输出,没有任何记忆或状态被带入下一步。
智能体则是被设计用来行动(act),而不只是响应。两者之间的差异,可以归结为一个非常基础的编程概念while 循环。
智能体循环是一种迭代循环:LLM 在其中使用工具、根据反馈做出调整,并反复执行这一过程,直到任务彻底完成。
智能体循环一般跨越五个阶段。
在多数任务中,核心执行循环可以进一步简化为持续迭代的三步:推理 → 行动 → 观察。

智能体循环的核心迭代
下面是这一架构的简单可视化示意:

详细的智能体循环架构
设想给一个智能体下达这样的指令:“找出 2026 年发表的、关于智能体记忆的被引用次数最多的论文,并对其进行总结。”
循环的执行过程大致如下:
在底层,整个架构归结为几行逻辑的Python 伪代码:
while not done:
response = call_llm(messages)
if response has tool_calls:
results = execute_tools(response.tool_calls)
messages.append(results)
else:
done = True
return response下面是用 Python、LangChain 和一个 Oracle 数据库连接来实现智能体循环的基础示例,其中 Oracle 数据库连接被用于工具执行。
from langchain.agents import create_agent
from langchain_core.tools import tool
from langchain_core.messages import AIMessage, ToolMessage
# 1. 定义智能体可以执行(ACT)的工具
@tool
def calculate(expression: str) -> str:
"""对一个数学表达式求值。"""
pass # 此处实现
@tool
def timezone_convert(time_str: str, from_city: str, to_city: str) -> str:
"""在城市之间转换本地时间。"""
pass # 此处实现
# 2. 创建带工具的智能体(这会编译出 StateGraph 循环)
agent = create_agent(
model=llm,
tools=[calculate, timezone_convert],
system_prompt="You are a precise assistant. Use tools to find answers."
)
# 3. 运行迭代循环
QUESTION = "If I fly from London at 14:00 for 6 hours, what time do I land in New York?"
for chunk in agent.stream({"messages": [("human", QUESTION)]}, stream_mode="values"):
last_msg = chunk["messages"][-1]
if isinstance(last_msg, AIMessage) and last_msg.tool_calls:
for call in last_msg.tool_calls:
print(f"[ACT] → Executing {call['name']}")
elif isinstance(last_msg, ToolMessage):
print(f"[OBSERVE] ← Result received")
elif isinstance(last_msg, AIMessage) and last_msg.content:
print(f"\\nFinal Answer: {last_msg.content}")适合使用的场景:
不适合使用的场景:
作者:Pranav Bagal
本文分享自 DeepHub IMBA 微信公众号,前往查看
如有侵权,请联系 cloudcommunity@tencent.com 删除。
本文参与 腾讯云自媒体同步曝光计划 ,欢迎热爱写作的你一起参与!