System Prompts & CLAUDE.md: The Foundation of Context Engineering

Category: AI Trends

By Isham Rashik

A comprehensive guide to system prompts and CLAUDE.md — covering the five components of a well-structured system prompt, the right altitude principle, iterative construction, file hierarchy, few-shot examples, and the coding agent landscape.

Context Engineering

System Prompts & CLAUDE.md

A comprehensive guide to the foundational layer of context provided to large language models — what goes into a system prompt, how to structure it, and how to engineer it for maximum effectiveness.

Isham Rashik / AI Engineer / March 29, 2026 / 30 min read Overview

This guide dives deep into system prompts: the foundational layer of context provided to large language models. In Claude Code, the primary vehicle for system prompts is the CLAUDE.md file.

It covers the five components of a well-structured system prompt (identity, rules, format, knowledge, tools), the "right altitude" principle for writing instructions, iterative construction methodology, selective retrieval of sections, the file hierarchy (CLAUDE.md, AGENTS.md, SKILL.md, SOUL.md), few-shot example strategies, and the coding agent landscape including Claude Code, Google Antigravity, Cursor, and Copilot.

Building on the six-layer context model from the companion guide on context window fundamentals, this guide focuses entirely on Layer 1: the system prompt.

01 — Context Layers

Recap: The Six Layers of Context

The six elements that constitute the context provided to an LLM are:

  1. System Prompt — rules, guardrails, identity (CLAUDE.md / AGENTS.md)
  2. User Instructions — the direct prompt or query from the user
  3. Tools — functions, APIs, libraries accessible to the model (MCP)
  4. Memory / State — persistent knowledge (preferences, historical patterns)
  5. RAG — dynamically fetched data from knowledge bases
  6. Conversation History — the current session's message exchanges

These six elements are not listed in any particular priority order. The order in which they appear in the context window is what matters.

Memory vs. Conversation History

A common point of confusion: memory and conversation history are not the same thing.

AspectConversation HistoryMemory / State
ScopeCurrent conversation thread onlyAcross all conversations and sessions
LifespanDuration of one sessionWeeks, months, or indefinitely
ContentThe 25 email exchanges in a threadYour email preferences over the past year
StorageContext windowMemory files, databases, knowledge bases

Concrete example: An email agent responding to a thread. The 25 prior exchanges in that specific thread form the conversation history. How the user has replied to technical vs. non-technical emails over the past year, distilled from 5,000–10,000 email exchanges, forms the persistent memory/state.

Context Ordering and the Lost-in-the-Middle Effect

The context window has a well-documented attention bias. LLMs give the most prominence to the beginning and end of the context window. Content in the middle receives slightly less attention: this is the "lost in the middle" effect.

Practical Guideline

Even if an LLM supports 1–2 million tokens as its context window, try to keep your actual context length to 50,000–100,000 tokens. Larger contexts amplify the lost-in-the-middle effect.

02 — Anatomy

Anatomy of a System Prompt (CLAUDE.md)

A CLAUDE.md file typically contains five components. These form the skeleton of any well-structured system prompt.

Component 1 Identity What this project IS. One paragraph · fewest tokens. Component 2 Rules HOW it should behave. Guardrails · highest priority. Component 3 Format Output structure. JSON · code · folder layout. Component 4 Knowledge WHAT reality looks like. Env vars · tech stack · phases. Component 5 Tools WHAT it can access. APIs · libraries · functions.

Identity

Identity is the first thing written in CLAUDE.md and often the least important in terms of behavioral impact. It describes what the project is — not in anthropomorphic terms ("you are a senior code reviewer"), but in functional terms.

Key Point

Identity does not mean "who the LLM is." It means what the thing you are building is meant to do. Avoid giving human-like personas unless specifically needed.

Token characteristics: Identity is typically one paragraph — it consumes the fewest tokens of all five components. If forced to cut sections, identity is the safest to remove first because the model can often infer its role from the rules, examples, and conversation context.

Rules

Rules define how the model should or should not behave. These are the guardrails — the component that most directly determines whether the system produces correct, safe, and consistent outputs.

Priority: Rules are the highest priority component. Even under extreme token constraints, rules should be the last component removed. Rules must be loaded into the context for every single API call because guardrails should never be broken.

Rule of Thumb

If it says "never do X" or "always do Y": it is a rule. If it describes what exists or how things are structured: it is knowledge.

Format

