章节摘要:本章进入攀爬装备区第一站——记忆与规划。AgentMemory 把 agent 的完整轨迹结构化(SystemPromptStep/TaskStep/ActionStep/PlanningStep/FinalAnswerStep);planning_interval 让 agent 每 N 步重新规划(human-in-the-loop 的基础);write_memory_to_messages 把记忆转成 LLM 消息列表;save/load/push_to_hub 支持序列化与分享。这是理解 agent"怎么记住自己做过什么"的关键。本章共 2 节,前置依赖为第 1-3 章。

memory.py;AgentMemory(system_prompt/task/planning steps列表);五种步骤(SystemPromptStep系统提示/TaskStep用户任务/ActionStep动作含observations与error/PlanningStep规划/FinalAnswerStep最终答案);ToolCall记录;CallbackRegistry回调注册;write_memory_to_messages(记忆→LLM消息,含图片多模态消息)。
planning_interval参数(每N步插入PlanningStep让LLM重新规划"已做什么/还剩什么");plan_customization示例(human-in-the-loop人工改计划);write_memory_to_messages与summarization;serialization.py(514行)安全序列化(JSON优先/pickle可选回退含安全警告);save/load本地;push_to_hub分享到HF Hub。
前置:第1-3章。后续:第5章工具体系(agent 的手脚)。