When Is Claude Code’s Memory Injected into system_prompt? Interview Insight

The article explains that Claude Code loads persisted memory once at REPL startup via _build_system(), inserts it as the 10th segment of system_prompt, enforces a 200‑line limit on MEMORY.md, deliberately avoids side‑effects in get_memory_dir(), and only refreshes the prompt with the /model command.

Wu Shixiong's Large Model Academy
Wu Shixiong's Large Model Academy
Wu Shixiong's Large Model Academy
When Is Claude Code’s Memory Injected into system_prompt? Interview Insight

1. Injection entry point: _build_system() runs once at REPL startup

Memory is loaded by the function _build_system(), called once when the REPL starts (see main.py:285). It assembles the system_prompt and inserts the persisted memory content. Because it runs only once, memory written during a session is not visible until the next REPL launch.

This design avoids the misconception that writing a memory entry makes it immediately effective in the current conversation.

2. Injection position: 10th segment of system_prompt, before CLAUDE.md

The memory block is placed at the 10th segment of system_prompt (see builder.py:74-135). The order is: core system instructions → environment info → tool descriptions → SUMMARIZE_TOOL_RESULTS → Memory → CLAUDE.md → remaining dynamic sections.

Placing memory in the middle‑front of the prompt ensures it is read by the model while still allowing project‑level directives in CLAUDE.md to override memory preferences.

Claude Code system_prompt construction order
Claude Code system_prompt construction order

3. Hard limit: MEMORY.md reads at most 200 lines

The index file MEMORY.md is read up to MAX_ENTRYPOINT_LINES = 200 (defined in sections.py:179). Each line is an entry of the form - safe_name -- description. Exceeding 200 lines causes truncation and a warning.

This limit balances token budget and the number of memory entries; exceeding it requires consolidating entries.

4. Design quirk: get_memory_dir() does not create the directory

The function get_memory_dir() returns the memory directory path without creating it, adhering to the principle “read operations should not have write side‑effects.” Directory creation is performed inside _build_system(), so later Write tool calls can write files without an extra mkdir.

5. Current session prompt is static – the only exception

During a session the system_prompt does not refresh when new memory files appear. The sole way to force a refresh is to run the /model command, which re‑executes _build_system() (see main.py:711), rebuilding the prompt with the latest memory.

This trick lets users make newly written memory effective immediately.

How to answer in an interview

Explain the four points in roughly 60‑90 seconds: trigger timing via _build_system(), placement as the 10th segment, the 200‑line hard cap, and the read‑only design of get_memory_dir() with the /model refresh exception.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

LLMInterview PreparationREPLClaude Codesystem promptMemory injection
Wu Shixiong's Large Model Academy
Written by

Wu Shixiong's Large Model Academy

We continuously share large‑model know‑how, helping you master core skills—LLM, RAG, fine‑tuning, deployment—from zero to job offer, tailored for career‑switchers, autumn recruiters, and those seeking stable large‑model positions.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.