managing-memories
Managing Memories
Extract persistent memories at natural breakpoints, not mid-chain.
When to extract
Extract when ALL conditions are met:
- Volume threshold: substantial conversation has occurred (roughly 10+ back-and-forth exchanges or significant work completed)
- Natural pause: task completed, waiting for user input, or session ending
- Novelty: something worth remembering that isn't already captured
- No pending work: the last assistant turn has no in-progress tool calls or unfinished chains
Do NOT extract:
- Mid-chain (during active multi-step implementation or debugging)
- After trivial exchanges (quick questions, one-liners, status checks)
- When the information is already in project instruction files or existing memories
- When the conversation is primarily about information retrieval (reading files, answering questions) with no decisions or corrections
Memory types
Each memory file uses YAML frontmatter + markdown body. The body for corrections and project conventions must include Why (context) and How to apply (actionable rule).
User preferences — working style, communication style, role
---
name: prefers-minimal-comments
description: User wants no code comments unless explaining non-obvious why
type: user
---
No narrating comments. Only trade-offs, constraints, workarounds.
Corrections — explicit user corrections of agent behavior
---
name: use-pnpm-not-npm
description: Project uses pnpm exclusively, user corrected agent when npm was used
type: correction
---
Always use pnpm for package operations.
**Why:** Project has pnpm-lock.yaml. Using npm creates conflicting lock files and breaks CI.
**How to apply:** Replace `npm install` → `pnpm install`, `npm run` → `pnpm run`, `npx` → `pnpm exec`.
Project conventions — architecture decisions, standards, processes
---
name: api-error-format-rfc7807
description: All API errors must use RFC 7807 Problem Details format
type: project
---
Error response shape: `{ type, title, status, detail, instance }` per RFC 7807.
**Why:** Frontend error handling depends on this format. Inconsistent errors break the error boundary.
**How to apply:** Use the project's error helper to construct responses. Never return bare `{ message }` objects.
References — pointers to external systems and resources
---
name: team-grafana-dashboard
description: Production monitoring dashboard link for the auth service
type: reference
---
Auth service dashboard: https://grafana.internal/d/abc123
On-call runbook: https://wiki.internal/auth-oncall
Session memory (short-term)
Distinct from persistent memories. Session memory captures key facts from the current conversation for use in compaction recovery and context continuity.
When to capture session memory:
- After substantial work (roughly 10K tokens of conversation) AND 3+ tool calls
- At natural breakpoints — task completed, waiting for input, not mid-chain
- Not after trivial exchanges or pure information retrieval
What session memory contains:
- Active task and current approach
- Key decisions made and why
- Files recently read or modified
- Errors encountered (exact messages)
- User corrections and preferences expressed during the session
How it differs from persistent memory:
- Scope: single session, not cross-session
- Lifetime: discarded when session ends (unless worth promoting to persistent memory)
- Purpose: survive compaction without losing operational context
- Storage: in-context or in a temp file, not in the memory directory
Promote to persistent memory when: a session-level observation is likely to be useful in future sessions (e.g., user corrected a recurring mistake, a project convention was discovered).
Memory staleness
Annotate memories with age when loading them into context. Memories older than 1 day should include their age so the agent can weight recent memories more heavily and question stale ones.
Format: append (last updated: 3 days ago) or (last updated: 2025-04-05) to the manifest entry.
If a memory contradicts current evidence (e.g., a project convention that the codebase no longer follows), flag the contradiction rather than blindly following the memory. Stale memories are worse than no memory.
Never save
- Code patterns or architecture — derive by reading the codebase (it changes; memories go stale)
- Git history — use
git log/git blame(authoritative and current) - Debugging solutions — the fix is in the code; the commit message has context
- Anything already documented in project instruction files or existing memories
- Ephemeral task state — in-progress work, temp context, conversation-specific details
Updating vs. creating
Before creating a new memory, check if a related memory already exists:
- If an existing memory covers the same topic, update it (edit the file) rather than creating a duplicate
- If the new information contradicts an existing memory, replace the old one — do not keep both
- If the new information extends an existing memory, append to it
Storage
All memories live in .agent/memories/ at the project root. If the project already has an agent state directory (.claude/, .cursor/memories/, etc.), use the existing one.
.agent/memories/
├── MEMORY.md ← Index (max 200 lines)
├── use-pnpm-not-npm.md
├── prefers-minimal-comments.md
└── api-error-format-rfc7807.md
Create .agent/ on first use. Add it to .gitignore unless the team wants shared agent state.
Organization
- One concept per file. Short, specific, searchable.
- Descriptions must be specific enough for relevance selection: "User prefers Tailwind over CSS modules for styling" not "UI preference."
- Keep the index (
MEMORY.md) with one-line summaries. Each line:- [Title](file.md) — one-line description. Under 200 lines. - File names should be kebab-case and descriptive:
use-pnpm-not-npm.md, notcorrection-1.md.
References
For deeper implementation details, load these on demand:
- Secret scanner patterns — 36 regex patterns for detecting credentials before persisting to shared memory. Scan at the write boundary.
- Memory recall prompt — LLM-based recall prompt template for selecting relevant memories from a store. Includes manifest format and pipeline.
Scripts
- scan-secrets.sh — Portable bash script implementing the secret scanner. Pipe content or pass files/directories. Exit code 1 = secrets found. Returns rule IDs only, never matched text.
- memory-index.sh — Manage the MEMORY.md index: initialize, add entries (with duplicate detection), search, list, validate index-to-file consistency.
./memory-index.sh init .agent/memories— create directory and index./memory-index.sh add .agent/memories use-pnpm-not-npm "Project uses pnpm" correction— add entry./memory-index.sh has .agent/memories use-pnpm-not-npm— check if exists (exit 0=yes)./memory-index.sh search .agent/memories "pnpm"— search index and contents./memory-index.sh validate .agent/memories— check for orphans and missing files
More from beltonk/claude-code-agent-skills
scaffolding-projects
Provides a structured approach to starting new features or projects. Guides the agent through understanding requirements, exploring existing code, planning, incremental implementation, and verification. Use when asked to build something new — a feature, module, service, or project — to avoid jumping into code without context.
4handing-off-sessions
Captures structured session state for resuming work in a new session or handing off to another agent. Use at the end of a session, before context limits, or when the user asks to save progress. Not needed for trivial sessions (quick questions, one-line answers).
4compacting-context
Provides a structured 9-section summarization template for compressing long conversations while preserving critical details. Use when a session approaches context limits and history must be compressed without losing user intent, file changes, errors, or next steps.
4agentic-standards
Foundational behavioral standards for any AI agent — safety/reversibility framework, output quality, memory conventions, and prompt injection defense. Applies to all agent interactions including chat, analysis, writing, debugging, and coding. Use when setting up an agent, onboarding to a new project, or when behavioral baseline guidance is needed. For coding-specific rules, also load coding-practices.
4receiving-code-review
Enforces rigorous handling of code review feedback. Classifies comments by severity, verifies claims independently, prevents blind implementation of incorrect suggestions, and handles contradictory reviews. Use when receiving review comments on a pull request or code change.
1coordinating-agents
Provides a coordinator workflow for orchestrating multiple sub-agents on complex tasks. Covers the research-synthesis-implementation-verification phases, self-contained worker prompt rules, error handling, and anti-patterns. Use when a task has independent sub-problems that benefit from parallel work or when the scope exceeds what a single agent pass can handle reliably.
1