The Research Report
How Claude Code Assembles Its 7‑Layer System Prompt
Suggested title: “Claude Code’s 7‑Layer Prompt Architecture – A Reverse‑Engineered Walk‑through”
Claude Code is Anthropic’s official CLI for Claude. Behind the sleek UI lies a sophisticated system prompt. The prompt isn’t a single static block. It’s built from seven distinct sections that stack in a strict order. Each section has a purpose, a priority, and a caching rule.
1️⃣ Identity & Prefix
The very first line tells the model who it is: “You are Claude Code, Anthropic’s official CLI for Claude.” This sets the tone and anchors the model’s persona.
2️⃣ Role Definition
Next comes a role description that switches between “assist” (interactive mode) and “complete” (headless mode). The model knows when to ask for clarification and when to just output code.
3️⃣ Security Guardrails
Claude Code embeds a risk taxonomy. It explicitly bans destructive actions – no DoS, no supply‑chain attacks. It also flags unsafe URLs with a “Never generate or guess URLs” rule.
4️⃣ Permission & Hooks
This layer defines permission modes and lifecycle hooks. It tells the model how to handle denied tool calls and where to insert custom hooks.
5️⃣ Anti‑YAGNI Guideline
Instead of vague advice, the prompt lists concrete prohibitions: don’t add features beyond the request, don’t create one‑off helpers, don’t add unnecessary error handling. This keeps the output tight and relevant.
6️⃣ Action Risk Taxonomy
Claude Code classifies actions into LOW, MEDIUM, HIGH risk. Low‑risk actions (reading files, searching code) run freely. Medium‑risk actions (writing files, creating branches) need caution. High‑risk actions (deleting files, force‑pushing) require explicit confirmation.
7️⃣ Tool & Output Rules
The final static layer tells the model which tools to prefer (Read over cat, Grep over grep) and how verbose to be. It enforces a ≤25‑word window between tool calls and caps overall response length for external users.
The Dynamic Boundary – Where Caching Ends
Between sections 6 and 7 sits a hidden marker:
__SYSTEM_PROMPT_DYNAMIC_BOUNDARY__
Everything above the marker is static. Anthropic’s API caches those tokens across turns, saving latency and cost. Everything below the marker is rebuilt each turn because it changes with the session.
Dynamic Sections (per‑session)
- Environment Info – current working directory, git branch, OS, model, date.
- Scratchpad Path – temporary folder for intermediate files.
- Result Clearing – compacting old tool results.
- MCP Instructions – per‑server custom prompts (uncached on purpose).
- Memory Injection – extracted memories from prior conversations.
- User Project Instructions (CLAUDE.md) – the user’s own extensions sit at the bottom, giving them the strongest influence.
By splitting the prompt this way, Claude Code maximises cache hits. Roughly 70 % of the prompt stays cached, while the remaining 30 % updates with fresh context.
Why This Matters for Your Agents
- Put stable text first. If you use Anthropic’s caching, front‑load identity, security, and risk rules.
- Use negative instructions. Telling the model what not to do is more reliable than vague “be concise” advice.
- Give explicit taxonomies. A table of risk levels removes ambiguity when the model decides to run a tool.
- Leverage feature flags. Conditional sections let you toggle token‑budget displays, proactive mode, or memory injection without rewriting the whole prompt.
Apply these patterns to any Claude‑based agent and you’ll see lower token usage, faster responses, and more predictable behavior.
Citations
- “How Claude Code Builds Its System Prompt — 18 Layers You Never See.” codex.cadences.app, https://codex.cadences.app/en/blog/claude-code-system-prompt/ (accessed 2026‑07‑04)
- “System Prompt | Claude Code Internals.” claude-code-explain.helmcode.com, https://claude-code-explain.helmcode.com/system-prompt/ (accessed 2026‑07‑04)
Fact‑check notes
- Verify that the seven static sections listed match the latest source code in
src/constants/prompts.ts. The blog mentions 18 sections overall, but the core static layers collapse to seven when the dynamic boundary is applied. - Confirm the exact wording of the risk taxonomy (LOW/MEDIUM/HIGH) in the current repository version.
- Double‑check the caching marker string:
SYSTEMPROMPTDYNAMIC_BOUNDARY.
Word count: 438