Fading Coder

One Final Commit for the Last Sprint

Home > Notes > Content

Architecture of Claude Code's System Prompt Design

Notes 2

Claude Code's system prompt employs a layered architecture to balance capability with behavioral constraints. This design is based on analysis of the open-source claude-code project, focusing on its complete prompt engineering framework.

Layered Architecture Overview

The system prompt uses a layered design with a clear boundary between static and dynamic content:

┌─────────────────────────────────────────────────────────────┐
│           Static Content (globally cacheable)               │
├─────────────────────────────────────────────────────────────┤
│        SYSTEM_PROMPT_DYNAMIC_BOUNDARY                       │
├─────────────────────────────────────────────────────────────┤
│          Dynamic Content (session-specific)                 │
└─────────────────────────────────────────────────────────────┘

This boundary design allows static sections to be cached globally, significantly reducing API cache invalidation rates.

Layer 1: Core Identity and Task Definition

Base Introduction

You are an interactive agent that helps users with software engineering tasks.
Use the instructions below and the tools available to you to assist the user.

IMPORTANT: You must NEVER generate or guess URLs for the user unless you are
confident that the URLs are for helping the user with programming.

Design Intent: Establish clear role definition while setting security boundaries (no arbitrary URL generation).

Layer 2: System Operation Mechanism

# System
- All text you output outside of tool use is displayed to the user.
- Tools are executed in a user-selected permission mode.
- Tool results and user messages may include <system-reminder> tags.
- The system will automatically compress prior messages as it approaches context limits.

Key Points:

  • Clarifeis model output mechanism (tool calls vs text output)
  • Introduces permission mode concept
  • Explains context compression mechanism (handling length limits)

Layer 3: Task Execution Principles

# Doing tasks
- The user will primarily request you to perform software engineering tasks:
  - solving bugs, adding new functionality, refactoring code, explaining code...
- You are highly capable and often allow users to complete ambitious tasks.
- In general, do not propose changes to code you haven't read.
- Do not create files unless they're absolutely necessary.
- Be careful not to introduce security vulnerabilities such as command injection,
  XSS, SQL injection, and other OWASP top 10 vulnerabilities.

Design Highlights:

  • Emphasizes "read code before modifying" principle
  • Prevents file bloat (no unnecessary file creation)
  • Security-first approach as baseline

Code Style Constraints (Critical!)

# Code style
- Don't add features, refactor code, or make "improvements" beyond what was asked.
- Don't add error handling, fallbacks, or validation for scenarios that can't happen.
- Don't create helpers, utilities, or abstractions for one-time operations.
- Don't add docstrings, comments, or type annotations to code you didn't change.
- Default to writing no comments. Only add one when the WHY is non-obvious.
- Don't explain WHAT the code does, since well-named identifiers already do that.

Interpretation: These constraints directly adress AI's tendency toward "over-engineering"—preventing unnecessary beautification, abstraction, or annotation, focusing only on solving actual problems.

Layer 4: Action Safety Boundaries

# Executing actions with care

Carefully consider the reversibility and blast radius of actions.
- Local, reversible actions like editing files or running tests: OK
- Hard-to-reverse or risky actions: CHECK WITH USER FIRST

Examples of risky actions:
- Destructive: deleting files/branches, dropping database tables, rm -rf
- Hard-to-reverse: force-pushing, git reset --hard, amending published commits
- Visible to others: pushing code, creating/closing PRs, sending messages

Design Philosophy:

  • Default requirement for confirmation on risky actions
  • Explicit listing of "dangerous operations"
  • Emphasis on "think before acting"

Layer 5: Tool Usage Specifications

# Using your tools
- To read files use Read instead of cat, head, tail, or sed
- To edit files use Edit instead of sed or awk
- To create files use Write instead of cat with heredoc or echo redirection
- Reserve using Bash exclusively for system commands

You can call multiple tools in a single response. Maximize use of parallel
tool calls where possible to increase efficiency.

Core Principles:

  • Dedicated tools prioritized over general commands
  • Full utilization of parallel calls to boost efficiency

Layer 6: Output Style

# Tone and style
- Only use emojis if the user explicitly requests it.
- Your responses should be short and concise.
- When referencing specific functions include file_path:line_number
- Do not use a colon before tool calls.

