Building Persistent Memory and Context Management for AI Agents
This fourth installment of a 10-part tutorial updates the Xiaoji agent to v0.4 with automatic trajectory compression and a file-based cross-session memory system. It explains three ways to control growing context—trimming, summarizing, and externalizing—and provides Python code using OpenAI's API for both compaction and a remember tool. A demonstration also shows that model-written memories can contain inferred or incorrect details, so the file should remain human-readable and editable.
The tutorial provides implementable patterns for controlling context growth and retaining useful information across agent sessions while making their costs and failure modes explicit.
《手搓 Agent》系列第 4 篇,共 10 篇。系列导读与目录|上一篇:睁开眼睛——上下文是它看到的全部世界
图 4-1 系列地图。这一篇还在蓝色的框里:眼睛。
前言
上一篇留下两个问题:轨迹只增不减,早晚装不下;程序一关,它什么都不记得。
这两个问题说的其实是同一件事:记性。我原来以为“记忆”是模型的一项功能,打开某个开关它就记住了。学到这里才明白,模型没有这项功能。第一篇讲过,它的参数训练完就固定了,每次请求对它都是全新的。所谓记性,全是在上下文上做文章。