Claude Code Context Management & CLAUDE.md โ€” From Pitfalls to Infrastructure

Claude Code Context Management & CLAUDE.md โ€” From Pitfalls to Infrastructure

A practical guide to the two biggest Claude Code pain points: context window management and CLAUDE.md bloat. Draws from a widely-shared Chinese-language analysis by ็ˆฑๅฏๅฏ-็ˆฑ็”Ÿๆดป, the open-source Citadel orchestration framework, and community discussions on Reddit about what happens when you stop adding rules and start building infrastructure instead.

*Source: Claude Code Best Practice (shanraisshan) Citadel โ€” Agent Orchestration (SethGammon) Reddit: What happens when you stop adding rules claude-md-management plugin Ole Lehmann: CLAUDE.md Pruning*

Context Management Is the Core Battlefield

The original articleโ€™s opening line: โ€œไธŠไธ‹ๆ–‡็ฎก็†ๆ˜ฏๆ ธๅฟƒๆˆ˜ๅœบโ€ โ€” context management is the core battlefield. Once your context window hits ~50% capacity, Claude starts compressing earlier messages, and quality drops. The key insight:

The smaller each task, the more likely it finishes within 50% context. The smaller the task, the more stable Claude is.

โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
โ”‚           Context Window (200K)              โ”‚
โ”‚                                             โ”‚
โ”‚  โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”  Sweet spot: each task         โ”‚
โ”‚  โ”‚ Task A  โ”‚  completes in <50% context     โ”‚
โ”‚  โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜                                โ”‚
โ”‚  โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”                                โ”‚
โ”‚  โ”‚ Task B  โ”‚  After 50%: compression kicks  โ”‚
โ”‚  โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜  in, Claude loses earlier info โ”‚
โ”‚  โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”    โ”‚
โ”‚  โ”‚ Task C (too large)         โš ๏ธ RISK  โ”‚    โ”‚
โ”‚  โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜    โ”‚
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜

Five Rules from the Trenches

Rule Why
Always start in plan mode Let Claude understand what to do before it touches code. Finish one task, then commit โ€” donโ€™t batch
Use iTerm, not IDE terminals IDE built-in terminals crash more easily with heavy Claude Code sessions
Voice input multiplies efficiency Dictating context is 2-3ร— faster than typing; great for initial specs
Use git worktrees Handle multiple branches simultaneously without context-switching
Run compact manually at ~50% Donโ€™t wait for auto-compression; take control of what gets kept

The Three-Role Architecture

The article describes Claude Codeโ€™s architecture as three distinct roles working together:

โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
โ”‚                Claude Code Architecture           โ”‚
โ”œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ค
โ”‚   Commands   โ”‚    Agents    โ”‚      Skills        โ”‚
โ”‚  (Entry &    โ”‚ (Orchestrate โ”‚  (Domain           โ”‚
โ”‚  Interaction)โ”‚  & Route)    โ”‚  Knowledge)        โ”‚
โ”œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ผโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ผโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ค
โ”‚ CLI input    โ”‚ Subagents    โ”‚ Preloaded specs    โ”‚
โ”‚ Slash cmds   โ”‚ Task routing โ”‚ Workflows          โ”‚
โ”‚ User prompts โ”‚ Parallel ops โ”‚ Best practices     โ”‚
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ดโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ดโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
         โ†“              โ†“              โ†“
    "What to do"   "How to route"  "What to know"

This is a progressive disclosure design โ€” Claude loads domain knowledge only when needed, avoiding context overload from the start.

Debugging: Observe, Donโ€™t Guess

Two underrated debugging techniques from the article:

  1. Watch the terminal as a log observer โ€” Run Claudeโ€™s background task output in a visible terminal. Watching the raw flow reveals patterns youโ€™d miss by just reading the final answer.

  2. Use MCP to connect Claude to your browser โ€” Instead of copy-pasting error messages, let Claude read browser console output directly. Much more efficient than the โ€œcopy โ†’ paste โ†’ describeโ€ loop.

The CLAUDE.md Bloat Problem

