BlogCost
Controlling the context a coding agent carries into a task
Reduce context by moving what is only sometimes relevant out of the always-loaded layer, not by deleting it: procedures into skills or rules that load on demand, package conventions into path-scoped files, bulky tool output behind filters or subagents. Keep the few constraints that must hold everywhere in the layer that always loads, back the critical ones with checks outside the context, and compare cost and outcomes on the same tasks before and after.
Why context costs twice
Claude Code's cost documentation states that token costs scale with context size, and that a session sends the full conversation with every request. Prompt caching makes the repeated part cheaper, not free, and a first message after the cache expires reprocesses the whole context.
The second cost is quality. Anthropic's article on context engineering describes context rot: as the number of tokens grows, a model's ability to recall information from that context decreases. It recommends the smallest set of high-signal tokens that makes the desired outcome likely. More instructions can mean worse adherence to the ones that matter.
Know what loads before the first prompt
Start with an inventory, per tool. In Claude Code, CLAUDE.md files, auto memory, MCP tool names and skill descriptions load before you type, and /context shows a live breakdown. MCP tool definitions are deferred by default, so only names and server instructions enter context until a tool is used. In Cursor, always-applied rules sit at the start of the context, a CLAUDE.md is always applied, and a file referenced with @ in a rule is read only when needed, not inlined.
Sort every instruction into a tier
- Always loaded: constraints that apply in every session and that the agent cannot infer from the code, such as build and test commands, paths it must never edit and security rules. Keep this layer short.
- Path-scoped: conventions for one package or file type, in nested instruction files or rules with paths or globs that load when the agent touches matching files.
- On demand: procedures such as writing a migration or preparing a release, as skills that load only when invoked or judged relevant.
- Nowhere: anything the agent can read from the code itself, and anything a linter or CI already enforces.
A constraint that must hold regardless of context belongs in permissions, hooks or CI as well. Claude Code treats instruction files as context, not configuration, so a cut that drops a rule from context should never be the only thing between the agent and a protected path.
The trap in long sessions
Moving instructions to on-demand scopes changes when they are present. Claude Code's documentation spells out what happens at compaction: the project-root CLAUDE.md and unscoped rules are re-injected from disk, but path-scoped rules and nested CLAUDE.md files are summarised away and return only when a matching file is read again. Invoked skill bodies come back capped at 5,000 tokens per skill and 25,000 in total, keeping the start of each file.
So a rule that must hold for a whole session should not be path-scoped, and the most important lines of a skill go at the top. The documentation says the same: if a rule must persist across compaction, drop its paths or move it to the root file.
Levers beyond instruction files
- Filter tool output before the agent sees it. Claude Code's documentation shows a hook that reduces test output to failures.
- Send verbose work such as log analysis to a subagent, which returns a summary. Its own requests still draw on usage, so this shrinks the main context but does not necessarily lower the bill.
- Disable MCP servers nobody uses, and prefer a CLI where one exists, which the documentation describes as more context-efficient.
- Clear the session between unrelated tasks, since old conversation is resent with every message.
Measure the cut on the same tasks
A hypothetical check for one repository: replay 20 merged tasks with the current setup and with the trimmed one, three runs each. Every run starts in an isolated copy whose history ends before the change, with no later refs, remotes, caches, memory or files from earlier trials and no network route to the merged solution. The agent sees the existing tests; the acceptance tests added with each change are held out for grading. Record cost per passing task, the held-out pass rate and a scripted count of constraint violations, such as edits to generated paths. Adopt the trimmed setup if violations do not rise and the pass rate stays within a threshold agreed beforehand. If the sample is too small to tell, the result is inconclusive and the current setup stays.
LLM cost optimisationKeeping Claude Code team instructions useful
Questions
Does a larger context window remove the problem?
It moves the limit, not the trade-off. Anthropic expects context windows of all sizes to remain subject to context pollution, and every extra token in a session is still resent and billed.
Does prompt caching make large instruction files free?
No. Cached tokens are billed at a lower rate, misses after a break reprocess everything, and caching does nothing for the quality cost of a crowded context.
Which instructions should never leave the always-loaded layer?
Those whose violation is expensive and hard to spot in review, such as paths that must not be edited or commands that must not run. Enforce them in permissions or CI as well, so the instruction is a reminder rather than the control.
Related services
LLM cost optimisation
Agent spend broken down by team and task, then lowered through defaults, budgets and routing your platform team can run.
Harness engineering
The setup that tells an agent how to build, test and change your code, kept in version control.
Coding agent adoption
Agents taken from a few pilot teams to the rest of engineering, with training and one shared setup.
More from the blog
Claude Code team instructions
How a team assigns owners to CLAUDE.md files and rules, reviews them, and retires entries that no longer change what Claude Code does.
Cursor rules in a monorepo
How to keep Cursor rules relevant to each package in a monorepo, with path-scoped rules, nested AGENTS.md files and an owner for every rule.
Testing coding agent upgrades
How to check whether a new model or agent version is better on your repositories before it becomes the team default, with pinned versions and a paired comparison.
Talk to an engineer
Bring one piece of work you would like to improve. The call takes 30 minutes and needs no access to your systems or your data.
Booking runs on Google Calendar.
Prefer to write first? Leave your email and a few lines, and we reply with times.
Look for the draft in your email program
Send it from there, and an engineer replies in person with times for a 30-minute call. If no draft has opened, write to us at miki@cloudsail.com.