Context & Cost Optimization Strategies
Claude Code bundles the system prompt, tool and MCP schemas, the always-on instructions, the full conversation history and every file read so far into each request. In a workspace that holds several projects, letting any of these grow unchecked is the main cause of slow, expensive turns. These habits keep that from happening.
Regulate Context with Slash Commands
When one session drifts to 40–50 messages, the cost of each turn climbs, because every new message re-sends the growing history.
/clear: task-switch reset. Run it as soon as you change context. For example, after finishing a change in one project and moving on to unrelated work in another, it drops the old history so you don’t keep paying for it./compact: in-flight condensation. When you are deep in a debugging loop and can’t clear, this compresses the history into a dense summary. It takes a focus hint, for example/compact Focus on the hook changes./context: audit the layout. Shows what is using the context window. Use it to catch an unexpectedly large file read early, before it is carried through several more turns.
Work in Plan Mode First
Asking the assistant to write a large module straight away can end in an expensive loop: it writes a few hundred lines, hits a structural error, discards the attempt and starts over. Plan Mode separates the two steps:
- Enter Plan Mode (
Shift+Tabin the terminal UI) before starting a non-trivial change. - Have the assistant lay out its approach as a plan.
- Review and approve the plan, then switch to execution.
This turns a possible multi-attempt rewrite into one approved pass.
Keep Machine-Generated Folders Out
Build output and dependency folders (**/build/, **/.dart_tool/, **/node_modules/, **/vendor/, compiled binaries) are large and almost never what a prompt needs. Reading one by accident can use a large part of the context window in a single turn.
- The workspace keeps
.claudeignoreand.graphifyignoreat its root, listing docs, build output and secret files. - Hard file-level exclusion goes through
permissions.denyand thePreToolUsehooks. See Hooks, Permissions and the Locked Policy. - Ask the knowledge graph before searching. See Graphify in the Workspace.
- Name the project and module in your request (“in
<site-repo>/src/lib/…”), so the assistant does not explore the whole workspace.
Keep Always-On Instructions Short
Every line in a CLAUDE.md or a rule without paths: is sent on every turn. Move detail into a path-scoped rule or a skill. See CLAUDE.md, Rules and Skills.
Enable Deferred Tool-Schema Loading
By default every tool and MCP schema is injected into the first turn, which costs tokens before any work happens. Deferred tool search loads a tool’s full schema only when a turn needs it. Check the CLI’s current settings documentation for how to turn it on, since the setting name has changed between versions.