The Research Report
10 Production Prompt Engineering Patterns From the Claude Code Leak
The Claude Code leak dropped a bomb of insider knowledge on March 31 2026. Anthropic’s internal codebase went public for a few minutes before DMCA takedowns. In that raw dump were the system prompts that power the Claude Code agentic coding assistant. By parsing the TypeScript source and the embedded prompt fragments, I pulled out ten repeatable patterns that any developer can copy into their own LLM pipelines.
This article walks through how those patterns were extracted, what each one looks like in Python, and why they matter for anyone building multi‑step agents.
How the patterns were found
- Grab the source – The leak was a 512 K line npm package. I cloned the repo, stripped out the generated JS, and searched for large string literals that were later concatenated into the system prompt.
- Map the hierarchy – Claude Code builds its prompt in stages: a static “identity” block, a dynamic “conditional” block, then a series of optional sections guarded by feature flags. The hierarchy appears as a nested
if/elsetree in the code. - Label each block – By reading the comments next to each block (e.g., “Security guardrails”, “Tool instructions”) I gave each piece a descriptive name.
- Extract Python skeletons – I rewrote each block as a small function that returns a string. The repo ships these ten functions under
patterns/. - Validate against the agent – Running the examples in
examples/reproduces the exact system prompt Claude Code uses in production, confirming the patterns are faithful.
All ten patterns live in the open‑source repo [miloudbelarebia/claude‑code‑prompt‑engineering‑patterns][1] and can be dropped into any LLM workflow with a single import.
The ten production‑grade patterns
1. Modular Prompt
Claude Code separates the prompt into reusable sections that can be assembled at runtime. The Python version builds a dictionary of named chunks (identity, security, tool_instructions) and joins them with a newline. This allows you to cache the static chunks and only recompute the dynamic ones.
def modular_prompt(sections):
return "\n".join(sections.values())
Why it matters: You can version each chunk independently, roll out updates without touching the whole prompt, and keep the static part cached across users – a 90 % cost saving reported in the repo [2].
2. Conditional Behavior
The system prompt branches on the user’s context. Internal users get verbose explanations and stricter safety checks; external users get the “be concise, save tokens” mode.
if user == "internal":
prompt += "Add anti‑hallucination rules.\n"
else:
prompt += "Be concise.\n"
Why it matters: A single prompt can serve many personas without cluttering the core logic.
3. Security Guardrails
A one‑paragraph paragraph defines the entire cyber‑security boundary. Anything outside that paragraph is automatically rejected.
“Assist with authorized security testing, defensive security, CTF challenges, and educational contexts. Refuse requests for destructive techniques, DoS attacks, mass targeting, supply chain compromise, or detection evasion for malicious purposes.”
Why it matters: Centralizing policy avoids accidental policy drift across many prompt fragments [3].
4. Agent Specialization
Claude Code spawns specialized sub‑agents (Planner, Explorer, Verifier) and never runs tools in the coordinator. The coordinator only delegates tasks.
COORDINATOR → PLAN → SPAWN → {EXPLORE, GENERAL, VERIFY}
Why it matters: Separation of concerns keeps each agent’s prompt short, reduces token usage, and improves failure isolation.
5. Anti‑Hallucination
Before reporting success, Claude Code runs a verification sub‑agent that checks the output against the tool’s real result.
def verify(output, tool_output):
return output == tool_output
Why it matters: Guarantees the agent only claims what it actually proved, a practice that beats “always say it worked” heuristics.
6. Attribution Control (Undercover Mode)
When the assistant works on a public repository, it activates an “undercover” instruction that strips any internal identifiers and hides the AI’s identity.
“You are operating UNDERCOVER in a PUBLIC repository. Your commits MUST NOT contain ANY Anthropic‑internal information. Do not blow your cover.”
Why it matters: Prevents accidental leakage of internal codenames or model versions into open‑source code [4].
7. Feature Flags
Claude Code hides 30+ unreleased capabilities behind compile‑time flags (KAIROS, PROACTIVE, COORDINATOR_MODE, etc.). Each flag gates an entire block of the system prompt.
if flags.get("KAIROS"):
prompt += "Activate autonomous mode.\n"
Why it matters: You can experiment with new behaviours without redeploying the whole model.
8. Prompt Caching Boundary
Everything above the SYSTEMPROMPTDYNAMIC_BOUNDARY is cached globally. The repo measures a 94 % cost reduction: cached text costs $0.30 / M tokens versus $5.00 / M for uncached text.
Why it matters: Large language models charge per token; caching the static prompt halves the bill for high‑throughput services.
9. Output Control
Claude Code forces a token budget on each tool call and caps the surrounding narrative to 25 words. The Python helper trims any section that would exceed the budget.
def trim_to_budget(text, max_tokens=25):
# simple token estimator
return " ".join(text.split()[:max_tokens])
Why it matters: Enforces predictable latency and price per request.
10. Tool Instructions
The prompt explicitly prefers built‑in utilities (Read instead of cat, Edit instead of sed). This teaches the model to use safe, sandboxed primitives.
“When you need to read a file, use the Read tool. Avoid raw shell commands.”
Why it matters: Reduces the attack surface for command‑injection style exploits.
Putting the patterns together
The repo’s examples/buildsystemprompt.py script stitches all ten functions into a single system prompt. Running it yields a prompt that looks exactly like the one Claude Code sends to its model in production. The script also demonstrates how to toggle feature flags, switch user contexts, and enable caching.
python3 examples/build_system_prompt.py \
--user external \
--flags KAIROS,PROACTIVE \
--cache-on
The output is a ready‑to‑drop string you can pass to openai.ChatCompletion or any compatible API.
Practical takeaways for prompt engineers
- Treat prompts as code: store each block in version control, run unit tests, and bump the version when you change a block.
- Cache aggressively: isolate the static part of your prompt and reuse it across requests.
- Guard your safety policy: keep the security paragraph in a single place and reference it everywhere else.
- Use feature flags: roll out experimental behaviours behind switches, then flip them on when you’re ready.
- Separate agents: design a coordinator that only routes work; give each worker a thin, purpose‑specific prompt.
- Verify before you claim: add a verification step that checks tool results against the model’s answer.
By adopting these patterns you move from ad‑hoc prompt tinkering to a production‑ready pipeline. The result is cheaper, safer, and easier to maintain—exactly what any engineering team needs when building agentic systems at scale.