Format specifies how the LLM should structure its output — independently of what it says or how it behaves. This includes output format (Python, JSON, CSV), project folder structure, and development commands.

src/
├── calculator.py # calculate_final_grade function
├── models.py # data classes
├── utils/
└── tests/

Knowledge

Knowledge describes what the reality of the application is: factual information the model needs to do its job — not instructions on how to behave, but the facts it must know to apply those instructions correctly.

Tools

Tools define what the model can access: APIs, libraries, packages, and functions the model is permitted and expected to use.

Blurred Boundaries Between Components

The boundaries between components are fluid, not rigid:

BoundaryExampleWhy It Is Ambiguous
Rules ↔ Knowledge"Start with cosine similarity"Is this a rule (how to do retrieval) or knowledge (what method to use)?
Knowledge ↔ ToolsTech stack listing Python, ChromaDBDefines both what tools to use AND knowledge about the stack
Rules ↔ FormatDevelopment commands (npm run dev)Specifies both how to run the project AND the format of operations
Knowledge ↔ FormatProject folder structureDescribes what exists AND specifies output structure
Info

Context engineering is not physics: it is more like a collection of thumb rules that evolved from experimentation by large companies and communities. The boundaries between knowledge, rules, and format are deliberately soft.

03 — Right Altitude

Right Altitude Principle

Every instruction in CLAUDE.md has a cost: it consumes tokens, adds noise, and competes with every other rule for the model's attention. Anthropic's documented guidance is direct — keep CLAUDE.md short and human-readable, and apply a single test to every line:

The Test

Would removing this line cause Claude to make mistakes? → Keep it. It encodes something the model cannot infer on its own.

Would removing this line make no difference? → Cut it. Bloated CLAUDE.md files cause Claude to ignore your actual instructions.

This maps onto two failure modes: instructions too vague to act on, and instructions so specific they break the moment the world changes.

DomainToo High (Vague)Too Low (Brittle)Right Altitude
Pricing Be helpful with pricing questions Reply: Plans start at $9.99/month Check the pricing database first. If no exact match, suggest the closest plan and explain the difference.
Errors Handle errors gracefully If get_orders returns 404, say 'Order not found' When a tool call fails, explain what happened in plain language, suggest one alternative action, and offer to escalate to a human.
Tone Be professional Always start with 'Thank you for reaching out!' Use a warm but professional tone. Mirror the user's formality level. Avoid jargon unless the user uses it first.
Code style Write clean code All variables must use snake_case. All functions must have exactly one return statement. Follow PEP 8. Use type hints on all function signatures. Functions should be under 30 lines. Use descriptive names.
Common Mistake

Saying "do not hallucinate" is the most vague thing you can ever write in a system prompt. It provides zero actionable guidance to the model.

Why Brittle Instructions Fail

CLAUDE.md has a lifespan of weeks to months. Hardcoding specific prices, error messages, or API responses means the file becomes stale the moment any detail changes.

What to Include vs. Exclude

Include

Exclude

Treat CLAUDE.md Like Code

Review it when things go wrong, prune it regularly, and test changes by observing whether Claude's behaviour actually shifts. If Claude keeps violating a rule despite it being written down, the file is probably too long and the rule is getting lost in the noise. Add emphasis — IMPORTANT or YOU MUST — for rules that must not be ignored.

04 — Formatting

System Prompt Formatting: XML vs. Markdown

FormatStructureModel Preference
XML<identity>...</identity> tagsClaude reportedly prefers XML based on documentation
Markdown# Identity with headings/subheadingsMore universal, works well with all models
Recommendation

Use Markdown for CLAUDE.md. The difference in output quality between XML and Markdown is not perceivable in practice. Markdown is also the natural choice if you ever write system prompts for other platforms (Cursor, Copilot, Windsurf), since those tools use their own Markdown-based config files.

Markdown heading hierarchy enables section-based retrieval:

Each heading creates a natural chunk boundary that can be independently retrieved. This property becomes critical as CLAUDE.md files grow larger — Markdown structure is what makes selective retrieval possible at all.

05 — Iterative Build

Iterative Construction: Start Minimal, Then Add

The temptation when writing CLAUDE.md is to define every rule upfront. Anthropic's recommendation is the opposite: start with the minimum viable system prompt and add rules only when observed failures demand them.

1 Minimal Identity Score: 3/10

"You are a customer support agent for ShopCo." Run on real tasks. Failure: model hallucinates return policy.

2 Add Tool Usage Rule Score: 5/10

