Skip to content

Chapter 06 · Memory and AGENTS.md ​

memory= points to AGENTS.md files that are all loaded at startup and persisted in the backend. It fits “long-term preferences / project conventions” and complements on-demand skills.

Mechanics and design choices ​

memory vs skill differs in load timing: memory always enters context; skills enter as frontmatter summaries and then on-demand full text. Keep memory short and stable; too much inflates per-turn tokens.

Example ​

python
import os
from langchain_openai import ChatOpenAI

model = ChatOpenAI(
    model=os.environ["MODEL_ID"],
    api_key=os.environ["DEEPAGENT_API_KEY"],
    base_url=os.environ.get("DEEPAGENT_BASE_URL", "https://api.openai.com/v1"),
)

import pathlib, shutil, tempfile
from deepagents import create_deep_agent
from deepagents.backends import FilesystemBackend

root = tempfile.mkdtemp(prefix="deepagents-memory-")
try:
    agents_md = pathlib.Path(root) / "AGENTS.md"
    agents_md.write_text("# Rules\n- Use Python 3.11+\n- Never hard-code secrets\n")
    agent = create_deep_agent(
        model=model,
        memory=[str(agents_md)],
        backend=FilesystemBackend(root_dir=root, virtual_mode=True),
        system_prompt="Follow the AGENTS.md rules.",
    )
    result = agent.invoke(
        {"messages": [{"role": "user", "content": "Summarize the coding rules in one line."}]},
        {"configurable": {"thread_id": "ch06"}},
    )
    print(result["messages"][-1].content)
finally:
    shutil.rmtree(root, ignore_errors=True)

Run and verify ​

API key required. The answer mentions both the Python 3.11+ and no-hard-coded-secrets rules.

Pitfalls ​

AGENTS.md holds conventions, not secrets. Put credentials in a secret manager; do not push the .env file itself into memory.

🧪 随堂测验

点击你认为正确的选项。答错时会展示正确答案与原因解析。

1. When is memory loaded?

2. Should AGENTS.md hold secrets?

3. What happens if memory is large?

4. How do skill and memory divide work?

Hands-on exercises ​

  1. Attach the same memory file to two agents and observe sharing.
  2. Write a preference the model will act on and verify it.
  3. Compare load timing between memory and skill.

Official references ​

Verified on 2026-09-17 with Deep Agents 0.7.15.