The community consensus: bloated CLAUDE.md files are a rite of passage. Everyoneโ€™s first instinct is to add more rules. The result?

  • Claude starts ignoring rules because there are too many
  • Cost per session increases (more prompt tokens loaded every time)
  • Rules contradict each other
  • Adding โ€œdonโ€™t do Xโ€ rules creates a negative instruction spiral

The Solution: Infrastructure, Not More Rules

The Reddit thread captures this insight perfectly: the answer isnโ€™t more rules โ€” itโ€™s building the right infrastructure.

Instead ofโ€ฆ Build thisโ€ฆ
โ€œAlways run tests before committingโ€ A pre-commit hook that runs tests automatically
โ€œUse the right model for simple tasksโ€ An agent layer that routes by complexity
โ€œDonโ€™t exceed budgetโ€ Budget enforcement with automatic model downgrade
โ€œFollow our coding styleโ€ A linter config that Claude reads and respects
โ€œCheck these files before editingโ€ Skills that preload relevant context

The Pruning Methodology

A concrete case study: one developer pruned 60% of their CLAUDE.md rules. The result: response speed improved 40% and hallucinations decreased noticeably.

The step-by-step method:

  1. Run an audit prompt โ€” ask Claude to scan your CLAUDE.md for contradictions. Youโ€™ll discover conflicts you never noticed, like a vague โ€œmaintain natural toneโ€ rule overriding specific formatting rules.
  2. Delete the flagged rules โ€” but donโ€™t blindly delete everything flagged.
  3. Test with your 3 most common tasks โ€” run them after deletion.
  4. Evaluate: output the same or better? โ†’ those rules were dead weight. A feature broke? โ†’ add that one rule back.

The deeper insight:

โ€œThe real skill isnโ€™t knowing rules โ€” itโ€™s knowing what to delete. Your AI settings should get simpler over time. If theyโ€™re getting more complex, youโ€™re using rule accumulation to avoid thinking about โ€˜what do I actually want?โ€™โ€œ

Claude canโ€™t solve this for you. If you donโ€™t know what you want, no amount of CLAUDE.md rules will produce it.

Practical CLAUDE.md Guidelines

Guideline Detail
Keep it under 200 lines For each line, ask: โ€œWould removing this cause Claude to make mistakes?โ€ If not, cut it
Use progressive disclosure Put domain-specific knowledge in skills, not CLAUDE.md
Use sub-folder CLAUDE.md files /frontend/CLAUDE.md, /backend/CLAUDE.md for scoped instructions
Audit regularly Use the claude-md-management plugin (76,000+ installs) to audit quality
Treat it like code Review it, prune it, test changes by observing behavior shifts
Simplify over time Your CLAUDE.md should get shorter as you learn what matters โ€” not longer

Cost Control: The Agent Layer Approach

The article highlights a real-world example: someone burned $15 in 8 minutes because they had no cost controls. Adding โ€œdonโ€™t use Opus for this taskโ€ as a rule doesnโ€™t work โ€” Claude has 30+ model selection rules and ignores them.

The effective approach is an agent layer that:

User Request
    โ†“
โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
โ”‚   Complexity Router  โ”‚
โ”‚                     โ”‚
โ”‚  Simple? โ†’ Haiku    โ”‚
โ”‚  Medium? โ†’ Sonnet   โ”‚
โ”‚  Complex? โ†’ Opus    โ”‚
โ”‚                     โ”‚
โ”‚  Budget remaining?  โ”‚
โ”‚  Yes โ†’ proceed      โ”‚
โ”‚  No  โ†’ downgrade    โ”‚
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
    โ†“
Execution with enforced budget

This moves cost control from โ€œprompt suggestionsโ€ to โ€œinfrastructure decisions.โ€

Citadel: A Full Implementation

Citadel by Seth Gammon is an open-source framework that implements these ideas as a complete system:

Tier Role When to use
Skills Domain experts Single-task, specific knowledge
Marshal Session coordinator Multi-step within one session
Archon Autonomous strategist Cross-session campaigns
Fleet Parallel agents Independent tasks in isolated worktrees

