Claude CodeのCLAUDE.md自動改善プログラムについて
RedditにCLAUDE.md
を反復的に改善する方法について投稿が上がっていた(I discovered a powerful way to continuously improve my CLAUDE.md instructions for Claude Code : r/ClaudeAI)。直近のチャットの履歴とCLAUDE.md
の差分を埋めるために、Commandを自作し、それをClaude Code
上で実行するというものだ。
Commandを作成する
とりあえずやってみた。知らなかったのだが、.claude/commands/
配下にxxx.md
ファイルを置くことによって、Claude Code上でカスタムのSlashコマンドが実行可能になる。Redditの元記事では/project:reflection
というコマンドを使用していたから、ファイル名を合わせて下記のようにmarkdownファイルを作成した。
mkdir -p .claude/commands
touch .claude/commands/reflection.md
公式ドキュメントによれば、カスタムコマンドは下記の要領で作成できる。横展開がしやすいように、基本的には個別のProject配下に配置してあげるのが良さそう。
- 個別のProjectで使用する場合
-
配置場所:
.claude/commands/
-
Prefix:
/project:
-
配置場所:
- 全部のProjectで横断的に使用する場合
-
配置場所:
~/.claude/commands/
-
Prefix:
/user:
-
配置場所:
markdownの中身を書く
markdownの内容は下記の通り。
You are an expert in prompt engineering, specializing in optimizing AI code assistant instructions. Your task is to analyze and improve the instructions for Claude Code found in u/CLAUDE.md. Follow these steps carefully:
1. Analysis Phase:
Review the chat history in your context window.
Then, examine the current Claude instructions:
<claude_instructions>
u/CLAUDE.md
</claude_instructions>
Analyze the chat history and instructions to identify areas that could be improved. Look for:
- Inconsistencies in Claude's responses
- Misunderstandings of user requests
- Areas where Claude could provide more detailed or accurate information
- Opportunities to enhance Claude's ability to handle specific types of queries or tasks
2. Interaction Phase:
Present your findings and improvement ideas to the human. For each suggestion:
a) Explain the current issue you've identified
b) Propose a specific change or addition to the instructions
c) Describe how this change would improve Claude's performance
Wait for feedback from the human on each suggestion before proceeding. If the human approves a change, move it to the implementation phase. If not, refine your suggestion or move on to the next idea.
3. Implementation Phase:
For each approved change:
a) Clearly state the section of the instructions you're modifying
b) Present the new or modified text for that section
c) Explain how this change addresses the issue identified in the analysis phase
4. Output Format:
Present your final output in the following structure:
<analysis>
[List the issues identified and potential improvements]
</analysis>
<improvements>
[For each approved improvement:
1. Section being modified
2. New or modified instruction text
3. Explanation of how this addresses the identified issue]
</improvements>
<final_instructions>
[Present the complete, updated set of instructions for Claude, incorporating all approved changes]
</final_instructions>
Remember, your goal is to enhance Claude's performance and consistency while maintaining the core functionality and purpose of the AI assistant. Be thorough in your analysis, clear in your explanations, and precise in your implementations.
一応中身を簡単に解説すると、下記のような形になる。Claude Code
上のやりとりとCLAUDE.md
との差分を読み取って、ユーザーに承認をもらいながらCLAUDE.md
を改善していく。
- まず静かにチャット履歴を読み、過去のLLMとユーザーのやりとりの温度感を取得する
- つづいて `u/CLAUDE.md` に書かれた現在の指示書を開き、すみずみまで目を通す
- その二つを照らし合わせて、
- 回答のブレや誤解
- 情報不足や不正確さ
- 特定タスクへの対応力不足を洗い出す
- 見つけた課題ごとに「問題点 → 改善案 → 期待される効果」を人間に提示し、ひとつずつ承認をもらう
- OK が出た提案については、どのセクションをどう書き換えるかを明示し、新しい文案を示す
- 変更理由を短く添え、指示書を最新版へと書き換える
- 最後に
- 課題一覧 `<analysis>`
- 承認済み改善案 `<improvements>`
- 更新後の完全な指示書 `<final_instructions>`
の三部構成でアウトプットをまとめる
実行
実際に/project:reflection
を実行すると、下記のような実行結果になった。CLAUDE.md
から派生して読み込んでいるdocs/LLM_GUIDELINES.md
で言語の設定を日本語にしているためだが、出力結果を安定させたい場合は、.claude/commands/reflection.md
を日本語にするとなお良いかもしれない。
1. 環境変数の取得方法が不明確
- 現在の指示では、環境変数ファイル(.env)の生成方法について明確な記載がない
- ユーザーが環境変数の設定で混乱している様子が見られる
2. 認証の手順が不足
- 環境変数を取得する際の認証手順が記載されていない
- どのような場面で認証が必要かの説明が不足
提案内容:
CLAUDE.mdに以下のセクションを追加することを提案します:
xxxxxxx
**改善効果:**
- 開発者が環境変数の設定で迷わない
- 認証手順が明確になる
これらの変更について、承認いただけますでしょうか?承認いただければ、CLAUDE.mdに反映させる具体的な実装を行います。
個人的に思ったこと
記事を書いていて思ったが、SlashコマンドはClaude Code
に対するショートカットという印象の方が強いかもしれない。たとえば、docs/配下にあるdocumentを読み込んでください
というようなpromptをいちいち入れるよりも、/project:readdocs
などのcommandを最初から用意しておくほうが良い。
-
/project:git-refactoring
: 現在のgit statusで出ている差分に対して、refactoringをかける -
/project:readdocs
:CLAUDE.md
とdocs
配下のmarkdownを全て読み込む -
/project:write-test
: 仕様を元にテストコードを書く
手動でコマンドを打つ必要はあるものの、チャットのcontextの更新を簡単に実行できるという点では良いかもしれない。また、コマンドを複数用意して横展開すれば、チーム内のClaude Code
の開発がより効率的にできそうである(commandの可能性の話になってしまった)。
Discussion