"Use provided tools for actions. Do not guess." +Anti-hallucination. Failure: approves refund outside window.

3 Add Policy Constraint Score: 7/10

"Returns must be within 30-day window." +Empathy-first, de-escalation. Failure: routes damaged item as standard return.

4 Add Edge Case Routing Score: 9/10

"Damaged items require different routing." +Edge cases, routing rules. Result: all tests pass within ~200 token budget.

Each failure reveals a missing rule or piece of knowledge. The scores progress from 3/10 to 9/10 over four rounds, with the total prompt growing from ~30 tokens to ~200 tokens. Every rule earns its place by fixing a real failure, not through speculation.

Avoiding Contradictions

The primary danger of writing a comprehensive system prompt in one shot is self-contradiction.

How Contradictions Happen

When defining 10–20 rules at once, some rules may conflict. The model then picks arbitrarily, and picks wrong.

Rule: Refund window must be within 30 days
User preference: If the case is legitimate, always provide the refund
Conflict: What if the case is legitimate but it is day 45? The model cannot satisfy both rules simultaneously.

Memorize

The least you can do is either iterate and build, or build the CLAUDE.md file and read through it once properly to catch contradictions.

06 — Workflow

Construction Workflow: From Raw Ideas to CLAUDE.md

Capturing requirements is often the hardest part. A three-step workflow bridges the gap between scattered project knowledge and a well-structured system prompt.

1 ideas.md — Brain Dump

Use a voice-to-text tool (e.g., Wispr Flow) to rapidly dump all requirements. This file is intentionally unstructured.

2 ideas_v2.md — Structured Intermediate

Ask Claude to restructure the raw ideas into a well-organized markdown document with headings and subheadings. This is NOT the CLAUDE.md yet.

3 CLAUDE.md — Final System Prompt

Ask Claude to create the CLAUDE.md file using ideas_v2.md as the source. The CLAUDE.md references ideas_v2.md for full project context.

Why not go directly from ideas.md to CLAUDE.md?

The unstructured nature of ideas.md would produce a poorly structured CLAUDE.md. The intermediate step ensures proper organization before the system prompt is generated.

Lifespan and Stability

Unlike most project files, CLAUDE.md is not meant to evolve continuously. Its lifespan is weeks to months. It should not change once finalized. The basic ideas and rules it encodes should remain constant throughout the project lifecycle. If something needs to change frequently, it probably belongs in a different file (SKILL.md, feedback files, etc.).

Size Guidelines

MetricRecommendation
Lines200–500 lines (not more)
Words~1,000–3,000 words
Tokens~1,000–3,000 tokens
Maximum before concern5,000+ lines is too large: selective retrieval becomes critical
Info

CLAUDE.md is not entirely loaded all at once. The most important parts (especially rules) are always loaded. Remaining sections are loaded on demand using keyword matching or RAG.

What if your system prompt hits 5,000+ tokens?

Split it into persistent memory and dynamic RAG-based retrieval. Keep rules and identity in the always-loaded persistent layer. Move knowledge sections and few-shot examples into retrievable chunks — only what is relevant to the current task gets loaded. This is not a workaround; it is the intended architecture.

07 — Retrieval

Selective Section Retrieval from CLAUDE.md

Not every section of CLAUDE.md is relevant to every API call. The system selectively retrieves sections based on the current task.

Example: A CLAUDE.md with 12 sections totaling ~1,300 tokens. When the task is "write unit test for user service," only 4 relevant sections are retrieved (testing standards, error handling, naming conventions, data validation): consuming ~440 tokens instead of the full 1,300 — a 65% savings.

RAG-Based vs. Keyword-Based Retrieval

MethodHow It WorksWhen Used
Keyword MatchingMatch keywords in user query against section headings/contentDefault in Claude Code; simpler and faster
RAGChunk → embed → store in vector DB → compare query vector with chunk vectors → retrieve top-KWhen explicitly configured; better for large/complex files
Claude Code

In Claude Code, section retrieval is handled automatically by the coding agent. You do not need to implement retrieval yourself. It is mostly done via keyword matching, not full RAG.

