# 第十章 Agent Loop 运行内核剖析

本章聚焦 Agent Loop 的执行主链：请求如何进入、上下文如何组装、工具如何被调度、结果如何流式返回。理解这条主链，是深入架构设计、定位复杂故障与优化执行效率的关键所在。

在深入代码细节之前，我们需要先建立一个心智模型：**OpenClaw 通过嵌入式集成将 π（pi）运行底座的极简执行骨架纳入自身进程，在此基础上叠加渠道适配、工具策略、分道排队等企业级能力，形成完整的智能体运行时。**

本章围绕上述架构底座的执行链路展开，依次剖析每个核心组件的运作原理与工程设计。

## 本章学习目标

本章内容将涵盖以下核心机制，包括排队控制、提示词工程、工具调度以及流式输出管理。

* [**10.1 请求流转与分层排障**](/openclaw_guide/di-san-bu-fen-shi-xian-yuan-li-yu-gong-cheng-luo-di/10_agent_loop/10.1_request_lifecycle.md)：追踪一条消息的完整生命周期，掌握“由外到内”的分层排障策略。
* [**10.2 π 运行底座与嵌入式集成**](/openclaw_guide/di-san-bu-fen-shi-xian-yuan-li-yu-gong-cheng-luo-di/10_agent_loop/10.2_pi_framework.md)：剖析 OpenClaw 如何通过嵌入 pi SDK 获得事件驱动的推理循环能力。
* [**10.3 入口、排队与并发控制**](/openclaw_guide/di-san-bu-fen-shi-xian-yuan-li-yu-gong-cheng-luo-di/10_agent_loop/10.3_entry_queue.md)：理解纯 TypeScript 实现的 Command Queue 分道排队机制。
* [**10.4 提示词装配与结构化注入防护**](/openclaw_guide/di-san-bu-fen-shi-xian-yuan-li-yu-gong-cheng-luo-di/10_agent_loop/10.4_prompt_assembly.md)：掌握提示词结构化装配过程，理解基于 Token 预算的裁剪策略。
* [**10.5 工具执行与结果回注**](/openclaw_guide/di-san-bu-fen-shi-xian-yuan-li-yu-gong-cheng-luo-di/10_agent_loop/10.5_tool_execution.md)：拆解工具调用的策略过滤、钩子拦截与结果裁剪机制。
* [**10.6 流式输出、重试与提前终止**](/openclaw_guide/di-san-bu-fen-shi-xian-yuan-li-yu-gong-cheng-luo-di/10_agent_loop/10.6_streaming_retry.md)：Block Chunker 的智能切分、有界重试策略与模型故障切换概述。
* [**10.7 本章小结**](/openclaw_guide/di-san-bu-fen-shi-xian-yuan-li-yu-gong-cheng-luo-di/10_agent_loop/summary.md)：总结核心运行机制，并为后续可靠性与安全章节做好衔接。

## 阅读建议

本章属于“深水区”，建议在阅读时对照自己脑海中的传统中间件架构，思考 Agent 模式为什么必须要把“状态管理”推向极致。并在本地跑起最小用例，打开结构化日志观测执行链路。


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://yeasy.gitbook.io/openclaw_guide/di-san-bu-fen-shi-xian-yuan-li-yu-gong-cheng-luo-di/10_agent_loop.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
