最近書いた記事の知見を活かして自分のCLAUDE.mdを最小化してみたところ、わずか5行にできた。
長らくKent BeckのCLAUDE.mdをコピペ+αを使っていたが、TDDの方法論などをskillsなどに分離した形だ。
Kent BeckのCLAUDE.md単体で見れば、たったの1行になったので、その方法を紹介する。
完成したCLAUDE.mdは以下の通りだ。単体でも使えるがskillsなどと合わせて真価を発揮するので、成果物はGitHub上の設定を参考にしてほしい(https://github.com/atusy/dotfiles/blob/6c080e07db493f6ddccd6072c4bdfbbe6ef3801b/dot_claude)。
# CORE PRINCIPLES
* Follow Kent Beck's Test-Driven Development (TDD) methodology as the preferred approach for all development work.
* Document at the right layer: Code → How, Tests → What, Commits → Why, Comments → Why not
* Keep documentation up to date with code changes最適化を進める上で最近書いた以下の記事をぜひ参考にしてほしいが、読んでなくても最適化は実践できる。
Claude Codeのユーザー設定プロンプトを使い分けてコンテキスト管理を最適化する (CLAUDE.md, rules, slash commands, skills, subagents)
最適化手順は以下の通りだ。詳細は後述する。
- Claude Codeの設定最適化ルールの用意
- ルールを
~/.claude/rules/claude/config-maintenance.mdに保存 - 参考: https://github.com/atusy/dotfiles/blob/fa7923c0dba20281441ca951cdec3cea2ff4e62a/dot_claude/rules/claude/config-maintenance.md
- ルールを
- Claude CodeにCLAUDE.md最適化を実行させる
- プロンプト:
Minimize startup context by refactoring @.claude/CLAUDE.md with excellent extractions of path-specific rules, commands, skills, and/or subagents.
- プロンプト:
- 手直し
最適化で生まれたスキルやPath-specific Rulesの読み込みは初回必要時だけでなく、毎回のようなので、指示忘れ対策としても有効そうだ。
ではでは、詳細を見ていこう。
Claude Codeの設定最適化ルールの用意
以下からダウンロードして利用可能だ。ダウンロードしたら~/.claude/rules/claude/config-maintenance.mdに保存しよう。
Claude Codeの設定変更時のみ読み込まれるよう、paths: "**/{.,dot_}claude/**" を指定しているので、コンテキスト汚染の心配もない。
内容のベースは「Claude Codeのユーザー設定プロンプトを使い分けてコンテキスト管理を最適化する (CLAUDE.md, rules, slash commands, skills, subagents)」になっている。
これをClaude Codeに理解させた上でCreate a rule for Claude Code configuration maintenance的な指示を与えて原案を生成した。
その後、Claude Codeを使ったり手作業したりしつつ、以下の修正を加えた。
- 原案にはディレクトリ構造など、Claude Code自身が熟知してそうな内容を含んでいたので削除
- CLAUDE.mdが含むべき内容が役割(role)、経験( expertise)、核たる指針( core principles)である旨を強調
- 上記に関連するskillsを積極利用するよう指示
CLAUDE.mdの最適化実行
ためしにKent BeckのCLAUDE.mdを最適化してみたところ、77行から10行にまで削減できた。
Kent BeckのCLAUDE.mdは以下のURLから確認できるが、TDDの詳細やコミット方法、計画の実行方法などが盛り込まれており、commandsやskillsに分割できる内容が多く丁度いい題材だった。
https://github.com/KentBeck/BPlusTree3/blob/main/rust%2Fdocs%2FCLAUDE.md
実は、この最適化実行を何度か試しながら、ルール自体も改善している。
プロンプトには単純に「リファクタしてくれ」とか「最小化してくれ」とか言ってもそれなりにやってくれるが、目的と手段を明確に伝えたほうが良い結果が得られやすいと思う。
今回であれば「起動時のコンテキストを最適化する」目的のために「Skillsなどを抽出する」手段をとってくれ、といった具合だ。
最終的に使ったプロンプトは以下。これ自体をslash commandにしてもよさそうだ。
Minimize startup context by refactoring @.claude/CLAUDE.md with excellent extractions of path-specific rules, commands, skills, and/or subagents.
最適化過程の確認
プロンプトを与えると、先に作成したルールがLoaded .claude/rules/claude/config-maintenance.mdとして読み込み、分析を始めてくれる。
> Minimize startup context by refactoring @.claude/CLAUDE.md with excellent extractions of path-specific rules, commands, skills, and/or subagents.
⎿ Read .claude/CLAUDE.md (77 lines)
⎿ Loaded .claude/rules/claude/config-maintenance.md
⏺ I'll analyze your current CLAUDE.md and restructure it following the configuration maintenance principles. Let me fir
st explore what's already in your .claude directory.しばらくすると、最適化戦略としてslash commandやskillsの抽出を計画してくれる。
⏺ ★ Insight ─────────────────────────────────────
Context Optimization Strategy:
1. CLAUDE.md should contain only the role identity and pointers to skills/commands
2. /go command - Triggered explicitly by user saying "go", handles the plan.md workflow
3. tdd skill - Auto-triggers when writing tests or implementing features, contains methodology
4. commit skill - Auto-triggers when committing, contains commit discipline
5. Path-specific rule - You already have code-quality.md with paths filter - this is good!
─────────────────────────────────────────────────そのまま進むと、skillやcommandを切り出した旨を報告してくれる。
コード品質に関するルールも切り出してくれている。パス限定ルールとしてよくあるプログラミング言語の拡張子に反応するようだが、自分の使う言語に反応しない場合は手直しするか、Skillに移すとよいだろう。
⏺ ★ Insight ─────────────────────────────────────
What Changed:
- Before: 77 lines loaded at every startup (3,450 bytes)
- After: 10 lines loaded at startup (~300 bytes) — 91% reduction
Context Loading Strategy:
| Component | When Loaded | Content |
|---------------------------------------------------|--------------------------|----------------------------------------------------|
| CLAUDE.md | Always | Role identity + skill/command discovery (10 lines) |
| rules/code-quality.md | When touching code files | Quality standards (15 lines, path-filtered) |
| skills/tdd/ | Auto, when writing tests | Full TDD methodology (description only at startup) |
| skills/commit/ | Auto, when committing | Commit discipline (description only at startup) |
| commands/go.md | User-invoked /go | Plan.md workflow (never loaded until used) |
| ───────────────────────────────────────────────── | | |最終的に得られたCLAUDE.mdは以下の通りだ。CLAUDE.mdは10行と主張しているが、実質9行である。
# Role
Senior software engineer following Kent Beck's Test-Driven Development (TDD) and Tidy First principles.
## Quick Reference
- Use `/go` command to advance through plan.md tests (Red → Green → Refactor cycle)
- `tdd` skill auto-triggers for test writing, implementation, and refactoring guidance
- `commit` skill auto-triggers for commit discipline and change separation試しにPythonでFizzBuzzを実装させたところ、適切にTDDスキルを使ってくれた。
> implement FizzBuzz in Python
⏺ Skill(tdd)
⏺ I'll implement FizzBuzz using TDD principles - starting with a failing test, making it pass, then refactoring. Let me
set up a todo list to track our Red → Green → Refactor cycles.手直し
わずか1プロンプトで77行を10行に最適化できたが、好みで手直ししてもいいだろう。
たとえばKent BeckのCLAUDE.md最適化は/goコマンドを抽出した。これはplan.mdの内容を実行してねとユーザーが指示するために使うもので、Claude Codeが存在を知る必要はないので省略できる。
commitスキルについても作業としてcommitが必要になれば自動で見つけてくれると期待できそうだ。
一方で、TDDスキルは、ただ「実装して」と指示するだけだとTDD以外の手法を使う可能性があるので残したほうがいい。
上記をふまえると、私のCLAUDE.mdに含まれる1行に集約できる。
やった!たったの1行だ!
- Follow Kent Beck’s Test-Driven Development methodology (
tddskill) as the preferred approach for all development work.
実際にはKent BeckのCLAUDE.mdで言及していない部分に軽く触れておきたいので、t-wadaさんのコードにはHowを……の一文と、ドキュメント更新について追記し、CORE PRINCIPLESセクション化している。いずれもスキル読み込みを問わずして意識してほしい内容だ。
- Document at the right layer: Code → How, Tests → What, Commits → Why, Comments → Why not
- Keep documentation up to date with code changes
ENJOY!
「Claude Codeのユーザー設定プロンプトを使い分けてコンテキスト管理を最適化する (CLAUDE.md, rules, slash commands, skills, subagents)」の記事の知見を活かして、1プロンプトでかなりCLAUDE.mdを小さくできることを確認した。
スキルやコマンドの分割は単にコンテキスト消費を最適化するだけでなく
- Claude Codeが本当に大事にすべきことを端的に伝えられる
- スキルやコマンドが必要になるごとに読み込む特性により、長時間の作業で指示を忘れるリスクを軽減できる
といった多面的な最適化を果たせた。
Atusy's blog