Your CLAUDE.md Is Eating Your Context. Keep Agent Memory Under a Token Budget A developer has published a method for auditing the token cost of Claude Code's always-loaded CLAUDE.md memory files, which load at the start of every session and on most subagents. The approach uses Anthropic's free token counting endpoint via a shell script to measure each instruction file, and recommends keeping files under the documented 200-line target since longer files consume more context and reduce rule adherence. The writeup notes that @path imports do not reduce context cost and that Claude 4.7 and later tokenizers produce roughly 30 percent more tokens for the same text. Claude Code https://code.claude.com/docs/en/overview 's own docs tell you when to add a line to CLAUDE.md: when Claude makes the same mistake a second time, or when you type the same correction you typed last session memory docs https://code.claude.com/docs/en/memory . It's good advice. Nobody tells you when to take a line out. So the file grows. The pnpm rule. The rule about the legacy billing folder. A release checklist you run once a quarter. A directory tree pasted in before a refactor moved half the folders. All of it loads at the start of every session, before the agent opens a single file of your code. Subagents load their own copy too, counted against their own context context window docs https://code.claude.com/docs/en/context-window . The built-in Explore and Plan agents skip it, and a custom subagent can opt out with omitClaudeMd: true subagent docs https://code.claude.com/docs/en/sub-agents . The same docs set a target: under 200 lines per CLAUDE.md file, because "longer files consume more context and reduce adherence." You pay twice. Once in tokens on every session, and again in rules followed less reliably because they compete with each other. Start with what actually loads. In a Claude Code session, run /context and look under Memory files . Claude Code loads CLAUDE.md and CLAUDE.local.md from your working directory and every directory above it, plus your personal ~/.claude/CLAUDE.md , plus any .claude/rules/ file without a paths: field. Auto memory adds the first 200 lines or 25KB of its MEMORY.md index. And @path imports don't help: the docs say they organize a long file "but don't reduce its context cost, because imported files also load at launch." If a file is over the recommended length, Claude Code warns you at startup and when you run /status . Line counts are a rough proxy. For tokens, use Anthropic's token counting endpoint https://platform.claude.com/docs/en/build-with-claude/token-counting . It's free to call rate-limited by usage tier and counts with the tokenizer of whichever model you name. CLAUDE.md reaches Claude as a user message after the system prompt, so counting each file as one user message is a fair approximation. This script needs jq https://jqlang.org and curl: bash /usr/bin/env bash count-context.sh: estimated tokens per instruction file set -euo pipefail model="${MODEL:-claude-opus-5-5}" for f in "$@"; do n=$ jq -Rs --arg m "$model" \ '{model: $m, messages: {role: "user", content: .} }' \ "$f" | curl -s https://api.anthropic.com/v1/messages/count tokens \ -H "x-api-key: $ANTHROPIC API KEY" \ -H "anthropic-version: 2023-06-01" \ -H "content-type: application/json" \ -d @- | jq '.input tokens' printf '%7s %s\n' "$n" "$f" done Point it at your instruction files. Count path-scoped rules separately: they only cost context when they load. chmod +x count-context.sh ./count-context.sh ~/.claude/CLAUDE.md CLAUDE.md \ .claude/rules/ .md If a row prints null , the API returned an error for that file; run the curl by hand to read it. Set MODEL to the model your agent actually runs. Read the numbers with a few caveats. The docs call every count an estimate, and a count may include a few tokens Anthropic adds for system optimizations, which you aren't billed for. Block-level HTML comments < -- like this -- are stripped from CLAUDE.md before it reaches the model, so a file full of maintainer notes counts higher here than it costs in a session. And recount when you switch models: Claude 4.7 and later use a newer tokenizer that produces roughly 30 percent more tokens for the same text, per the same token counting page. Every always-loaded rule rides along on every session, and on most subagents. Every line in CLAUDE.md should pass one test: does the agent need this in every session? The memory docs answer most of the rest. Multi-step procedures belong in a skill https://code.claude.com/docs/en/skills . Rules for one part of the codebase belong in a path-scoped rule. Anything that must happen at a fixed point, like a lint before every commit, belongs in a hook https://code.claude.com/docs/en/hooks-guide , which runs as a shell command instead of sitting in context. | What the line is | Where it goes | When it costs context | |---|---|---| | Build and test commands, non-default conventions | CLAUDE.md | Every session | | Rules for one area of the code | .claude/rules/ .md with paths: | When a matching file is read or edited | | A multi-step procedure | .claude/skills/