Key features: campaign persistence across sessions, parallel agent coordination with discovery sharing, lifecycle hooks for quality enforcement, and a universal /do router that classifies intent and dispatches to the cheapest capable tier.

The Boris Template: CLAUDE.md as AI Onboarding Manual

A widely-shared infographic (็ˆฑๅฏๅฏ-็ˆฑ็”Ÿๆดป) reframes CLAUDE.md as an AI co-workerโ€™s onboarding manual โ€” not just a config file, but a structured document that lets AI โ€œremember the past, carry out the present, and evolve into the future.โ€

Source: ็ˆฑๅฏๅฏ-็ˆฑ็”Ÿๆดป on Weibo (2026-03)

The Problem It Solves

Without CLAUDE.md:
  Every session starts from zero โ†’ inefficient loop
  โ”‚ User โ†’ explain context โ†’ AI executes โ†’ session ends โ†’ repeat โ”‚

With CLAUDE.md:
  AI reads your "onboarding manual" โ†’ picks up where it left off
  โ”‚ Past errors โ”‚ Project rules โ”‚ Execution rules โ”‚ โ†’ Claude AI

The analogy: CLAUDE.md is a letter to future collaborators โ€” not just a config tool.

Boris Template Design

The Boris template structures CLAUDE.md into task modes:

Mode Purpose When to Use
Simple Task Write spec first, start Quick changes
Complex Analysis Subagent-based Keep context coherent across subtasks
Post-correction Update tasks/lessons.md Donโ€™t repeat mistakes next time
Task Complete Prove completion Donโ€™t just claim done โ€” show evidence
Decision Making Explain trade-offs Not just explain what, explain why not

Evolution Mechanism

The template defines three maturity levels:

Level How CLAUDE.md is Used
Beginner Static config file (copy-paste and forget)
Intermediate Update after each session (effect compounding)
Advanced Dynamic updates via .bashrc + agent hooks (auto-evolution)

Core Principles (from terminal screenshot)

The accompanying terminal screenshot reveals the full operating principles:

  1. Plan Mode Default โ€” Enter plan mode for ANY non-trivial task. If something goes sideways, STOP and re-plan immediately.
  2. Subagent Strategy โ€” Use subagents liberally to keep main context window clean. For complex problems, throw more compute at it via subagents.
  3. Self-Improvement Loop โ€” After ANY correction from user, update tasks/lessons.md with the pattern. Write rules for yourself that prevent the same mistake.
  4. Verification Before Done โ€” Never mark a task complete without proving it works. Run tests, check logs, demonstrate correctness.
  5. Demand Elegance (Balanced) โ€” For non-trivial changes, pause and ask โ€œis there a more elegant way?โ€ But donโ€™t over-engineer.
  6. Autonomous Bug Fixing โ€” Given a bug report, first list the evidence. Point at logs, errors, failing tests โ€” then resolve them. Zero context switching required from the user.

The .bashrc insight: the real nature of CLAUDE.md isnโ€™t to make AI smarter โ€” itโ€™s to make it more like a domesticated role-specific interface for your company, where computing power and knowledge become free-flowing water.

Complete Project Structure โ€” The Directory Cheat Sheet

A visual reference (by Sandipan Bhaumik / agentbuild.ai) showing the full Claude Code project directory structure. Most people only use CLAUDE.md โ€” the real power is in the .claude/ directory.

Source: ้ป˜ๅบตยท่ถ…็บงไธชไฝ“ on Xiaohongshu (2026-04)

