Show original
Enjoyed this article?
Use "Request tipping" to ask the author to set up tip receiving.

AI translation
Audit of context injection reduced from 228KB to 48KB
Enjoyed this article?
Use "Request tipping" to ask the author to set up tip receiving.
Last time, I wrote Claude Codeに自分でスキルを書かせて育てる話. As you add more skills and plugins, you'll inevitably hit the problem of sessions becoming sluggish and missing recent instructions. This time, I'm documenting the audit where I measured the cause and reduced context injection from 228KB to 48KB.
At some point, Claude Code started behaving like this:
At first I thought it was "the model's condition," but it wasn't. The context injected at SessionStart had ballooned so much that recent user instructions were getting buried. The larger the injection, the thinner the relative presence of the latest part of the conversation becomes.
Here's the important triage I discovered through auditing:
If you lump "slow" and "confused" together, you'll just delete plugins without seeing results. The real culprit for "confused" is elsewhere.
There are multiple injection sources: plugin metadata, rules/, Obsidian context, auto-memory, remember history. I measured these by source in actual practice. Tokens can be roughly estimated as "bytes ÷ 4 ≈ English tokens."
# rules/ 配下は「グローバル指示」として全文が自動ロードされる(上限なし)
find ~/.claude/rules/ecc -name '*.md' -exec cat {} + | wc -c
# auto-memory(MEMORY.md は全文注入)
wc -c ~/.claude/projects/*/memory/MEMORY.md
# remember 履歴(now/recent/archive が注入対象)
wc -c ~/.remember/now.md ~/.remember/recent.md ~/.remember/archive.mdWhen I measured, the culprit was obvious at a glance. rules/ecc alone occupied about 57K tokens (≈228KB), making it the single largest block in all injections.
This was the biggest pitfall. The ~/.claude/rules/ directory isn't read on-demand via @import; instead, Claude Code loads the entire directory as a global instruction every single time.
My rules/ecc contained rules for 10 languages from a generic rule pack (angular / golang / swift / php / ruby / arkts / java / kotlin …) all bundled together. I only actually write Python / TypeScript / Web—three languages—yet unused Swift and Ruby coding conventions were being fully injected into every session.
Since rules/ is directory-load rather than @import, you don't need to touch import lines. Just mv the files outside the tree to stop injection. And when you want them back, just mv them back—completely non-destructive.
# 使う言語(common/python/typescript/web)だけ残し、残りはツリー外へ退避
A="$HOME/.claude/.rules-archive/ecc"; mkdir -p "$A"
cd ~/.claude/rules/ecc
for d in angular arkts cpp csharp dart fsharp golang java kotlin perl php ruby rust swift zh; do
[ -d "$d" ] && mv "$d" "$A/"
done
# 復元は mv "$A/rust" ~/.claude/rules/ecc/ で1個ずつKey Point
Even hidden directories (.rules-archive) will be mislodaded if placed inside the rules/ tree. Always move archived files outside the tree.
After pruning, measuring rules/ecc again gave me this:
$ find ~/.claude/rules/ecc -name '*.md' -exec cat {} + | wc -c
48092
$ ls -d ~/.claude/rules/ecc/*/ | xargs -n1 basename
common
python
typescript
web228KB (≈57K tokens) → 48KB (≈12K tokens). About 79% reduction. The directory also shrank from 10 languages to 4. Opening a new session made startup lighter, and missing recent instructions clearly decreased.
At first, I thought Obsidian's hot.md had ballooned to 32KB, so "if I delete this, it'll get lighter." Wrong. Obsidian context already has injection-script-side caps of hot.md=9000 chars / index=2500 chars, so no matter how many KB the file grows, injection stays constant at about 7K tokens.
Similarly, remember's today-*.done.md is outside the injection scope. Deleting these is disk cleanup, not token reduction—it doesn't save a single bit of tokens. "Large file = heavy injection" doesn't hold, so always measure injection sources in actual bytes.
You might think "if I'm on MAX plan, $/token is flat-rate, so it doesn't matter?" That's true for cost. But the real merit of reduction is elsewhere:
When reducing plugins, settings.json's enabled: true→false can be rolled back instantly. claude plugin uninstall reclaims the disk, so it's non-reversible. You should first disable and see how it goes. However, disabling a plugin makes its slash commands error out, so keep rarely-used but critical commands.
Next time, I'll write about automating these (skill generation, auditing, briefing) with **24時間回すために組んだ launchd の話**, and the macOS-specific pitfalls I hit there.
Lily (@bokuwalily) — Individual developer. Building automation infrastructure with Claude Code while mass-producing iOS apps and web services
◼︎ Apps I've made are summarized at **ポートフォリオ**📱
◼︎ New releases and development behind-the-scenes on X **@bokuwalily**🐦
◼︎ OSS: **github.com/bokuwalily**🐙
Your ❤️ and shares are my motivation!
前回、Claude Codeに自分でスキルを書かせて育てる話を書きました。スキルやプラグインを増やしていくと、いつか必ずぶつかるのがセッションが重くなる・指示を取りこぼす問題です。今回は、その原因を測定してコンテキスト注入を228KBから48KBまで削った監査の記録です。
ある時期から、Claude Codeがこういう挙動をするようになりました。
最初は「モデルの調子」かと思いましたが、違いました。SessionStartで注入されるコンテキストが膨らみすぎて、直近のユーザー指示が埋もれていたのが原因です。注入が大きいほど、会話の最新部分の相対的な存在感が薄まります。
監査して分かった大事な切り分けがこれです。
「重い」と「ボケる」を一緒くたにすると、プラグインばかり削って効果が出ません。ボケる方の本命は別にあります。
注入源は複数あります。プラグインのメタdata、rules/、Obsidian文脈、auto-memory、remember履歴。これをソースごとに実測します。トークンはざっくり「バイト数 ÷ 4 ≒ 英語トークン」で見積もれます。
# rules/ 配下は「グローバル指示」として全文が自動ロードされる(上限なし)
find ~/.claude/rules/ecc -name '*.md' -exec cat {} + | wc -c
# auto-memory(MEMORY.md は全文注入)
wc -c ~/.claude/projects/*/memory/MEMORY.md
# remember 履歴(now/recent/archive が注入対象)
wc -c ~/.remember/now.md ~/.remember/recent.md ~/.remember/archive.md測ってみると、犯人は一目瞭然でした。**rules/ecc 単独で約57Kトークン(≒228KB)**を占めていて、全注入のなかで群を抜く最大ブロックだったのです。
ここが一番の落とし穴でした。~/.claude/rules/ 配下は、@import で必要な時に読むのではなく、Claude Codeがグローバル指示としてディレクトリ全体を毎回フルロードします。
僕の rules/ecc には、汎用ルールパック由来で 10言語分のルール(angular / golang / swift / php / ruby / arkts / java / kotlin …)が丸ごと入っていました。僕が実際に書くのは Python / TypeScript / Web の3つだけなのに、使いもしないSwiftやRubyのコーディング規約が、毎セッション全文注入され続けていたわけです。
rules/ は @import ではなくディレクトリロードなので、import行をいじる必要はありません。ファイルをツリーの外に mv するだけで注入が止まります。そして戻したい時は mv で戻すだけ ―― 完全に非破壊です。
# 使う言語(common/python/typescript/web)だけ残し、残りはツリー外へ退避
A="$HOME/.claude/.rules-archive/ecc"; mkdir -p "$A"
cd ~/.claude/rules/ecc
for d in angular arkts cpp csharp dart fsharp golang java kotlin perl php ruby rust swift zh; do
[ -d "$d" ] && mv "$d" "$A/"
done
# 復元は mv "$A/rust" ~/.claude/rules/ecc/ で1個ずつポイント
隠しディレクトリ(.rules-archive)でも、rules/ ツリーの中に置くと誤ロードされる恐れがあります。退避先は必ずツリーの外にしてください。
刈り込み後、現在の rules/ecc を測り直すとこうなりました。
$ find ~/.claude/rules/ecc -name '*.md' -exec cat {} + | wc -c
48092
$ ls -d ~/.claude/rules/ecc/*/ | xargs -n1 basename
common
python
typescript
web228KB(≒57Kトークン)→ 48KB(≒12Kトークン)。約79%減。ディレクトリも10言語から4つに減りました。新規セッションを開くと、起動が軽くなり、直近指示の取りこぼしも明確に減りました。
最初、Obsidianの hot.md が32KBに膨らんでいたので「これを削れば軽くなる」と思いました。間違いでした。Obsidian文脈は注入スクリプト側で hot.md=9000字 / index=2500字 と既に上限カットされていて、ファイルが何KBに膨らもうが注入量は約7Kトークンで一定です。
同じく remember の today-*.done.md も注入対象外。これらを消すのはディスク整理であって、トークンは1bitも減りません。「ファイルが大きい=注入が重い」は成り立たないので、必ず注入源を実測してください。
「MAXプランなら$/tokenは定額だから関係ないのでは?」と思うかもしれません。コスト面はその通りです。でも削減の本当のメリットは別にあります。
プラグインを減らす場合、settings.json の enabled: true→false なら即ロールバックできます。claude plugin uninstall はディスクごと回収するので非可逆。まずは disable で様子を見るべきです。ただし、disableするとそのプラグインのスラッシュコマンドはエラーになるので、稀にしか使わないが重要なコマンドは残します。
次回は、これらの自動化(スキル生成・監査・ブリーフ)を**24時間回すために組んだ launchd の話**と、そこで踏んだmacOS固有の罠を書きます。
Lily(@bokuwalily)― 個人開発者。Claude Code で自動化基盤を組みながら、iOSアプリやWebサービスを量産しています
◼︎作ったアプリは **ポートフォリオ** にまとめています📱
◼︎新着・開発の裏側は X **@bokuwalily** で発信しています🐦
◼︎OSS: **github.com/bokuwalily**🐙
皆さんの ❤️ やシェアが励みになります!