agent起点
没有循环 就没有agent
agent的作用本质 发请求给模型 -> 发现模型想调工具 -> 真的去执行工具 -> 把结果再喂回模型 -> 继续下一轮
概念解释 loop:循环 只要任务未完成就带着信息继续进行循环
turn:一轮 一般可以理解为loop中的一轮 通常包括1把当前消息发给模型 2读取模型回复 3如果模型调用了工具就执行工具 4把工具结果写回消息历史 此为一轮
tool_result 工具执行结果。要重新写回对话历史、让模型下一轮真的能看见的结果块。
state 主循环继续往下走时,需要一直带着走的那份数据。
user message | v LLM | +— 普通回答 ----------> 结束 | +— tool_use ----------> 执行工具 | v tool_result | v 写回 messages | v 下一轮继续
message {“role”: “user”, “content”: ”…”} {“role”: “assistant”, “content”: […]}
Tool Result Block { “type”: “tool_result”, “tool_use_id”: ”…”, “content”: ”…”, } tool_use_id 的作用很简单:
告诉模型“这条结果对应的是你刚才哪一次工具调用”。
LoopState 这章建议你不要只用一堆零散局部变量。
最小也应该显式收拢出一个循环状态:
state = { “messages”: […], “turn_count”: 1, “transition_reason”: None, } 这里的 transition_reason 先只需要理解成:
这一轮结束后,为什么要继续下一轮。
agent 的核心不是“模型很聪明”,而是“系统持续把现实结果喂回模型”。
Thought 思考结果 Action 需求动作 Observation 工具使用的结果
If this article helped you, please share it with others!
Some information may be outdated






