推理循环
一句话总结agent 的”主循环”:反复执行”模型决策 → 执行动作 → 观察结果 → 更新状态”,直到完成或停止。它是承载 agent-loop-paradigms(ReAct / plan-execute / graph…)的执行骨架,也是其他所有组件的挂载点。
它解决什么问题
让模型能多步自主行动,而不是一问一答。循环决定了:谁来决定下一步(模型 or 代码)、何时停止、出错怎么办、上下文如何在步间流动。
设计维度 / 实现谱系
- 控制权:模型驱动(ReAct/CodeAct)↔ 框架驱动(状态机/图)
- 停止条件:模型自报完成 / 最大步数 / 工具返回终态 / 外部中断
- 错误处理:异常回灌给模型自我修正 vs 框架捕获重试 vs 直接失败
- 循环形态:while 循环(命令式)vs 图遍历(声明式)vs 事件驱动(流式/语音)
- 步间状态:消息列表追加 vs 显式 state 对象 vs 黑板
关键要点
- 循环是 agent 的”心跳”;范式只是循环的不同组织。
- 流式/语音 agent(Pipecat)的循环是事件驱动的,与请求-响应式不同。
- 图式循环(LangGraph/Mastra)天然支持断点续跑,见 state-persistence。
关联
各框架实现对比
下表汇总 44 个实现了「推理循环」的框架(源码级阅读结论)。网站上以可展开 + 源码节选呈现。
| 框架 | 实现方式 |
|---|---|
| AG2 | 非固定 ReAct:每个 Agent 持有有序 reply-func 列表,generate_reply() 依次尝试,首个 final=True 即返回。默认顺序(后注册先执行/LIFO insert):终止&人类输入 → 工具调用 → 代码执行 → LLM(oai) 回复。多轮由 send/receive 互发消息驱动 |
| Agency Swarm | 不自实现;完全委托给 OpenAI Agents SDK 的 Runner.run / run_streamed(ReAct 式 tool-calling loop,跑在 Responses API 上)。框架只在外层包一层 setup→run→保存 |
| Agent-LLM (AGiXT) | 自定义 XML 标签 ReAct+反思:LLM 输出 |
| AgentDock | 非显式 ReAct 循环;委托给 Vercel AI SDK streamText 的 multi-step tool calling:AgentNode.handleMessage 备好工具与 prompt 后调 LLMOrchestrationService.streamWithOrchestration→CoreLLM.streamText,由 SDK 在 maxSteps(默认5) 内自动跑”LLM→tool→回灌→再 LLM”。每个 step 经 onStepFinish 回调追踪已用工具 |
| AgentField | 框架本身不强制 ReAct 内循环;单次推理是 app.ai() 一发 LLM 调用。当传 tools= 时进入 discover→call 工具循环(execute_tool_call_loop,默认 max_tool_calls=25);更”自治”的多轮循环交给外部 harness(Claude Code/Codex 等)。控制平面则把多步建成 workflow DAG |
| Agentic Context Engine (ACE) | 非传统 ReAct;核心是”执行→评估→反思→策展”学习闭环,组合为不可变 Pipeline 顺序执行。Agent 角色本身是一次结构化 LLM 调用(run_sync);RR/SkillManager 是带递归+预算的 agentic 循环(RecursiveAgent) |
| AgentScope | 纯异步 ReAct:while cur_iter < max_iters(默认20) 循环,每轮 _check_next_action 判定 reasoning/acting/exit→_reasoning 调模型生成 text/thinking/tool_call→_batch_tool_calls 把工具分 sequential/concurrent 批执行→结果回灌;无 tool_call 即产出最终 Msg 退出 |
| Agentset | agentic RAG 循环(非 ReAct):for i in maxEvals → generateQueries(LLM 产出 keyword/semantic 查询) → 并行查库 → evaluateQueries 让 LLM 判 canAnswer → 可答或超 tokenBudget 则停,再用聚合 chunks 流式作答 |
| AgentVerse | 非 ReAct 单 agent 循环,而是环境回合制:simulation 每个 step() 走 order→describer→agents 并发 astep→selector→updater→visibility(basic.py:57);task-solving 每轮走 role_assign→decision_making→execute→evaluate(basic.py:45)。单 agent 内 ConversationAgent.astep 只是”填模板→LLM→parse→出 Message”带 max_retry 重试,无自循环 |
| Ailoy | ReAct 式 loop:流式调 LM 累积 delta → 若 assistant 消息含 tool_calls 则逐个执行并把结果作为 Role::Tool 消息回灌 → 否则 break;提供 run(聚合) 与 run_delta(流式) 两个入口 |
| Astron Agent | ReAct 式 CoT:非原生 function-calling,而是 prompt 约定 Thought/Action/Action Input/Observation/Final Answer 文本格式,while max_loop>loop_count 循环:流式读 LLM→字符串切分解析出 action→执行 plugin→把 Observation 写回 scratchpad→重灌再问 |
| AutoGen | 双层:①底层是 actor 事件循环(runtime 投递消息→@message_handler 分发);②AssistantAgent 提供 ReAct 式工具循环:LLM→若返回 FunctionCall 则执行→结果回灌→再次推理,受 max_tool_iterations(默认 1)约束;末轮可 reflect_on_tool_use 再推理或直接 summarize |
| Botpress | code-first 而非 ReAct-JSON:while(true) 循环,每轮让 LLM 生成 TS 代码→编译→沙箱执行;命中 Exit 则成功返回,thinking/error/invalid-code 则 continue 重试,超 loop 上限抛 LoopExceededError。Chat 模式下 ListenExit=让位用户 |
| ConnectOnion | ReAct 式 while 循环:LLM→若有 tool_calls 则执行→把结果回灌→重复,直到无 tool_call 或达 max_iterations(默认 100);可经 stop_signal 中途让位用户 |
| CrewAI | 双模 agent loop:LLM 支持 native function calling 则走结构化 tool_calls 回灌循环,否则回退 ReAct 文本模式(解析 Action/Action Input);均循环至 AgentFinish 或 max_iter |
| Dust | step 式 multi-actions 循环(非经典 ReAct 文本):Temporal 工作流逐步执行”调模型选动作 → 执行工具 → 回灌”,每步一次 LLM 调用,最多 MAX_STEPS_USE_PER_RUN_LIMIT 步;core 侧另有块式 App 顺序执行引擎 |
| Haystack | 两层:①Pipeline 层=声明式 DAG,引擎按拓扑序+优先级队列驱动 component(非 LLM 推理,是数据流编排),支持环/分支;②Agent 层=ReAct 式 while counter < max_agent_steps 循环:ChatGenerator→若 replies 含 tool_call 则 ToolInvoker 执行→回灌 messages→重复,直到无 tool_call 或命中 exit_condition |
| Hermes Agent | ReAct 式 while 循环:调模型→若有 tool_calls 则执行(可并发)→回灌结果→重复,直到无 tool_call 或耗尽 max_iterations(默认 90) / IterationBudget;逐 provider 处理 finish_reason(stop/length/incomplete)、失败 failover、partial-stream 续写 |
| Hive | graph-of-event_loops:唯一节点类型 event_loop 是多轮 streaming LLM 循环(reason→tool→observe→judge),节点内 reflexion 自纠(accept/retry/escalate);Orchestrator 沿边遍历图直到终止或耗尽 max_steps |
| Lagent | 多种范式并存:基础 Agent 是单次 LLM 调用;ReAct 用 for _ in range(max_turn) 循环 select_agent→检查 finish_condition→执行 actions(默认 max_turn=5);AgentForInternLM/MathCoder 是 InternLM 原生工具循环;FunctionCallAgent 是 select/env 双 agent 的 while 循环 |
| LangChain | 经典 ReAct 式工具循环,但物化为 LangGraph 状态图:model 节点调 LLM→条件边 _make_model_to_tools_edge 看末条 AIMessage 有无 tool_calls,有则 Send(“tools”, …) 执行、回灌、循环,无则走 exit;递归上限 9999 |
| Llama Agentic System (llama-stack-apps) | 服务端 Agentic Loop:一个 Turn 内 Executor 串 Shield→Inference→(Tool+Inference 循环)→Shield→输出;客户端经 create_turn 触发,AgentEventLogger 流式回放 inference/shield/tool_execution 步骤。另有 ReActAgent 提供 ReAct 范式(JSON schema 约束输出) |
| LlamaIndex | Workflow 事件状态机:init_run→setup_agent→run_agent_step(调 take_step)→parse_agent_output→call_tool→aggregate_tool_results→回 AgentInput 循环。FunctionAgent 用原生 function calling,ReActAgent 用 Thought/Action/Observation 文本协议解析 |
| llm-agents | 经典文本式 ReAct:run() 里 while num_loops < max_loops(默认15) 循环——把累积的 previous_responses 填回 prompt → LLM 生成 Thought+Action → 正则解析出工具 → 执行 → 把 Observation: |
| LoongFlow | 两套范式:① PES 进化循环——并发多 cycle,每 cycle 串行 Plan→Execute→Summary,按 target_score / max_iterations 收敛;② ReAct 循环——while(step<max_steps) 做 Reason→Act→(Finalize 检查)→Observe,达 finalizer 工具或步数耗尽退出 |
| Maestro | 非 ReAct;是 supervisor 式 orchestrate-execute-refine 循环:while True 反复调 orchestrator 拆出”下一个子任务”→交 sub-agent 执行→结果回灌,直到 orchestrator 输出含 “The task is complete:” 才 break;无工具调用环节 |
| Mastra | 自主工具循环,但用 workflow 引擎实现:一次运行编译成 agentic-loop,靠 .dowhile(agenticExecution, …) 反复执行 LLM 调用→工具调用步骤,直到 stepResult.isContinued 为 false;停止条件由 stopWhen(StopCondition 数组,如 step 计数)与 maxSteps 控制,每轮触发 onIterationComplete 钩子可注入反馈/继续/中止 |
| MetaGPT | 经典 Role 是 _observe→_think→_act 状态机循环:react() 按 react_mode 分派;REACT 模式下 _think 用 LLM 选下一个 Action(STATE_TEMPLATE 让 LLM 回答状态编号),_react 在 max_react_loop 内 think-act 交替。新 RoleZero 是工具化 ReAct:LLM 直接输出命令列表→解析→执行→回灌 |
| Modus | 框架不内置 ReAct/计划循环;范式是”函数即端点 + actor 化 agent 的消息处理”。Agent 靠 OnReceiveMessage(msgName, data) 的 switch 分发处理消息(类 actor 收信),多步推理需用户在函数内自行编排 model+tool 调用 |
| nanobot | single-agent 工具循环:turn 建模为显式状态机(RESTORE→…→RESPOND→DONE),内层 AgentRunner._run_core 按 max_iterations(默认见 AgentDefaults)迭代:请求模型→若 should_execute_tools 则执行工具回灌→否则收敛为最终回复;含空回复/截断/注入恢复 |
| Open Multi-Agent | worker 层为 ReAct 式 while(true):LLM→提取 tool_use→并行执行工具→回灌 tool_result→循环,直到无 tool_call 或达 maxTurns(默认 10);团队层为 plan-execute:coordinator 一次拆解 → 队列分轮并行执行 → coordinator 合成 |
| OpenClaw | ReAct 式双层 while 循环:内层 LLM→若有 toolCall 则执行(顺序/并行)→结果回灌→重复;外层处理 steering(运行中插话)与 follow-up 消息;stopReason/shouldStopAfterTurn/terminate 决定终止 |
| Pipecat | 非 ReAct;范式是 frame-based 流式管道:Frame 沿 processor 链单向流动,每个 FrameProcessor.process_frame() 处理后 push_frame() 给下游;推理本身委托给 LLM service(function-calling 多轮由 run_function_calls 把结果回灌进 LLMContext 再触发下一轮 inference) |
| PraisonAI | 函数调用式 ReAct:_chat_completion 取 LLM 响应→若有 tool_calls 则执行并回灌→无 tool_call 后进入自我反思 while 循环(先答、再让 LLM 输出 {reflection, satisfactory} JSON、不满意则”按反思重写”再循环);满足 min_reflect 且 satisfactory=yes,或达 max_reflect 才返回 |
| Semantic Kernel | 非显式 ReAct;核心是 native function-calling 自动循环:模型返回 FunctionCallContent→FunctionCallsProcessor 查表执行→结果回灌 ChatHistory→再次请求模型,直到无工具调用或达上限。Agent 层把每轮新增的 tool/assistant 消息回写线程 |
| smolagents | ReAct 多步循环:while not final and step<=max_steps,每步 think→act→observe;CodeAct 变体下 act=执行 Python 代码。子类只实现 _step_stream |
| Strands Agents | model-driven 自驱循环:调模型→stop_reason==“tool_use” 则执行工具→结果回灌→递归重开,无 planner/状态机,全靠模型决策;max_tokens 默认硬失败 |
| Swarm | ReAct 式工具循环;while 直到无 tool_call 或 max_turns |
| SwarmClaw | LangGraph createReactAgent + MemorySaver 跑单回合 ReAct(streamEvents v2 流式),外层自研 for 迭代循环做”续跑/早停/工具频控/idle watchdog”,受 recursionLimit 约束 |
| Swarms | ReAct 式 while 循环:call_llm→parse_llm_output→若有 tool_calls 则 execute_tools 回灌→重复,直到无 tool_call 或 loop_count >= max_loops;max_loops=“auto” 时无上限直到自决完成 |
| Transformers Agents | ReAct(ReactCodeAgent / ReactJsonAgent);早期为单步 plan-then-run |
| Upsonic | 非单一 while 循环,而是 24 步显式 pipeline;LLM↔工具的迭代发生在 model-execution 步内(CallManager 驱动 process_response,达 tool_call_limit(默认100) 停止);带 streaming 平行管线 |
| vectara-agentic | 不自研循环,复用 LlamaIndex 的 workflow agent:AgentType.FUNCTION_CALLING(原生 function calling,默认)或 REACT(Thought/Action/Observation 文本协议)。achat() 经 current_agent.run(user_msg, memory, ctx) 驱动 workflow,循环由 LlamaIndex FunctionAgent/ReActAgent 内部完成 |
| VoltAgent | 不自造循环:委托 Vercel AI SDK 的多步工具循环,stopWhen: stepCountIs(maxSteps) 控制步数(默认 maxSteps=5,启用 workspace 时=100);每步 onStepFinish 回灌并写 trace。提供 generateText/streamText/generateObject/streamObject 四种入口 |
各框架实现对比 · 源码级
AG2 python 非固定 ReAct:每个 Agent 持有有序 reply-func 列表,generate_reply() 依次尝试,首个 final=True 即返回。默认顺序(后注册先执行/LIFO insert):终止&人类输入 → 工具调用 → 代码执行 → LLM(oai) 回复。多轮由 send/receive 互发消息驱动
非固定 ReAct:每个 Agent 持有有序 reply-func 列表,generate_reply() 依次尝试,首个 final=True 即返回。默认顺序(后注册先执行/LIFO insert):终止&人类输入 → 工具调用 → 代码执行 → LLM(oai) 回复。多轮由 send/receive 互发消息驱动
agentchat/conversable_agent.py:3172
return False, None
def generate_reply(
self,
messages: list[dict[str, Any]] | None = None,
sender: Optional["Agent"] = None,
exclude: Container[Any] = (),
) -> str | dict[str, Any] | None:
"""Reply based on the conversation history and the sender.
Either messages or sender must be provided.
Register a reply_func with `None` as one trigger for it to be activated when `messages` is non-empty and `sender` is `None`. Agency Swarm python 不自实现;完全委托给 OpenAI Agents SDK 的 Runner.run / run_streamed(ReAct 式 tool-calling loop,跑在 Responses API 上)。框架只在外层包一层 setup→run→保存
不自实现;完全委托给 OpenAI Agents SDK 的 Runner.run / run_streamed(ReAct 式 tool-calling loop,跑在 Responses API 上)。框架只在外层包一层 setup→run→保存
agent/execution_helpers.py:72agent/execution_streaming.py:133agent/execution.py:69 logger.warning(f"Entering async context for server {server.name}")
await mcp_stack.enter_async_context(server) # type: ignore[arg-type]
result = await Runner.run(
starting_agent=agent,
input=history_for_runner,
context=master_context_for_run,
hooks=hooks_override,
run_config=with_codex_model_input_role_rewrite(run_config_override or RunConfig()),
max_turns=kwargs.get("max_turns", 1000000),
)
return result Agent-LLM (AGiXT) python 自定义 XML 标签 ReAct+反思:LLM 输出 <thinking>/<reflection>/<execute><name>…</name></execute>/<answer>,平台正则解析 <execute> 块→执行→把 <output> 回灌再次推理,直到出现完整 <answer>;非纯原生 function-calling
自定义 XML 标签 ReAct+反思:LLM 输出 <thinking>/<reflection>/<execute><name>…</name></execute>/<answer>,平台正则解析 <execute> 块→执行→把 <output> 回灌再次推理,直到出现完整 <answer>;非纯原生 function-calling
Interactions.py:7058Interactions.py:7199Interactions.py:3247Interactions.py:64 yield {"type": "answer", "content": final_answer, "complete": True}
def extract_commands_from_response(self, response):
# Extract all <execute>...</execute> blocks
command_blocks = re.findall(r"(<execute>.*?</execute>)", response, re.DOTALL)
extracted_commands = []
for command_block in command_blocks:
# Extract the content inside <execute>...</execute>
command_content = re.search(
r"<execute>(.*?)</execute>", command_block, re.DOTALL
).group(1)
# Extract the command name
name_match = re.search(r"<name>(.*?)</name>", command_content, re.DOTALL) AgentDock typescript 非显式 ReAct 循环;委托给 Vercel AI SDK streamText 的 multi-step tool calling:AgentNode.handleMessage 备好工具与 prompt 后调 LLMOrchestrationService.streamWithOrchestration→CoreLLM.streamText,由 SDK 在 maxSteps(默认5) 内自动跑"LLM→tool→回灌→再 LLM"。每个 step 经 onStepFinish 回调追踪已用工具
非显式 ReAct 循环;委托给 Vercel AI SDK streamText 的 multi-step tool calling:AgentNode.handleMessage 备好工具与 prompt 后调 LLMOrchestrationService.streamWithOrchestration→CoreLLM.streamText,由 SDK 在 maxSteps(默认5) 内自动跑"LLM→tool→回灌→再 LLM"。每个 step 经 onStepFinish 回调追踪已用工具
nodes/agent-node.ts:414llm/llm-orchestration-service.ts:157 * @returns A Promise resolving to the `StreamTextResult` object from the Vercel AI SDK,
* containing the live stream and usage promise for the caller to handle.
*/
async handleMessage(
options: AgentNodeHandleMessageOptions
): Promise<AgentDockStreamResult<Record<string, CoreTool>, any>> {
const {
messages,
sessionId,
orchestrationManager,
systemOverride,
useFallback = false,
config: runtimeOverrides, AgentField go 框架本身不强制 ReAct 内循环;单次推理是 app.ai() 一发 LLM 调用。当传 tools= 时进入 discover→call 工具循环(execute_tool_call_loop,默认 max_tool_calls=25);更"自治"的多轮循环交给外部 harness(Claude Code/Codex 等)。控制平面则把多步建成 workflow DAG
框架本身不强制 ReAct 内循环;单次推理是 app.ai() 一发 LLM 调用。当传 tools= 时进入 discover→call 工具循环(execute_tool_call_loop,默认 max_tool_calls=25);更"自治"的多轮循环交给外部 harness(Claude Code/Codex 等)。控制平面则把多步建成 workflow DAG
sdk/python/agentfield/tool_calling.py:394tool_calling.py:48agent_ai.py:257 )
async def execute_tool_call_loop(
agent: "Agent",
messages: List[Dict[str, Any]],
tools: List[Dict[str, Any]],
config: ToolCallConfig,
needs_lazy_hydration: bool,
litellm_params: Dict[str, Any],
make_completion: Callable,
) -> tuple[Any, ToolCallTrace]:
"""Execute the LLM tool-call loop. Agentic Context Engine (ACE) python 非传统 ReAct;核心是"执行→评估→反思→策展"学习闭环,组合为不可变 Pipeline 顺序执行。Agent 角色本身是一次结构化 LLM 调用(run_sync);RR/SkillManager 是带递归+预算的 agentic 循环(RecursiveAgent)
非传统 ReAct;核心是"执行→评估→反思→策展"学习闭环,组合为不可变 Pipeline 顺序执行。Agent 角色本身是一次结构化 LLM 调用(run_sync);RR/SkillManager 是带递归+预算的 agentic 循环(RecursiveAgent)
pipeline/pipeline.py:53ace/runners/ace.py:27ace/implementations/agent.py:71# ---------------------------------------------------------------------------
class Pipeline:
"""Ordered sequence of steps. Satisfies StepProtocol — can be nested.
Build via the fluent API::
pipe = (
Pipeline()
.then(AgentStep())
.then(EvaluateStep())
.then(ReflectStep()) # ReflectStep.async_boundary = True AgentScope python 纯异步 ReAct:while cur_iter < max_iters(默认20) 循环,每轮 _check_next_action 判定 reasoning/acting/exit→_reasoning 调模型生成 text/thinking/tool_call→_batch_tool_calls 把工具分 sequential/concurrent 批执行→结果回灌;无 tool_call 即产出最终 Msg 退出
纯异步 ReAct:while cur_iter < max_iters(默认20) 循环,每轮 _check_next_action 判定 reasoning/acting/exit→_reasoning 调模型生成 text/thinking/tool_call→_batch_tool_calls 把工具分 sequential/concurrent 批执行→结果回灌;无 tool_call 即产出最终 Msg 退出
agent/_agent.py:595agent/_agent.py:687agent/_agent.py:628 # Step 3: Enter the reasoning-acting loop until reaching max_iters or
# no more tool calls to execute
# ===================================================================
while self.state.cur_iter < self.react_config.max_iters:
# ===============================================================
# Step 3.1:
# ===============================================================
action, data = self._check_next_action()
if action == "exit" and isinstance(data, Msg):
yield data
return
# =============================================================== Agentset typescript agentic RAG 循环(非 ReAct):for i in maxEvals → generateQueries(LLM 产出 keyword/semantic 查询) → 并行查库 → evaluateQueries 让 LLM 判 canAnswer → 可答或超 tokenBudget 则停,再用聚合 chunks 流式作答
agentic RAG 循环(非 ReAct):for i in maxEvals → generateQueries(LLM 产出 keyword/semantic 查询) → 并行查库 → evaluateQueries 让 LLM 判 canAnswer → 可答或超 tokenBudget 则停,再用聚合 chunks 流式作答
apps/web/src/lib/agentic/search.ts:35agentic/utils.ts:30agentic/index.ts:29
const lastMessage = messages[messages.length - 1]!.content as string;
for (let i = 0; i < maxEvals; i++) {
const { queries: newQueries, totalTokens: queriesTokens } =
await generateQueries(model, messages, queries);
newQueries.forEach((q) => {
if (queries.some((q2) => q2.query === q.query)) return;
queries.push(q);
});
totalTokens += queriesTokens; AgentVerse python 非 ReAct 单 agent 循环,而是环境回合制:simulation 每个 step() 走 order→describer→agents 并发 astep→selector→updater→visibility(basic.py:57);task-solving 每轮走 role_assign→decision_making→execute→evaluate(basic.py:45)。单 agent 内 ConversationAgent.astep 只是"填模板→LLM→parse→出 Message"带 max_retry 重试,无自循环
非 ReAct 单 agent 循环,而是环境回合制:simulation 每个 step() 走 order→describer→agents 并发 astep→selector→updater→visibility(basic.py:57);task-solving 每轮走 role_assign→decision_making→execute→evaluate(basic.py:45)。单 agent 内 ConversationAgent.astep 只是"填模板→LLM→parse→出 Message"带 max_retry 重试,无自循环
environments/simulation_env/basic.py:57environments/tasksolving_env/basic.py:45agents/simulation_agent/conversation.py:50 )
super().__init__(rule=rule, **kwargs)
async def step(self) -> List[Message]:
"""Run one step of the environment"""
# Get the next agent index
agent_ids = self.rule.get_next_agent_idx(self)
# Generate current environment description
env_descriptions = self.rule.get_env_description(self)
# Generate the next message Ailoy rust ReAct 式 loop:流式调 LM 累积 delta → 若 assistant 消息含 tool_calls 则逐个执行并把结果作为 Role::Tool 消息回灌 → 否则 break;提供 run(聚合) 与 run_delta(流式) 两个入口
ReAct 式 loop:流式调 LM 累积 delta → 若 assistant 消息含 tool_calls 则逐个执行并把结果作为 Role::Tool 消息回灌 → 否则 break;提供 run(聚合) 与 run_delta(流式) 两个入口
src/agent/base.rs:224src/agent/base.rs:206src/agent/base.rs:177 knowledge_config.unwrap_or_default()
).await?;
let tool_descs = Self::get_tool_descs(&tools);
loop {
let mut assistant_msg_delta = MessageDelta::new().with_role(Role::Assistant);
{
let mut model = self.lm.clone();
let mut strm = model.infer_delta(
messages.clone(),
tool_descs.clone(),
docs.clone(),
inference_config.clone().unwrap_or_default()
); Astron Agent python ReAct 式 CoT:非原生 function-calling,而是 prompt 约定 Thought/Action/Action Input/Observation/Final Answer 文本格式,while max_loop>loop_count 循环:流式读 LLM→字符串切分解析出 action→执行 plugin→把 Observation 写回 scratchpad→重灌再问
ReAct 式 CoT:非原生 function-calling,而是 prompt 约定 Thought/Action/Action Input/Observation/Final Answer 文本格式,while max_loop>loop_count 循环:流式读 LLM→字符串切分解析出 action→执行 plugin→把 Observation 写回 scratchpad→重灌再问
engine/nodes/cot/cot_runner.py:305engine/nodes/cot/cot_prompt.py:1 cot_step.action_output = plugin_response.result
yield AgentResponse(typ="cot_step", content=cot_step, model=self.model.name)
async def run(
self, span: Span, node_trace_log: NodeTraceLog
) -> AsyncIterator[AgentResponse]:
"""cot run"""
with span.start("RunCotAgent") as sp:
system_prompt = await self.create_system_prompt()
user_prompt_template = await self.create_user_prompt() AutoGen python 双层:①底层是 actor 事件循环(runtime 投递消息→@message_handler 分发);②AssistantAgent 提供 ReAct 式工具循环:LLM→若返回 FunctionCall 则执行→结果回灌→再次推理,受 max_tool_iterations(默认 1)约束;末轮可 reflect_on_tool_use 再推理或直接 summarize
双层:①底层是 actor 事件循环(runtime 投递消息→@message_handler 分发);②AssistantAgent 提供 ReAct 式工具循环:LLM→若返回 FunctionCall 则执行→结果回灌→再次推理,受 max_tool_iterations(默认 1)约束;末轮可 reflect_on_tool_use 再推理或直接 summarize
_assistant_agent.py:1118_assistant_agent.py:1056 yield model_result
@classmethod
async def _process_model_result(
cls,
model_result: CreateResult,
inner_messages: List[BaseAgentEvent | BaseChatMessage],
cancellation_token: CancellationToken,
agent_name: str,
system_messages: List[SystemMessage],
model_context: ChatCompletionContext,
workbench: Sequence[Workbench],
handoff_tools: List[BaseTool[Any, Any]], Botpress typescript code-first 而非 ReAct-JSON:while(true) 循环,每轮让 LLM 生成 TS 代码→编译→沙箱执行;命中 Exit 则成功返回,thinking/error/invalid-code 则 continue 重试,超 loop 上限抛 LoopExceededError。Chat 模式下 ListenExit=让位用户
code-first 而非 ReAct-JSON:while(true) 循环,每轮让 LLM 生成 TS 代码→编译→沙箱执行;命中 Exit 则成功返回,thinking/error/invalid-code 则 continue 重试,超 loop 上限抛 LoopExceededError。Chat 模式下 ListenExit=让位用户
packages/llmz/src/llmz.ts:302packages/llmz/src/llmz.ts:416CLAUDE.md:31 })
try {
while (true) {
if (ctx.iterations.length >= ctx.loop) {
return new ErrorExecutionResult(ctx, new LoopExceededError())
}
const iteration = await ctx.nextIteration()
try {
await executeOnIterationStartHook({
iteration, ConnectOnion python ReAct 式 while 循环:LLM→若有 tool_calls 则执行→把结果回灌→重复,直到无 tool_call 或达 max_iterations(默认 100);可经 stop_signal 中途让位用户
ReAct 式 while 循环:LLM→若有 tool_calls 则执行→把结果回灌→重复,直到无 tool_call 或达 max_iterations(默认 100);可经 stop_signal 中途让位用户
core/agent.py:416core/agent.py:446 {"role": "user", "content": prompt}
]
def _run_iteration_loop(self, max_iterations: int) -> str:
"""Run the main LLM/tool iteration loop until complete or max iterations."""
while self.current_session['iteration'] < max_iterations:
self.current_session['iteration'] += 1
# Fire before_iteration (poll IO, check mode changes)
self._invoke_events('before_iteration')
# Get LLM response
response = self._get_llm_decision() CrewAI python 双模 agent loop:LLM 支持 native function calling 则走结构化 tool_calls 回灌循环,否则回退 ReAct 文本模式(解析 Action/Action Input);均循环至 AgentFinish 或 max_iter
双模 agent loop:LLM 支持 native function calling 则走结构化 tool_calls 回灌循环,否则回退 ReAct 文本模式(解析 Action/Action Input);均循环至 AgentFinish 或 max_iter
crewai/agents/crew_agent_executor.py:306 msg["files"] = files
break
def _invoke_loop(self) -> AgentFinish:
"""Execute agent loop until completion.
Checks if the LLM supports native function calling and uses that
approach if available, otherwise falls back to the ReAct text pattern.
Returns:
Final answer from the agent.
"""
use_native_tools = ( Dust typescript step 式 multi-actions 循环(非经典 ReAct 文本):Temporal 工作流逐步执行"调模型选动作 → 执行工具 → 回灌",每步一次 LLM 调用,最多 MAX_STEPS_USE_PER_RUN_LIMIT 步;core 侧另有块式 App 顺序执行引擎
step 式 multi-actions 循环(非经典 ReAct 文本):Temporal 工作流逐步执行"调模型选动作 → 执行工具 → 回灌",每步一次 LLM 调用,最多 MAX_STEPS_USE_PER_RUN_LIMIT 步;core 侧另有块式 App 顺序执行引擎
front/temporal/agent_loop/workflows.ts:185front/temporal/agent_loop/lib/run_model.ts:117core/src/app.rs:305 }
}
export async function agentLoopWorkflow({
authType,
initialStartTime,
agentLoopArgs,
startStep,
}: {
authType: AuthenticatorType;
initialStartTime: number;
agentLoopArgs: AgentLoopArgs;
startStep: number; Haystack python 两层:①Pipeline 层=声明式 DAG,引擎按拓扑序+优先级队列驱动 component(非 LLM 推理,是数据流编排),支持环/分支;②Agent 层=ReAct 式 while counter < max_agent_steps 循环:ChatGenerator→若 replies 含 tool_call 则 ToolInvoker 执行→回灌 messages→重复,直到无 tool_call 或命中 exit_condition
两层:①Pipeline 层=声明式 DAG,引擎按拓扑序+优先级队列驱动 component(非 LLM 推理,是数据流编排),支持环/分支;②Agent 层=ReAct 式 while counter < max_agent_steps 循环:ChatGenerator→若 replies 含 tool_call 则 ToolInvoker 执行→回灌 messages→重复,直到无 tool_call 或命中 exit_condition
core/pipeline/pipeline.py:114core/pipeline/base.py:1289components/agents/agent.py:809agent.py:861
return component_output
def run( # noqa: PLR0915, PLR0912, C901
self,
data: dict[str, Any],
include_outputs_from: set[str] | None = None,
*,
break_point: Breakpoint | AgentBreakpoint | None = None,
pipeline_snapshot: PipelineSnapshot | None = None,
snapshot_callback: SnapshotCallback | None = None,
) -> dict[str, Any]:
""" Hermes Agent python ReAct 式 while 循环:调模型→若有 tool_calls 则执行(可并发)→回灌结果→重复,直到无 tool_call 或耗尽 max_iterations(默认 90) / IterationBudget;逐 provider 处理 finish_reason(stop/length/incomplete)、失败 failover、partial-stream 续写
ReAct 式 while 循环:调模型→若有 tool_calls 则执行(可并发)→回灌结果→重复,直到无 tool_call 或耗尽 max_iterations(默认 90) / IterationBudget;逐 provider 处理 finish_reason(stop/length/incomplete)、失败 failover、partial-stream 续写
agent/conversation_loop.py:801agent/conversation_loop.py:351run_agent.py:319 should_review_memory=_should_review_memory,
)
while (api_call_count < agent.max_iterations and agent.iteration_budget.remaining > 0) or agent._budget_grace_call:
# Reset per-turn checkpoint dedup so each iteration can take one snapshot
agent._checkpoint_mgr.new_turn()
# Check for interrupt request (e.g., user sent new message)
if agent._interrupt_requested:
interrupted = True
_turn_exit_reason = "interrupted_by_user"
if not agent.quiet_mode:
agent._safe_print("\n⚡ Breaking out of tool loop due to interrupt...") Hive python graph-of-event_loops:唯一节点类型 event_loop 是多轮 streaming LLM 循环(reason→tool→observe→judge),节点内 reflexion 自纠(accept/retry/escalate);Orchestrator 沿边遍历图直到终止或耗尽 max_steps
graph-of-event_loops:唯一节点类型 event_loop 是多轮 streaming LLM 循环(reason→tool→observe→judge),节点内 reflexion 自纠(accept/retry/escalate);Orchestrator 沿边遍历图直到终止或耗尽 max_steps
agent_loop/agent_loop.py:1orchestrator/orchestrator.py:486"""AgentLoop: Multi-turn LLM streaming loop with tool execution and judge evaluation.
Implements AgentProtocol and runs a streaming event loop:
1. Calls LLMProvider.stream() to get streaming events
2. Processes text deltas, tool calls, and finish events
3. Executes tools and feeds results back to the conversation
4. Uses judge evaluation (or implicit stop-reason) to decide loop termination
5. Publishes lifecycle events to EventBus
6. Persists conversation and outputs via write-through to ConversationStore
""" Lagent python 多种范式并存:基础 Agent 是单次 LLM 调用;ReAct 用 for _ in range(max_turn) 循环 select_agent→检查 finish_condition→执行 actions(默认 max_turn=5);AgentForInternLM/MathCoder 是 InternLM 原生工具循环;FunctionCallAgent 是 select/env 双 agent 的 while 循环
多种范式并存:基础 Agent 是单次 LLM 调用;ReAct 用 for _ in range(max_turn) 循环 select_agent→检查 finish_condition→执行 actions(默认 max_turn=5);AgentForInternLM/MathCoder 是 InternLM 原生工具循环;FunctionCallAgent 是 select/env 双 agent 的 while 循环
agents/react.py:59agents/stream.py:100agents/fc_agent.py:78 )
super().__init__(**kwargs)
def forward(self, message: AgentMessage, session_id=0, **kwargs) -> AgentMessage:
for _ in range(self.max_turn):
message = self.select_agent(message, session_id=session_id, **kwargs)
if self.finish_condition(message):
return message
message = self.actions(message, session_id=session_id)
return message
class AsyncReAct(AsyncAgent): LangChain python 经典 ReAct 式工具循环,但物化为 LangGraph 状态图:model 节点调 LLM→条件边 _make_model_to_tools_edge 看末条 AIMessage 有无 tool_calls,有则 Send("tools", ...) 执行、回灌、循环,无则走 exit;递归上限 9999
经典 ReAct 式工具循环,但物化为 LangGraph 状态图:model 节点调 LLM→条件边 _make_model_to_tools_edge 看末条 AIMessage 有无 tool_calls,有则 Send("tools", ...) 执行、回灌、循环,无则走 exit;递归上限 9999
agents/factory.py:1317factory.py:1724factory.py:1805factory.py:1662 structured_response=structured_response,
)
def model_node(state: AgentState[Any], runtime: Runtime[ContextT]) -> list[Command[Any]]:
"""Sync model request handler with sequential middleware processing."""
request = ModelRequest(
model=model,
tools=default_tools,
system_message=system_message,
response_format=initial_response_format,
messages=state["messages"],
tool_choice=None,
state=state, Llama Agentic System (llama-stack-apps) python 服务端 Agentic Loop:一个 Turn 内 Executor 串 Shield→Inference→(Tool+Inference 循环)→Shield→输出;客户端经 create_turn 触发,AgentEventLogger 流式回放 inference/shield/tool_execution 步骤。另有 ReActAgent 提供 ReAct 范式(JSON schema 约束输出)
服务端 Agentic Loop:一个 Turn 内 Executor 串 Shield→Inference→(Tool+Inference 循环)→Shield→输出;客户端经 create_turn 触发,AgentEventLogger 流式回放 inference/shield/tool_execution 步骤。另有 ReActAgent 提供 ReAct 范式(JSON schema 约束输出)
docs/sequence-diagram.md:1examples/agents/agent_with_tools.py:70examples/agents/react_agent.py:55The sequence diagram with more details of the communication between components is below:
```mermaid
sequenceDiagram
participant U as User
participant S as Shields
participant E as Executor
participant L as LLM LlamaIndex python Workflow 事件状态机:init_run→setup_agent→run_agent_step(调 take_step)→parse_agent_output→call_tool→aggregate_tool_results→回 AgentInput 循环。FunctionAgent 用原生 function calling,ReActAgent 用 Thought/Action/Observation 文本协议解析
Workflow 事件状态机:init_run→setup_agent→run_agent_step(调 take_step)→parse_agent_output→call_tool→aggregate_tool_results→回 AgentInput 循环。FunctionAgent 用原生 function calling,ReActAgent 用 Thought/Action/Observation 文本协议解析
agent/workflow/base_agent.py:383agent/workflow/function_agent.py:101agent/workflow/react_agent.py:121
return tool_output
@step
async def init_run(self, ctx: Context, ev: AgentWorkflowStartEvent) -> AgentInput:
"""Sets up the workflow and validates inputs."""
await self._init_context(ctx, ev)
user_msg: Optional[Union[str, ChatMessage]] = ev.get("user_msg")
chat_history: Optional[List[ChatMessage]] = ev.get("chat_history", [])
# Convert string user_msg to ChatMessage
if isinstance(user_msg, str): llm-agents python 经典文本式 ReAct:run() 里 while num_loops < max_loops(默认15) 循环——把累积的 previous_responses 填回 prompt → LLM 生成 Thought+Action → 正则解析出工具 → 执行 → 把 Observation: <result> 拼回去;命中 Final Answer: 即返回
经典文本式 ReAct:run() 里 while num_loops < max_loops(默认15) 循环——把累积的 previous_responses 填回 prompt → LLM 生成 Thought+Action → 正则解析出工具 → 执行 → 把 Observation: <result> 拼回去;命中 Final Answer: 即返回
agent.py:56agent.py:14agent.py:80 def tool_by_names(self) -> Dict[str, ToolInterface]:
return {tool.name: tool for tool in self.tools}
def run(self, question: str):
previous_responses = []
num_loops = 0
prompt = self.prompt_template.format(
today = datetime.date.today(),
tool_description=self.tool_description,
tool_names=self.tool_names,
question=question,
previous_responses='{previous_responses}'
) LoongFlow python 两套范式:① PES 进化循环——并发多 cycle,每 cycle 串行 Plan→Execute→Summary,按 target_score / max_iterations 收敛;② ReAct 循环——while(step<max_steps) 做 Reason→Act→(Finalize 检查)→Observe,达 finalizer 工具或步数耗尽退出
两套范式:① PES 进化循环——并发多 cycle,每 cycle 串行 Plan→Execute→Summary,按 target_score / max_iterations 收敛;② ReAct 循环——while(step<max_steps) 做 Reason→Act→(Finalize 检查)→Observe,达 finalizer 工具或步数耗尽退出
framework/pes/pes_agent.py:174framework/react/react_agent.py:94 self.logger.info(f"Registering Summary worker: '{name}'")
register_worker(name, SUMMARY, worker_class)
async def _evolution_cycle(self, iteration_id: int) -> None:
"""
Represents a single, complete evolution cycle for a given iteration ID.
"""
trace_id = str(uuid.uuid4().hex[:12])
self.logger.info(
f"Trace ID: {trace_id}, Starting evolution cycle for iteration {iteration_id}."
)
previous_prompt_tokens = self.total_prompt_tokens Maestro python 非 ReAct;是 supervisor 式 orchestrate-execute-refine 循环:while True 反复调 orchestrator 拆出"下一个子任务"→交 sub-agent 执行→结果回灌,直到 orchestrator 输出含 "The task is complete:" 才 break;无工具调用环节
非 ReAct;是 supervisor 式 orchestrate-execute-refine 循环:while True 反复调 orchestrator 拆出"下一个子任务"→交 sub-agent 执行→结果回灌,直到 orchestrator 输出含 "The task is complete:" 才 break;无工具调用环节
maestro.py:226maestro.py:235task_exchanges = []
haiku_tasks = []
while True:
# Call Orchestrator to break down the objective into the next sub-task or provide the final output
previous_results = [result for _, result in task_exchanges]
if not task_exchanges:
# Pass the file content only in the first iteration if available
opus_result, file_content_for_haiku, search_query = opus_orchestrator(objective, file_content, previous_results, use_search)
else:
opus_result, _, search_query = opus_orchestrator(objective, previous_results=previous_results, use_search=use_search)
if "The task is complete:" in opus_result: Mastra typescript 自主工具循环,但用 workflow 引擎实现:一次运行编译成 agentic-loop,靠 .dowhile(agenticExecution, …) 反复执行 LLM 调用→工具调用步骤,直到 stepResult.isContinued 为 false;停止条件由 stopWhen(StopCondition 数组,如 step 计数)与 maxSteps 控制,每轮触发 onIterationComplete 钩子可注入反馈/继续/中止
自主工具循环,但用 workflow 引擎实现:一次运行编译成 agentic-loop,靠 .dowhile(agenticExecution, …) 反复执行 LLM 调用→工具调用步骤,直到 stepResult.isContinued 为 false;停止条件由 stopWhen(StopCondition 数组,如 step 计数)与 maxSteps 控制,每轮触发 onIterationComplete 钩子可注入反馈/继续/中止
loop/loop.ts:11loop/workflows/agentic-loop/index.ts:80import type { LoopOptions, LoopRun, StreamInternal } from './types';
import { workflowLoopStream } from './workflows/stream';
export function loop<Tools extends ToolSet = ToolSet, OUTPUT = undefined>({
resumeContext,
models,
logger,
runId,
idGenerator,
messageList,
includeRawChunks,
modelSettings,
tools, MetaGPT python 经典 Role 是 _observe→_think→_act 状态机循环:react() 按 react_mode 分派;REACT 模式下 _think 用 LLM 选下一个 Action(STATE_TEMPLATE 让 LLM 回答状态编号),_react 在 max_react_loop 内 think-act 交替。新 RoleZero 是工具化 ReAct:LLM 直接输出命令列表→解析→执行→回灌
经典 Role 是 _observe→_think→_act 状态机循环:react() 按 react_mode 分派;REACT 模式下 _think 用 LLM 选下一个 Action(STATE_TEMPLATE 让 LLM 回答状态编号),_react 在 max_react_loop 内 think-act 交替。新 RoleZero 是工具化 ReAct:LLM 直接输出命令列表→解析→执行→回灌
metagpt/roles/role.py:454role.py:340role.py:512metagpt/roles/di/role_zero.py:303 return
self.rc.msg_buffer.push(message)
async def _react(self) -> Message:
"""Think first, then act, until the Role _think it is time to stop and requires no more todo.
This is the standard think-act loop in the ReAct paper, which alternates thinking and acting in task solving, i.e. _think -> _act -> _think -> _act -> ...
Use llm to select actions in _think dynamically
"""
actions_taken = 0
rsp = AIMessage(content="No actions taken yet", cause_by=Action) # will be overwritten after Role _act
while actions_taken < self.rc.max_react_loop:
# think
has_todo = await self._think() Modus go 框架不内置 ReAct/计划循环;范式是"函数即端点 + actor 化 agent 的消息处理"。Agent 靠 OnReceiveMessage(msgName, data) 的 switch 分发处理消息(类 actor 收信),多步推理需用户在函数内自行编排 model+tool 调用
框架不内置 ReAct/计划循环;范式是"函数即端点 + actor 化 agent 的消息处理"。Agent 靠 OnReceiveMessage(msgName, data) 的 switch 分发处理消息(类 actor 收信),多步推理需用户在函数内自行编排 model+tool 调用
sdk/go/pkg/agents/agents.go:258sdk/go/examples/agents/counterAgent.go:103
// OnReceiveMessage is called when the agent receives a message.
// Custom agents may implement this method to handle incoming messages.
OnReceiveMessage(msgName string, data *string) (*string, error)
}
// The AgentBase struct provides default implementations for the Agent interface methods.
// Custom agents can embed this struct to avoid implementing all methods.
type AgentBase struct {
}
func (a *AgentBase) Id() string {
return *activeAgentId nanobot python single-agent 工具循环:turn 建模为显式状态机(RESTORE→…→RESPOND→DONE),内层 AgentRunner._run_core 按 max_iterations(默认见 AgentDefaults)迭代:请求模型→若 should_execute_tools 则执行工具回灌→否则收敛为最终回复;含空回复/截断/注入恢复
single-agent 工具循环:turn 建模为显式状态机(RESTORE→…→RESPOND→DONE),内层 AgentRunner._run_core 按 max_iterations(默认见 AgentDefaults)迭代:请求模型→若 should_execute_tools 则执行工具回灌→否则收敛为最终回复;含空回复/截断/注入恢复
agent/loop.py:76agent/runner.py:321agent/runner.py:386
UNIFIED_SESSION_KEY = "unified:default"
class TurnState(Enum):
RESTORE = auto()
COMPACT = auto()
COMMAND = auto()
BUILD = auto()
RUN = auto()
SAVE = auto()
RESPOND = auto()
DONE = auto() Open Multi-Agent typescript worker 层为 ReAct 式 while(true):LLM→提取 tool_use→并行执行工具→回灌 tool_result→循环,直到无 tool_call 或达 maxTurns(默认 10);团队层为 plan-execute:coordinator 一次拆解 → 队列分轮并行执行 → coordinator 合成
worker 层为 ReAct 式 while(true):LLM→提取 tool_use→并行执行工具→回灌 tool_result→循环,直到无 tool_call 或达 maxTurns(默认 10);团队层为 plan-execute:coordinator 一次拆解 → 队列分轮并行执行 → coordinator 合成
src/agent/runner.ts:761src/agent/runner.ts:948src/orchestrator/orchestrator.ts:565 // -----------------------------------------------------------------
// Main agentic loop — `while (true)` until end_turn or maxTurns
// -----------------------------------------------------------------
while (true) {
// Respect abort before each LLM call.
if (effectiveAbortSignal?.aborted) {
break
}
// Guard against unbounded loops.
if (turns >= this.maxTurns) {
break
} OpenClaw typescript ReAct 式双层 while 循环:内层 LLM→若有 toolCall 则执行(顺序/并行)→结果回灌→重复;外层处理 steering(运行中插话)与 follow-up 消息;stopReason/shouldStopAfterTurn/terminate 决定终止
ReAct 式双层 while 循环:内层 LLM→若有 toolCall 则执行(顺序/并行)→结果回灌→重复;外层处理 steering(运行中插话)与 follow-up 消息;stopReason/shouldStopAfterTurn/terminate 决定终止
packages/agent-core/src/agent-loop.ts:213/**
* Main loop logic shared by agentLoop and agentLoopContinue.
*/
async function runLoop(
initialContext: AgentContext,
newMessages: AgentMessage[],
initialConfig: AgentLoopConfig,
signal: AbortSignal | undefined,
emit: AgentEventSink,
streamFn?: StreamFn,
runtime?: AgentCoreStreamRuntimeDeps,
): Promise<void> {
let currentContext = initialContext; Pipecat python 非 ReAct;范式是 frame-based 流式管道:Frame 沿 processor 链单向流动,每个 FrameProcessor.process_frame() 处理后 push_frame() 给下游;推理本身委托给 LLM service(function-calling 多轮由 run_function_calls 把结果回灌进 LLMContext 再触发下一轮 inference)
非 ReAct;范式是 frame-based 流式管道:Frame 沿 processor 链单向流动,每个 FrameProcessor.process_frame() 处理后 push_frame() 给下游;推理本身委托给 LLM service(function-calling 多轮由 run_function_calls 把结果回灌进 LLMContext 再触发下一轮 inference)
processors/frame_processor.py:615services/llm_service.py:888 if self.__input_event:
self.__input_event.set()
async def process_frame(self, frame: Frame, direction: FrameDirection):
"""Process a frame.
Args:
frame: The frame to process.
direction: The direction of frame flow.
"""
if self._observer:
timestamp = self._clock.get_time() if self._clock else 0
data = FrameProcessed( PraisonAI python 函数调用式 ReAct:_chat_completion 取 LLM 响应→若有 tool_calls 则执行并回灌→无 tool_call 后进入自我反思 while 循环(先答、再让 LLM 输出 {reflection, satisfactory} JSON、不满意则"按反思重写"再循环);满足 min_reflect 且 satisfactory=yes,或达 max_reflect 才返回
函数调用式 ReAct:_chat_completion 取 LLM 响应→若有 tool_calls 则执行并回灌→无 tool_call 后进入自我反思 while 循环(先答、再让 LLM 输出 {reflection, satisfactory} JSON、不满意则"按反思重写"再循环);满足 min_reflect 且 satisfactory=yes,或达 max_reflect 才返回
agent/chat_mixin.py:1131agent/chat_mixin.py:1865agent/chat_mixin.py:1917
# Self-reflection (legacy OpenAI path)
reflection_count = 0
while True:
reflection_prompt = f"""
Reflect on your previous response: '{response_text}'.
{self.reflect_prompt if self.reflect_prompt else "Identify any flaws, improvements, or actions."}
Provide a "satisfactory" status ('yes' or 'no').
Output MUST be JSON with 'reflection' and 'satisfactory'.
"""
reflection_messages = messages + [
{"role": "assistant", "content": response_text},
{"role": "user", "content": reflection_prompt}, Semantic Kernel csharp 非显式 ReAct;核心是 native function-calling 自动循环:模型返回 FunctionCallContent→FunctionCallsProcessor 查表执行→结果回灌 ChatHistory→再次请求模型,直到无工具调用或达上限。Agent 层把每轮新增的 tool/assistant 消息回写线程
非显式 ReAct;核心是 native function-calling 自动循环:模型返回 FunctionCallContent→FunctionCallsProcessor 查表执行→结果回灌 ChatHistory→再次请求模型,直到无工具调用或达上限。Agent 层把每轮新增的 tool/assistant 消息回写线程
dotnet/src/InternalUtilities/connectors/AI/FunctionCalling/FunctionCallsProcessor.cs:25dotnet/src/Agents/Core/ChatCompletionAgent.cs:331/// 4. Invoke each function and add the function result to the <see cref="ChatHistory"/>.
/// </summary>
[ExcludeFromCodeCoverage]
internal sealed class FunctionCallsProcessor
{
/// <summary>
/// The maximum number of auto-invokes that can be in-flight at any given time as part of the current
/// asynchronous chain of execution.
/// </summary>
/// <remarks>
/// This is a fail-safe mechanism. If someone accidentally manages to set up execution settings in such a way that
/// auto-invocation is invoked recursively, and in particular where a prompt function is able to auto-invoke itself,
/// we could end up in an infinite loop. This const is a backstop against that happening. We should never come close smolagents python ReAct 多步循环:while not final and step<=max_steps,每步 think→act→observe;CodeAct 变体下 act=执行 Python 代码。子类只实现 _step_stream
ReAct 多步循环:while not final and step<=max_steps,每步 think→act→observe;CodeAct 变体下 act=执行 Python 代码。子类只实现 _step_stream
agents.py:540agents.py:1638agents.py:1276Strands Agents python model-driven 自驱循环:调模型→stop_reason=="tool_use" 则执行工具→结果回灌→递归重开,无 planner/状态机,全靠模型决策;max_tokens 默认硬失败
model-driven 自驱循环:调模型→stop_reason=="tool_use" 则执行工具→结果回灌→递归重开,无 planner/状态机,全靠模型决策;max_tokens 默认硬失败
event_loop/event_loop.py:179event_loop.py:400event_loop.py:790 )
async def event_loop_cycle(
agent: "Agent",
invocation_state: dict[str, Any],
structured_output_context: StructuredOutputContext | None = None,
limits: Limits | None = None,
) -> AsyncGenerator[TypedEvent, None]:
"""Execute a single cycle of the event loop.
This core function processes a single conversation turn, handling model inference, tool execution, and error
recovery. It manages the entire lifecycle of a conversation turn, including: Swarm python ReAct 式工具循环;while 直到无 tool_call 或 max_turns
ReAct 式工具循环;while 直到无 tool_call 或 max_turns
core.py:257 history = copy.deepcopy(messages)
init_len = len(messages)
while len(history) - init_len < max_turns and active_agent:
# get completion with current history, agent
completion = self.get_chat_completion(
agent=active_agent,
history=history,
context_variables=context_variables,
model_override=model_override,
stream=stream,
debug=debug, SwarmClaw typescript LangGraph createReactAgent + MemorySaver 跑单回合 ReAct(streamEvents v2 流式),外层自研 for 迭代循环做"续跑/早停/工具频控/idle watchdog",受 recursionLimit 约束
LangGraph createReactAgent + MemorySaver 跑单回合 ReAct(streamEvents v2 流式),外层自研 for 迭代循环做"续跑/早停/工具频控/idle watchdog",受 recursionLimit 约束
chat-execution/stream-agent-chat.ts:910 endToolBuildPerf({ toolCount: tools.length })
const checkpointer = new MemorySaver()
let agent = createReactAgent({
llm,
tools,
prompt,
checkpointer,
})
let pendingGraphMessages = [...langchainMessages]
// -------------------------------------------------------------------------
// Init turn state and limits Swarms python ReAct 式 while 循环:call_llm→parse_llm_output→若有 tool_calls 则 execute_tools 回灌→重复,直到无 tool_call 或 loop_count >= max_loops;max_loops="auto" 时无上限直到自决完成
ReAct 式 while 循环:call_llm→parse_llm_output→若有 tool_calls 则 execute_tools 回灌→重复,直到无 tool_call 或 loop_count >= max_loops;max_loops="auto" 时无上限直到自决完成
agent.py:1616agent.py:1701agent.py:1787 self.save()
self._autosave_config_step(loop_count=0)
while (
self.max_loops == "auto"
or loop_count < self.max_loops
):
loop_count += 1
# Compress short-term memory if an auto-loop run has
# crossed the configured fraction of the context window.
if self._context_compressor is not None:
self._context_compressor.maybe_compress(self) Transformers Agents python ReAct(ReactCodeAgent / ReactJsonAgent);早期为单步 plan-then-run
ReAct(ReactCodeAgent / ReactJsonAgent);早期为单步 plan-then-run
查看 Transformers Agents 完整笔记 →Upsonic python 非单一 while 循环,而是 24 步显式 pipeline;LLM↔工具的迭代发生在 model-execution 步内(CallManager 驱动 process_response,达 tool_call_limit(默认100) 停止);带 streaming 平行管线
非单一 while 循环,而是 24 步显式 pipeline;LLM↔工具的迭代发生在 model-execution 步内(CallManager 驱动 process_response,达 tool_call_limit(默认100) 停止);带 streaming 平行管线
agent/pipeline/steps.py:1570agent.py:4629agent.py:2311 self._finalize_step_result(step_result, context)
class ModelExecutionStep(Step):
"""Execute the model request."""
@property
def name(self) -> str:
return "model_execution"
@property
def description(self) -> str:
return "Execute model request" vectara-agentic python 不自研循环,复用 LlamaIndex 的 workflow agent:AgentType.FUNCTION_CALLING(原生 function calling,默认)或 REACT(Thought/Action/Observation 文本协议)。achat() 经 current_agent.run(user_msg, memory, ctx) 驱动 workflow,循环由 LlamaIndex FunctionAgent/ReActAgent 内部完成
不自研循环,复用 LlamaIndex 的 workflow agent:AgentType.FUNCTION_CALLING(原生 function calling,默认)或 REACT(Thought/Action/Observation 文本协议)。achat() 经 current_agent.run(user_msg, memory, ctx) 驱动 workflow,循环由 LlamaIndex FunctionAgent/ReActAgent 内部完成
agent.py:592agent.py:602agent_core/factory.py:58factory.py:103 current_agent = self._get_current_agent()
# Deal with workflow-based agent types (Function Calling and ReAct)
if self._get_current_agent_type() in [
AgentType.FUNCTION_CALLING,
AgentType.REACT,
]:
from llama_index.core.workflow import Context
# Create context and pass memory to the workflow agent
# According to LlamaIndex docs, we should let the workflow manage memory internally
ctx = Context(current_agent) VoltAgent typescript 不自造循环:委托 Vercel AI SDK 的多步工具循环,stopWhen: stepCountIs(maxSteps) 控制步数(默认 maxSteps=5,启用 workspace 时=100);每步 onStepFinish 回灌并写 trace。提供 generateText/streamText/generateObject/streamObject 四种入口
不自造循环:委托 Vercel AI SDK 的多步工具循环,stopWhen: stepCountIs(maxSteps) 控制步数(默认 maxSteps=5,启用 workspace 时=100);每步 onStepFinish 回灌并写 trace。提供 generateText/streamText/generateObject/streamObject 四种入口
agent/agent.ts:1383agent/agent.ts:1081agent/agent.ts:1190 // Default values
temperature: this.temperature,
maxOutputTokens: this.maxOutputTokens,
stopWhen: options?.stopWhen ?? this.stopWhen ?? stepCountIs(maxSteps),
// User overrides from AI SDK options
...aiSDKOptions,
maxRetries: 0,
// Structured output if provided
output,
// Provider-specific options
providerOptions,
// VoltAgent controlled (these should not be overridden)
abortSignal: oc.abortController.signal,