Claude Codeの“記憶力”とうまく付き合う
要点
claude codeで会話がかみ合わなくなる原因は「コンテキストサイズ」と「auto-compact」。
CLIで見える化できるので、軽くTips的に紹介します。
はじめに
こんにちは!J-CATでWabunkaを開発している二本柳です!
「Claude Codeが前の発言を忘れて会話が噛み合わない」──みたいな経験ありませんか?
原因は コンテキストサイズの上限 と、そこに近づくと発動する auto-compact(自動要約圧縮) です。
-
Claude Code(Claude 3系モデル)のコンテキスト上限は 約200,000トークン※
※ 最新モデル Claude Sonnet 4 では、最大 1,000,000 トークンのコンテキストウィンドウがベータ提供中らしい - 参考リンク -
上限の約 80~95% に達すると自動的に
/compact
が走り、会話履歴が要約される -
要約の際に必要な情報が落ちると「記憶喪失」っぽくなる
どう対処する?
/clear
等する
コンテキスト使用率を把握して、定期的に/clear
などを使用して、再度必要な情報を渡してあげると高品質な回答が得られるようになる傾向があります。
けど、、
コンテキスト使用率は定期的に確認しておく必要がありますが、毎回わざわざ確認するのは正直めんどうです。
そこで、v1.0.71 のアップデートで CLI の下部に使用率を表示できるようになりました!
これでリセットのタイミングを判断しやすくなります。
こんな感じ。
ということで設定方法です。
1
claudeを立ち上げて、/statusline
と入力
2
Tell me ~~?と聞かれるので、プロンプトでどのように表示したいかを伝えましょう。
(適当に言っても、ちゃんとやってくれます!)
3
下部にステータスが表示されると思います。
微調整したい場合、再度/statuline
でどんな感じで調整するかプロンプトで伝える
出来上がり!
補足
スタイルにこだわりたい人やうまくいかない人は.claude/settings.json
に下記を設定のうえ、~/.claude/statusline.js
にスクリプトを書いてください。
{
"statusLine":
{ "type": "command",
"command": "~/.claude/statusline.js"
}
}
参考までに、私の~/.claude/statusline.js
です。
コピペで使ってください。
statusline.js
#!/usr/bin/env node
const fs = require('fs');
// Read input from stdin
let input = '';
process.stdin.on('data', chunk => {
input += chunk;
});
process.stdin.on('end', () => {
try {
const data = JSON.parse(input);
// Extract necessary information
const transcriptPath = data.transcript_path || '';
const cwd = data.workspace?.current_dir || '';
const model = data.model?.display_name || 'Unknown';
// Calculate context usage
let contextInfo = 'Context: 0 / 200000 (0%)';
if (transcriptPath && fs.existsSync(transcriptPath)) {
try {
const content = fs.readFileSync(transcriptPath, 'utf8');
const words = content.split(/\s+/).filter(word => word.length > 0);
const wordCount = words.length;
const total = 200000;
const percentage = Math.floor((wordCount * 100) / total);
contextInfo = `Context: ${wordCount} / ${total} (${percentage}%)`;
} catch (err) {
// Keep default context info if file read fails
}
}
// Get git branch if in a git repository
let gitBranch = '';
try {
const { execSync } = require('child_process');
const gitDir = execSync(`git -C "${cwd}" rev-parse --git-dir 2>/dev/null`, { encoding: 'utf8' }).trim();
if (gitDir) {
const branch = execSync(`git -C "${cwd}" --no-optional-locks branch --show-current 2>/dev/null`, { encoding: 'utf8' }).trim();
gitBranch = ` [${branch || 'detached'}]`;
}
} catch (err) {
// Not a git repository or git command failed
}
// Get directory name
const dirName = cwd.split('/').pop() || cwd;
// Format and output the status line
console.log(`${model} | ${dirName}${gitBranch} | ${contextInfo}`);
} catch (err) {
console.error('Error:', err.message);
process.exit(1);
}
});%
まとめ
- Claude Codeの“忘れっぽさ”は コンテキスト上限と/compactの仕組みによるもの
-
/clear
や/compact
を手動でうまく使えば、回答の品質を上げることができる。 - CLIの ステータスラインで「今どのくらい使っているか」を見える化できる
参考リンク
- Context windows: https://docs.anthropic.com/en/docs/build-with-claude/context-windows
- Cost管理と/compact: https://docs.anthropic.com/en/docs/claude-code/costs
- ステータスライン: https://docs.anthropic.com/en/docs/claude-code/statusline
Discussion