Long-Term Memory for Agents in Practice: Building, Maintaining, and Fixing Cross-Session Memory

1. The takeaway: long-term memory has three layers, each with its own job
A large language model starts every session from a blank page. Long-term memory needs three mechanisms: the rule layer is rule files written by you, such as Claude Code's CLAUDE.md and Cursor's .cursor/rules; the automatic memory layer is notes the agent writes itself; the memory service layer is an external memory layer such as Mem0, which stores user facts as memory shared across sessions and tools.
The layers should not replace each other: fixed conventions written in rule files cannot be learned by automatic memory; a preference mentioned in passing is not worth a rule file; and facts shared across tools cannot be read by other tools if they sit in one tool's rule files.

2. What you need before starting
Start with a Git repository: rule files and memory files belong in version control so mistakes can be rolled back. Then pick Cursor or Claude Code — either one — and get one tool's memory mechanism working.
Local files are enough for personal projects; consider Mem0's memory layer when user facts must be shared across tools and agents. Before writing anything, set a privacy boundary: secrets, tokens, customer data, and personal information never enter any memory file or memory service.
3. The rule layer: how to write rule files and memory files
Claude Code's rule file is CLAUDE.md, with four scopes: project-level files at ./CLAUDE.md or ./.claude/CLAUDE.md; user-level files at ~/.claude/CLAUDE.md; personal project-level CLAUDE.local.md, which belongs in .gitignore; and organization-level files managed by IT. Larger projects can split rules into the .claude/rules/ directory, one file per topic, with paths limiting a rule to matching files. Keep a single file under 200 lines and write concrete, verifiable instructions.
Cursor's rule files are .mdc files with frontmatter in the .cursor/rules directory; three fields control when a rule applies: description (the agent judges relevance from it), globs (the rule applies only to matching files), and alwaysApply (the rule applies to every session). Four application modes: Always Apply applies every session, Apply Intelligently lets the agent judge, Apply to Specific Files applies per file, and Apply Manually applies when @-mentioned. Keep rules under 500 lines. Team rules are configured by admins in the dashboard, and precedence runs team rules, then project rules, then user rules.
4. The automatic memory layer and the memory service layer
Automatic memory is Claude Code writing its own notes: it records what it learns from your corrections and preferences, and each session loads the first 200 lines or 25KB. A wrongly recorded preference keeps affecting later sessions, so correct it directly in a session and let the agent rewrite that note.
Mem0 makes memory a standalone layer, and agents read and write user facts through its interface, with memories kept across sessions and agents. Mem0 offers an open-source version that can be self-hosted, and the official hosted Mem0 Platform runs the vector store and rerankers for you. There are two ways to use it: the application calls the memory layer's interface to read and write memories, or the rule file tells the agent to record important decisions in memory files during the session.
5. Trade-offs, handling stale memory, and privacy
Memories go stale: old preferences, abandoned conventions, and replaced tech stacks all become noise that misleads the agent. Review once a month or at each milestone, delete stale entries, and when two memories conflict, keep the newer one and delete or mark the older one as superseded. Claude Code's /doctor prompt-audit reviews instruction files for outdated and conflicting content automatically.
Trade-offs: rule files hold only facts needed in every session, while multi-step procedures belong in skills or path-scoped rules; automatic memory holds corrections and preferences; only facts shared across tools belong in the memory layer. Privacy: secrets and customer data never enter any memory file or memory service; when self-hosting a memory layer, treat its database like sensitive data.
The steps in this article were verified against the official pages on 2026-11-12; if platform rules change, the official pages apply.
Tools mentioned
Sources
This article is compiled from the public sources above and does not reproduce the originals in full; see the official sources for details.