Skip to content

Memory: making it remember the context ​

After reading this section, your Agent will still remember what it did before even after being closed and reopened; and long conversations won't blow up the context.

The model itself remembers nothing ​

Every time you call the model, it's "meeting you for the first time." The reason it seems to remember the earlier conversation is that we resend the entire conversation (the messages list) to it every time. That's exactly what the loop in the previous section did.

So an Agent's memory, in essence, comes down to managing this list:

  • Save it: write it to a file before the program exits, and read it back on the next startup;
  • Don't let it get too long: the model can only see a limited length, and longer is also slower and more expensive.

Saving to disk: cross-session memory ​

python
MEMORY = HERE / ".agent" / "history.json"          # 对话记忆存在这里,下次启动接着聊

def load_memory():
    return json.loads(MEMORY.read_text(encoding="utf-8")) if MEMORY.exists() else []

def save_memory(messages):
    MEMORY.parent.mkdir(exist_ok=True)
    MEMORY.write_text(json.dumps(messages, ensure_ascii=False, indent=1), encoding="utf-8")

Save once at the end of each conversation round. Here's how we verified it: first run it once and have it organize a weekly report (following the skill's requirements, it shows the full text to the user before writing the file, then the program ends); then start a new process and say just one sentence:

bash
python agent.py "确认,就按这个写入文件"

You should see:

text
  [1] write_file({"path": "./周报.md", "content": "# Weekly Report\n## Completed This Week\n- Monday: Fixed the style misalignment of the login page on Safari\n- ..."})

⚠️  Write to file ./周报.md (175 characters)? [y/N] y
Agent: The weekly report file 周报.md has been generated, containing this week's work summary and plans.

In the new process it never read those record files, yet it knows what "this" refers to and what the weekly report contains, because the memory was read back from history.json.

What if it gets too long: compress into a summary ​

python
def compact(messages):
    if len(json.dumps(messages, ensure_ascii=False)) < KEEP_CHARS:
        return messages
    old, recent = messages[:-8], messages[-8:]
    while recent and recent[0]["role"] == "tool":        # 别把工具结果和它对应的调用拆开
        old, recent = old + recent[:1], recent[1:]
    summary = client.chat.completions.create(model=MODEL, messages=[
        {"role": "user", "content": "把下面的对话压缩成要点(事实、决定、未完成的事),不超过 300 字:\n"
         + json.dumps(old, ensure_ascii=False)[:20000]}]).choices[0].message.content
    return [{"role": "user", "content": "【之前对话的摘要】" + summary},
            {"role": "assistant", "content": "好的,我记住了。"}] + recent

The idea: keep the most recent 8 messages verbatim, and have the model itself compress earlier ones into a summary placed at the very front.

That while is important: if the split point happens to fall on a tool message, its corresponding tool call gets compressed into the summary, leaving behind an "orphaned" tool result, which many APIs will reject outright. So you need to shift the split point later until the list doesn't start with a tool message.

By default, compression only kicks in after exceeding 24000 characters. For the demo, we temporarily lowered the threshold to 1500 characters using an environment variable:

bash
AGENT_KEEP_CHARS=1500 python agent.py "worklog 里周三做了什么?"

You should see it answer the question first, then show a notice:

text
(Memory too long; earlier conversation has been compressed into a summary)

Open .agent/history.json, and the first entry now looks like this (our actual result this time):

text
【Summary of the earlier conversation】**Facts**:
- The worklog folder contains 11-16.txt, 11-18.txt, and 11-20.txt.
- After organizing, the weekly report contains: completed this week (5 items), in progress (1 item), plans for next week (1 item), needs assistance (none).
……

Look it upOther ways to do memory

Click one to see.

Quiz timeWhen compressing memory, why can't the retained part start with a tool message?

Check yourself ​

  • I understand that "memory is the conversation history carried along each time"
  • After I close and restart the agent, it still remembers the previous session's content
  • I triggered a summary compression once using a small threshold

Next section: Skills: SKILL.md.

gaodong.aiAboutPrivacyContact

Independent tutorials, not affiliated with any product mentioned. Product names and trademarks belong to their owners. Check official pages for prices and policies.