your-project/
โ”œโ”€โ”€ CLAUDE.md ยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยท Loaded at session start. Project overview,
โ”‚                               tech stack, coding conventions, architecture.
โ”œโ”€โ”€ CLAUDE.local.md ยทยทยทยทยทยทยทยทยทยทยท Personal preferences. Overrides CLAUDE.md.
โ”‚                               Git-ignored โ€” your private config.
โ”œโ”€โ”€ .mcp.json ยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยท MCP integration configs. Connect to GitHub,
โ”‚                               Jira, Slack, databases. Shared across team via git.
โ”‚
โ””โ”€โ”€ .claude/
    โ”œโ”€โ”€ settings.json ยทยทยทยทยทยทยทยทยท Permissions & tool access. Model selection,
    โ”‚   settings.local.json     hooks config. .local overrides shared settings.
    โ”‚
    โ”œโ”€โ”€ rules/ ยทยทยทยทยทยทยทยทยทยทยทยทยทยทยทยท Modular .md files by topic:
    โ”‚   โ”œโ”€โ”€ code-style.md       - Covers style, testing, API design
    โ”‚   โ”œโ”€โ”€ testing.md          - Can target specific files/paths
    โ”‚   โ””โ”€โ”€ api-conventions.md  - Loaded automatically by Claude
    โ”‚
    โ”œโ”€โ”€ commands/ ยทยทยทยทยทยทยทยทยทยทยทยทยท Custom slash commands (/project-name):
    โ”‚   โ”œโ”€โ”€ review.md           - Used for repeatable workflows
    โ”‚   โ””โ”€โ”€ fix-issue.md        - Supports shell execution
    โ”‚
    โ”œโ”€โ”€ skills/ ยทยทยทยทยทยทยทยทยทยทยทยทยทยทยท Auto-triggered based on task context:
    โ”‚   โ””โ”€โ”€ deploy/             - Loads only when needed
    โ”‚       โ”œโ”€โ”€ SKILL.md        - Keeps context lightweight
    โ”‚       โ””โ”€โ”€ deploy-config.md
    โ”‚
    โ”œโ”€โ”€ agents/ ยทยทยทยทยทยทยทยทยทยทยทยทยทยทยท Specialized sub-agents with roles:
    โ”‚   โ”œโ”€โ”€ code-reviewer.md    - Isolated context windows
    โ”‚   โ””โ”€โ”€ security-auditor.md - Custom tools and model preferences
    โ”‚
    โ””โ”€โ”€ hooks/
        โ””โ”€โ”€ validate-bash.sh ยทยท Event-driven scripts (pre/post tool use).
                                Automate validation, testing, formatting.
                                Block unsafe operations.

What Each Layer Does

Layer Purpose Key Insight
CLAUDE.md Project rules + context The โ€œonboarding manualโ€ โ€” loaded every session
CLAUDE.local.md Personal overrides Git-ignored; your preferences without polluting team config
rules/ Coding standards Modular โ€” split by topic instead of one giant CLAUDE.md
commands/ Repeatable workflows Custom /slash-commands for your teamโ€™s processes
skills/ Domain knowledge Auto-loaded only when relevant โ€” keeps context lean
agents/ Specialized workers Each agent has its own role, tools, and context isolation
hooks/ Automation gates Deterministic enforcement โ€” linters, tests, safety checks

The Real Takeaway

โ€œMost people put one CLAUDE.md and call it done. The real power is the .claude/ directory โ€” rules for standards, commands for workflows, skills for knowledge, agents for delegation, hooks for safety.โ€

Configure once, benefit every session. The more structured your .claude/ directory, the more reliable and consistent Claudeโ€™s output becomes.

How LearnAI Team Could Use This

  • Prune shared CLAUDE.md files using the Boris template: keep under 100 lines, move specifics to sub-folder files.
  • Move deterministic rules into hooks instead of prompt instructions โ€” code enforcement over prompt suggestions.
  • Use the three-role architecture (Researcher โ†’ Planner โ†’ Executor) for complex documentation and wiki update tasks.
  • Audit CLAUDE.md quarterly: delete rules you canโ€™t remember writing, convert verbose instructions into 3-word imperatives.

Real-World Use Cases

  • Running claude-md-management plugin to audit and prune bloated CLAUDE.md files across projects.
  • Setting up Citadel-style agent routing to use cheaper models for simple tasks and Opus for complex reasoning.
  • Converting project-specific rules from global CLAUDE.md into sub-folder .claude/CLAUDE.md files that only load in context.
  • Using hooks for deterministic enforcement (formatting, commit messages, security checks) instead of relying on prompt compliance.

Key Takeaway

The articleโ€™s closing line sums it up: using AI coding tools well isnโ€™t about finding the perfect configuration โ€” itโ€™s about understanding how the tool works, then building systems that flow with it.