--- tags: [agent-loop, mermaid, claude-code, 流程图] create time: 2026-06-09 22:30 --- # Agent Loop 完整流程 ## 概述 Claude Code Agent 的完整执行循环,包含 Context 管理、Pre/Post-sampling Hook、权限审批、工具并发执行、子 Agent 递归调用、Stop Hook 和 Token Budget 控制等全部环节。 ## 正文 ```mermaid flowchart TB START(("输入")) --> CTX["Context 管理"] CTX --> PRE["Pre-sampling Hook"] PRE --> LLM["LLM 流式输出"] LLM --> TC{"tool_use?"} TC --> |是| PERM{"需权限?"} PERM --> |是| USER["用户审批"] USER --> |allow| TOOL_PRE USER --> |deny| DENIED["拒绝"] PERM --> |否| TOOL_PRE["Pre-tool Hook"] TOOL_PRE --> EXEC["并发执行工具"] EXEC --> TOOL_POST["Post-tool Hook"] TOOL_POST --> CTX DENIED --> CTX TC --> |否| POST["Post-sampling Hook"] POST --> STOP{"Stop Hook"} STOP --> |不通过| CTX STOP --> |通过| BUDGET{"Token Budget"} BUDGET --> |继续| CTX BUDGET --> |完成| DONE(("完成")) subgraph SUB["子 Agent"] FORK["AgentTool"] --> RECURSE["递归调用"] end EXEC -.-> FORK classDef proc fill:#eef,stroke:#66c,color:#224 classDef decision fill:#fee,stroke:#c66,color:#422 classDef hook fill:#ffe,stroke:#cc6,color:#442 classDef io fill:#eff,stroke:#6cc,color:#244 classDef sub fill:#efe,stroke:#6a6,color:#242 class CTX,LLM,EXEC proc class TC,PERM,STOP,BUDGET decision class PRE,TOOL_PRE,TOOL_POST,POST hook class START,DONE,USER,DENIED io class FORK,RECURSE sub ``` ## 关联笔记 - [[agent-loop-simple]] - Agent Loop 简易流程