Implication for Markdown structure: Headings (#, ##, ###) create natural chunk boundaries. Each section under a heading becomes a retrievable unit. This is why Markdown structure matters: it directly enables selective retrieval.

08 — File Hierarchy

File Hierarchy: CLAUDE.md, AGENTS.md, and SKILL.md

CLAUDE.md — Four Scoping Levels

LocationScopeLoading Behavior
~/.claude/CLAUDE.mdAll sessions, every projectAlways loaded at launch
./CLAUDE.md (project root)Current project onlyAlways loaded at launch; check into git to share with team
Parent directoriesInherited by all childrenLoaded in full at launch (useful for monorepos)
Child directoriesOnly when working in that directoryLoaded on demand when Claude reads files in that subdirectory

CLAUDE.md also supports importing other files using @path/to/import syntax, for example: @README.md, @docs/git-instructions.md, or @~/.claude/my-project-instructions.md.

Strategy: If a preference is universal (web development patterns, coding standards), put it in the global CLAUDE.md. If it is project-specific (writing style for one product), put it in the project CLAUDE.md. Use child-level files for subdirectory-specific rules in monorepos.

AGENTS.md — Sub-Agent Definitions

AGENTS.md is an open standard released by OpenAI in August 2025 and now governed by the Linux Foundation's Agentic AI Foundation (AAIF). It has been adopted by over 60,000 open-source projects and supported by major agent frameworks including Codex, Cursor, Devin, Gemini CLI, GitHub Copilot, Jules, and VS Code.

Key differences from CLAUDE.md:

SKILL.md — Capability Definitions

SKILL.md files define specific skills the model should have for a domain: front-end development preferences, API design patterns, writing style preferences, and more.

Lifespan: Hours to days. SKILL.md files can be heavily modified as more features or specifications are added.

SOUL.md

SOUL.md is a special file (not Claude Code specific) that defines the personality and character of an agent.

The difference between your output when you have a SOUL.md versus when you don't have a SOUL.md is night and day.

— Dr. Sreedath Panat, Course Instructor

Hierarchy Resolution and Override Rules

When multiple AGENTS.md files exist in a nested hierarchy:

  1. All AGENTS.md files are loaded and considered
  2. If there is a conflict, the deeper (more specific) file's rules override the parent's rules
  3. Non-conflicting rules from all levels are merged and used together
CSS Analogy

Like CSS specificity: more specific selectors override general ones, but non-conflicting styles from all levels apply.

repo-root/
 AGENTS.md <-- Read first (project-wide rules)
 src/
 AGENTS.md <-- Read second (src-specific rules)
 api/
 AGENTS.md <-- Read third (api-specific rules)

Codex assembles: root rules + src rules + api rules
= complete instruction chain for a file in src/api/
09 — Context Isolation

Sub-Agents and Context Window Isolation

A critical architectural question: do sub-agents share their parent's context window?

Answer: No. Each sub-agent has its own context window.

Rationale:

What IS shared: Certain items like CLAUDE.md are accessible to all agents. Feature specifications may also be shared. But the context window itself is isolated.

10 — Lifespan Hierarchy

Context Lifespan Hierarchy

Different elements of context have vastly different lifespans. The context stack operates like an operating system: Layer 1 is the kernel (always running), Layer 5 is the application (comes and goes).

Layer 1 CLAUDE.md / AGENTS.md Weeks to Months Layer 2 Feature Specs (INITIAL.md) Hours to Days Layer 3 Implementation Plans Hours to Days Layer 4 Examples Folder Days to Weeks Layer 5 Dynamic Tool Results / RAG Seconds

The more foundational a piece of context is, the longer it should live and the more carefully it should be engineered. CLAUDE.md is the longest-lived and therefore deserves the most deliberate design.

Fixing Context Rot

When chatbot performance degrades over a long session, two interventions work: periodically reinject key system prompt rules mid-conversation, or add targeted few-shot examples that directly address the observed degradation.

11 — Few-Shot Examples

Few-Shot Examples in System Prompts

Providing examples in the system prompt significantly improves output quality. There are three types of examples based on task complexity.

Examples Beat Abstract Instructions

When a system prompt says "be formal and professional" but the few-shot examples demonstrate a casual, friendly tone, the model follows the examples — not the instruction. Concrete demonstrations of desired behaviour carry higher weight than abstract directives. This means poorly chosen examples can silently override your written rules.

1. Classification Tasks: Input/Output Pairs

The simplest case: a few input/output pairs teach the mapping.

Example 1: [email about meeting invitation] → Primary
Example 2: [email about LinkedIn notification] → Social
Example 3: [email about software update] → Updates

Provide up to 3 diverse examples: one per category if possible.

2. Reasoning Tasks: Chain-of-Thought Examples

When tasks require policy application or multi-step reasoning, the model needs to see how to reason through the problem.

Input: "Customer ordered laptop 45 days ago, wants return"
Reasoning: 45 days > 15-day return window → not eligible
Output: "Return not eligible — outside 15-day return window"

The reasoning step shows the model how to think, not just what to output.

3. Structured Output Tasks: Prefix/Suffix Examples

For tasks requiring specific output formats, provide a complete example of the expected structure:

{
 "order_id": "3301",
 "status": "eligible_for_return",
 "reason": "Within 30-day window",
 "next_steps": ["Initiate return label", "Schedule pickup"]
}

Static vs. Dynamic Example Selection

StrategyDescriptionProsCons
No examplesSystem prompt onlyMinimal token usageWorst performance
Static examplesSame 3 examples every callSimple to implementWastes ~30% tokens on irrelevant examples
Dynamic examplesSelect examples matching current queryRelevant context; efficientRequires retrieval mechanism

Dynamic selection outperforms static because only relevant examples are loaded, examples match the current query's domain, and token budget is spent on useful context.

The Diversity Principle

When selecting few-shot examples, the most important criterion is not relevance alone: it is diversity across categories.

Diversity Trap

If three examples are positive, neutral, negative, then adding two more positive examples means you now have three positive, one neutral, and one negative. The diversity suffers and the model becomes biased toward the overrepresented category.

12 — Coding Agents

Coding Agents, LLMs, and Platform Configuration

A frequently confused distinction:

ConceptExamplesWhat It Is
LLMOpus, Sonnet, Haiku, GPT-4, Gemini, QwenThe underlying model that generates text
Coding AgentClaude Code, Codex, Cursor, Copilot, Windsurf, AntigravityA tool built ON TOP of LLMs that provides an agentic development experience
Claude Code Anthropic Built on Claude (Opus / Sonnet / Haiku). Terminal CLI interface. CLAUDE.md Codex OpenAI Built on OpenAI LLMs. Terminal / IDE interface. Markdown files Google Antigravity Google Agent-first VS Code fork. Built on Gemini 3. Launched Nov 2025. GEMINI.md Cursor Anysphere Model-agnostic. IDE (VS Code fork). .cursor/rules GitHub Copilot Microsoft / GitHub Model-agnostic. IDE plugin. copilot-instructions.md Windsurf Codeium Model-agnostic. IDE (VS Code fork). .windsurfrules CLAUDE.md is Claude Code Only

CLAUDE.md is not a universal configuration file. It is read exclusively by Claude Code. Other coding assistants do not recognise it. If you switch platforms, you must rewrite your configuration using that platform's equivalent file. AGENTS.md is the closest thing to a universal standard.

13 — Good vs. Bad

Poorly Designed vs. Well-Designed CLAUDE.md

Poorly Designed

Well-Designed

The model can learn code patterns and naming conventions by reading source files — but it cannot learn "never use raw SQL here: always use the query builder" or "don't use floating-point for financial calculations." This tribal knowledge (knowledge that exists only in developers' heads and code review comments) is what the anti-pattern section encodes. No amount of code reading will surface it.

Here is an example of a well-designed CLAUDE.md:

# Identity
Customer support agent for ShopCo. Handles return requests,
order status inquiries, and billing disputes via chat.

## Rules
- NEVER approve a return outside the 30-day window
- Always consult the return policy document first
- Damaged items route to escalation queue
- Use snake_case for all variable and function names

## Format
- API responses: JSON with { status, reason, next_steps }
- Build: `npm run build` | Dev: `npm run dev`
- Tests: `npm run test -- --watch`

## Knowledge
- Tech stack: Next.js 14, Tailwind CSS, Supabase, Vercel
- Current phase: Phase 2 (multi-user, launched Jan 2026)
- Return window: 30 calendar days from delivery date

## Tools
- Supabase client for all database operations
- Resend for transactional email
- Sentry for error tracking

## Common Mistakes to Avoid
- Do NOT use raw SQL: always use the Supabase query builder
- Do NOT use floating-point for financial calculations
- Do NOT hardcode the return window (30 days)

Key Takeaways


Notes by Isham Rashik · AI Engineer

Based on the Context Engineering course by Dr. Sreedath Panat and Vizuara Technologies.

Version v1.0 · March 29, 2026

Read the full article on Garage Labs Tech — India's applied AI education platform. Explore our AI courses and programmes.