InkdownInkdown
Start writing

Claude-Code

62 files·4 subfolders

Shared Workspace

Claude-Code
codex

13-memory-system

Shared from "Claude-Code" on Inkdown

Memory System (memdir)

Claude Code's persistent file-based memory — how it remembers things across sessions.


Overview

The memory system lets Claude Code persist information across sessions. It stores memories as markdown files and maintains an index (MEMORY.md) for quick lookup.


Memory Types

TypePurposeExample
0000_start_here_index_and_recommended_reading_order.md
0100_project_overview_tech_stack_runtime_modes_and_folder_map.md
0200_startup_flow_entry_points_and_cold_start_sequence.md
0300_codebase_modules_layers_state_models_and_schemas.md
0400_system_architecture_and_design_rationale.md
0500_interactive_repl_request_flow_end_to_end.md
0600_headless_sdk_and_print_mode_request_flow_end_to_end.md
0700_mcp_integration_connection_and_tool_call_flow.md
0800_external_services_sdks_storage_and_local_dependencies.md
0900_environment_variables_settings_feature_flags_and_failure_modes.md
1000_non_obvious_patterns_gotchas_and_debugging_traps.md
1100_full_codebase_file_inventory_grouped_by_directory.md
kimi
00-overview.md
01-entrypoints.md
02-state-management.md
03-query-system.md
04-tools-system.md
05-tasks-system.md
06-ui-components.md
07-bridge-remote.md
08-services.md
09-skills-plugins.md
10-commands.md
11-testing-architecture.md
12-permission-system.md
13-build-system.md
14-ink-internals.md
15-git-internals.md
16-context-compaction.md
17-vim-mode.md
18-mailbox-notifications.md
19-session-persistence.md
20-hooks-system.md
21-error-recovery.md
README.md
qwen
00-overview.md
01-entry-points.md
02-query-engine.md
03-tools-and-tasks.md
04-commands-and-skills.md
05-state-management.md
06-ink-rendering.md
07-bridge-remote.md
08-mcp-services.md
09-services-overview.md
10-multi-agent.md
11-system-prompt-constants.md
12-tool-interface.md
13-memory-system.md
14-buddy-companion.md
15-keybindings.md
16-stop-hooks.md
17-vim-mode.md
18-upstreamproxy.md
19-cost-tracking-history.md
20-contexts-styles-onboarding.md
21-hooks.md
22-screens.md
tweets-explain
claude-code-memory-analysis.md
compact
memory-system
agentic-architecture
user
About the person
"Prefers TypeScript over Python"
feedbackCorrections/confirmations"Don't use semicolons"
projectWork context"API is at /api/v2/users"
referenceExternal system pointers"Jira project key is CC"

Storage Architecture

Plain text
~/.claude/projects/<git-root>/memory/
├── MEMORY.md                    # Index file (pointers to all memories)
├── user/
│   ├── preferences.md           # Individual memory files
│   └── work-style.md
├── feedback/
│   └── code-style.md
├── project/
│   ├── architecture.md
│   └── deployment.md
└── reference/
    └── external-systems.md
Two-Step Save
  1. Write content to individual .md files with YAML frontmatter:

    Markdown
    ---
    type: project
    created: 2025-01-15
    ---
    The API uses GraphQL at /api/graphql
  2. Add pointer to MEMORY.md (the index):

    Markdown
    - [API uses GraphQL](project/architecture.md)
Truncation Protection

MEMORY.md is capped at:

  • 200 lines maximum
  • 25KB maximum

When the cap is hit, old entries are removed.


Path Resolution

Memory directory is resolved in priority order:

  1. Environment variable — CLAUDE_MEMORY_DIR
  2. Settings.json — memoryDir field
  3. Computed — derived from git root
Security Validation

Paths are validated to reject:

  • Relative paths (../)
  • Root paths (/)
  • UNC paths (\\server\share)
  • Null bytes

Team Memory

When feature('TEAMMEM') is enabled, shared memory is available:

Plain text
<project-root>/.claude/memory/
├── MEMORY.md
├── user/
├── feedback/
├── project/
└── reference/

Team memory is stored in the project directory (not ~/.claude/) so all team members share the same memories.

Team Memory Paths
TypeScript
// Resolve team memory path
getTeamMemDir(projectRoot)
getTeamMemPath(projectRoot, type, name)
Team Memory Prompts

Special instructions injected into the system prompt when team memory is active:

Plain text
You have access to team memory files at <path>.
These memories are shared with all team members.

KAIROS Daily Log Mode

For long-lived assistant sessions, memory works differently:

Plain text
<project-root>/.claude/memory/
├── logs/
│   └── 2025/
│       └── 01/
│           ├── 2025-01-15.md   # Append-only daily log
│           └── 2025-01-16.md
└── topics/                     # Distilled from logs
    ├── architecture.md
    └── decisions.md

Instead of maintaining a live MEMORY.md index:

  1. Daily logs are append-only (date-named files)
  2. A nightly /dream skill distills logs into topic files
  3. Topics are the distilled, organized knowledge

Memory Scanning

scanForMemories(context)

Scans for relevant memories before a query:

TypeScript
// Called at query start
const memories = await scanForMemories({
  messages,
  toolUseContext,
})

// Returns relevant memories based on:
// - Current conversation topic
// - Recent tool usage
// - User messages
findRelevantMemories(messages)

Finds memories relevant to the current conversation:

TypeScript
// Uses semantic matching on:
// - Keywords in messages
// - File paths mentioned
// - Tool names used
Memory Age

Memories have an age field in frontmatter:

YAML
---
type: project
created: 2025-01-15
age: fresh | recent | stale | old
---

Age affects relevance scoring.


System Prompt Integration

Memory instructions are injected into the system prompt:

Plain text
## Memory

You have access to memory files that store important information
about this project and user preferences.

To search memories:
1. Check MEMORY.md for the index
2. Read relevant files with FileReadTool
3. Use GrepTool to search across all memory files

Key Files Reference

FilePurpose
src/memdir/memdir.tsCore memory system
src/memdir/memoryTypes.tsMemory type definitions
src/memdir/paths.tsPath resolution
src/memdir/memoryScan.tsMemory scanning
src/memdir/findRelevantMemories.tsRelevance matching
src/memdir/memoryAge.tsAge tracking
src/memdir/teamMemPaths.tsTeam memory paths
src/memdir/teamMemPrompts.tsTeam memory prompts
src/services/SessionMemory/Session memory service
src/services/extractMemories/Memory extraction
src/services/teamMemorySync/Team memory sync