[Agent]智能体循环[Agent] The Agent Loop
Agent loop
agent接收input后在调用工具->返回到agent与Agent相应的循环
一次对话(turn)就是指从输入到最后返回控制权/结果(助手信息)的过程
单次对话:
多轮对话就是多了一个history,模型每次从asembled history里找最近消息,然后继续循环,将新的工具调用与响应写入histroy
第一次查询
使用Response API,当用户查询时,API会将查询结构化,形成一个项目列表,并根据项目的角色确定优先级:system、developer、user、assistant
Pesponse API接收一个JSON payload,重点是三个部分:instruction, tools ,input
instruction 是插入模型context的系统/开发者命令
tools 可能使用的工具列表
input 文本/图像/文件输入列表
input在发出之前会插入以下内容:
role:developer :描述沙箱权限
可选 role:developer :从用户
config.toml文件中读取的developer_instructions值可选 role:user :skills,agent.md,agent.override.md以及config.toml读取的指定文件(大小限制32KiB)
role:user ,描述智能体当前运行的本地环境
随着对话不断推进,我们发送给 Responses API 的 input 长度会不断增加
Item(项) 与 Delta(增量)
OpenAI定义的Response生命周期:
| 阶段 | 对应事件 | 语义与作用 |
|---|---|---|
| 容器初始化 | response.created | 服务端收到了请求,正式开启一次推理 Response。 |
| 条目诞生 | response.output_item.added | 服务端通知客户端:“模型接下来要输出一个新物件了”,并在 payload 中指明这个 item 是文本还是工具调用。 |
| 内容增量 | response.output_text.delta<br>response.function_call_arguments.delta | 最核心的高频流事件。每次模型吐出新的几个字或几段代码参数碎片时触发,客户端用于拼接或实时打印。 |
| 内容完成 | response.output_text.done<br>response.function_call_arguments.done | 该条目内的文本或工具调用参数全部生成完毕。 |
| 条目关闭 | response.output_item.done | 整个 item 的最终完整状态(包含累积好的完整文本或 JSON 参数)封包下发。 |
| 响应结束 | response.completed / response.done | 模型在这一步推理完成,关闭当前 HTTP SSE 长连接。 |
对应关系:
Turn(用户发问到最终得到回答的完整轮次)│├── Iteration 1(第 1 次 API 请求)│ └── 对应一个 Response(从 response.created 到 response.done)│ ├── item 1: message ("正在为您检查目录...")(从item added到item.done)
| | ------ delta是token│ └── item 2: function_call ("ls -la")
| | ------ delta是json片段
││ [客户端本地执行 ls 命令,拿到结果]│├── Iteration 2(第 2 次 API 请求,带着工具返回的执行结果再次请求)│ └── 对应一个全新的 Response(从 response.created 到 response.done)│ └── item 1: message ("当前目录下共有 5 个文件,分别是...")│└── Turn 结束(没有后续工具调用,控制权归还给用户)
Cache Hit 缓存命中
cache hit能让agent重用之前的计算,从而将复杂度从2次增长降低至线性增长
缓存未命中通常发生在:更换tools,更换沙盒/审批模式/工作目录,更换模型
缓存命中与上下文窗口有明显的Trade-off(制约)关系

上下文越多,则越稳定(不变信息的多),命中率就高
现代工程解决方案是compact(压缩),token消耗到一定上限制时启用压缩将input压缩
Agent loop
After receiving input, the agent calls tools -> returns to the agent, forming the corresponding loop with the Agent
A single turn refers to the process from input to finally returning control/results (assistant message)
Single turn:
Multi-turn conversation simply adds a history; each time, the model finds the most recent messages from the assembled history, then continues the loop, writing new tool calls and responses into the history
First Query
Using the Response API, when a user queries, the API structures the query into a list of items and determines priority based on the item's role: system, developer, user, assistant
The Response API receives a JSON payload, with three key parts: instruction, tools, input
instruction is the system/developer command inserted into the model's context
tools is the list of tools that may be used
input is the list of text/image/file inputs
Before input is sent out, the following content is inserted:
role:developer : describes sandbox permissions
optional role:developer : the
developer_instructionsvalue read from the user'sconfig.tomlfileoptional role:user : skills, agent.md, agent.override.md, and specified files read from config.toml (size limit 32KiB)
role:user , describes the local environment in which the agent is currently running
As the conversation continues, the length of the input we send to the Responses API keeps increasing
Item and Delta
OpenAI's defined Response lifecycle:
| Stage | Corresponding Event | Semantics and Role |
|---|---|---|
| Container initialization | response.created | The server has received the request and officially starts an inference Response. |
| Item birth | response.output_item.added | The server notifies the client: "The model is about to output a new object," and indicates in the payload whether this item is text or a tool call. |
| Content delta | response.output_text.delta<br>response.function_call_arguments.delta | The most core high-frequency streaming event. Triggered each time the model emits a few new characters or fragments of code parameters; the client uses it for concatenation or real-time printing. |
| Content completion | response.output_text.done<br>response.function_call_arguments.done | The text or tool call parameters within this item have been fully generated. |
| Item closure | response.output_item.done | The final complete state of the entire item (including the accumulated full text or JSON parameters) is packaged and sent down. |
| Response end | response.completed / response.done | The model has finished inference at this step and closes the current HTTP SSE long connection. |
Correspondence:
Turn (the complete round from the user asking to finally getting an answer)│├── Iteration 1 (1st API request)│ └── corresponds to one Response (from response.created to response.done)│ ├── item 1: message ("Checking the directory for you...") (from item added to item.done)
| | ------ delta is a token│ └── item 2: function_call ("ls -la")
| | ------ delta is a JSON fragment
││ [Client locally executes the ls command and gets the result]│├── Iteration 2 (2nd API request, requesting again with the tool's returned execution result)│ └── corresponds to a brand-new Response (from response.created to response.done)│ └── item 1: message ("There are 5 files in the current directory, namely...")│└── Turn ends (no subsequent tool calls, control returns to the user)
Cache Hit
A cache hit allows the agent to reuse previous computations, thereby reducing complexity from quadratic growth to linear growth
Cache misses usually occur when: changing tools, changing sandbox/approval mode/working directory, changing model
There is a clear trade-off between cache hits and the context window
The more context there is, the more stable it is (more unchanging information), and the higher the hit rate
A modern engineering solution is compact (compression): when token consumption reaches a certain upper limit, compression is enabled to compress the input
![[书] 挪威的森林](/_next/image?url=%2Fuploads%2F1790522448212-b370bfcb09051b5b.webp&w=3840&q=75)
![[书] 邓小平时代](/_next/image?url=%2Fuploads%2F1790318183685-756adf913973e6ef.webp&w=3840&q=75)