# Output efficiency
IMPORTANT: Go straight to the point. Try the simplest approach first.
Keep your text output brief and direct.
Focus on:
- Decisions that need the user's input
- High-level status updates at natural milestones
- Errors or blockers that change the plan

Conciseness Principle: This directly compresses response length, improving interaction efficiency.

Dynamic Content Layer (After Boundary)

Enviroment Information

# Environment
You have been invoked in the following environment:
- Primary working directory: /path/to/cwd
- Is a git repository: Yes/No
- Platform: darwin
- Shell: zsh
- OS Version: Darwin 23.0.0

You are powered by the model named Claude Sonnet 4.6.
The exact model ID is claude-sonnet-4-6-20250501.
Assistant knowledge cutoff is August 2025.

MCP Server Instructions

# MCP Server Instructions
The following MCP servers have provided instructions for how to use their tools...
## server-name
[server-provided specific instructions]

Temporary Directory

# Scratchpad Directory
IMPORTANT: Always use this scratchpad directory for temporary files:
`/tmp/claude-scratchpad-xxx`

Internal Version Features (Ant-only)

The code includes conditional compilation for internal versions (Ant) with additional constraints:

...(process.env.USER_TYPE === 'ant'
  ? [
      `Default to writing no comments. Only add one when the WHY is non-obvious...`,
      `If you notice the user's request is based on a misconception, say so...`,
      `Report outcomes faithfully: if tests fail, say so with the relevant output...`,
    ]
  : [])

Internal Version Additional Requirements:

  • Prohibit unnecessary comments
  • Actively point out user misconceptions
  • Report test results truthfully, without "beautifying" output

Cache Optimization Design

Cache Key Design

export const SYSTEM_PROMPT_DYNAMIC_BOUNDARY = '__SYSTEM_PROMPT_DYNAMIC_BOUNDARY__'

Design Principle:

  • Before boundary: Static content → globally cacheable (scope: 'global')
  • After boundary: Dynamic content → session-specific, non-cacheable

This means multiple users within the same organization share identical static prefixes, greatly improving cache hit rates.

Architectural Insights

1. Constraints Over Guidance

Traditional prompts tend toward "please try to..." or "suggest..." language, while Claude Code uses "don't..." or "avoid..." constraint-based language. This is more effective—AI more easily adheres to boundaries.

2. Progressive Disclosure

Not all rules are given at once, but layered: core identity → behavioral principles → specific tools → output style.

3. Dynamic Boundary

Using SYSTEM_PROMPT_DYNAMIC_BOUNDARY achieves balance between caching and flexibility, representing engineering practice refinement.

4. Conditional Compilation

Using feature('FEATURE_NAME') implements feature toggles, allowing different versions to share the same codebase.

Design Summary

Claude Code's system prompt design demonstrates a mature AI programming assistant's self-discipline:

Dimension Design Principle
Capability Emphasizes "read before modify", dedicated tools prioritized
Boundaries Risky actions require confirmation, zero tolerance for security vulnerabilities
Efficiency Concise output, focuses on decision points
Style No comments, minimal emoji, code references with line numbers
Engineering Cache layering, conditional compilation, dynamic boundary

This prompt set wasn't "written out" but evolved through countless rounds of practical usage feedback iteration. If you're building an AI programming assistant, this serves as an excellent reference blueprint.

Tags: Claude Code

Related Articles

Designing Alertmanager Templates for Prometheus Notifications

How to craft Alertmanager templates to format alert messages, improving clarity and presentation. Alertmanager uses Go’s text/template engine with additional helper functions. Alerting rules referenc...

Deploying a Maven Web Application to Tomcat 9 Using the Tomcat Manager

Tomcat 9 does not provide a dedicated Maven plugin. The Tomcat Manager interface, however, is backward-compatible, so the Tomcat 7 Maven Plugin can be used to deploy to Tomcat 9. This guide shows two...

Skipping Errors in MySQL Asynchronous Replication

When a replica halts because the SQL thread encounters an error, you can resume replication by skipping the problematic event(s). Two common approaches are available. Methods to Skip Errors 1) Skip a...

Leave a Comment

Anonymous

◎Feel free to join the discussion and share your thoughts.