2026-11-25Gunner Technology
Clean Code Has to Earn Its Token Bill
Clean code can reduce AI coding costs when it helps an agent find the right boundary, change less code and pass the checks with fewer attempts. The savings are real only when they lower the cost of accepted work, not when they make a repository look tidier while the same jobs still wander and return for repair.
Where does a coding agent spend tokens before it writes code?
A coding agent spends tokens reading the task, searching the repository, opening files, following references and interpreting tool results before it makes a change. Tokens are the pieces of text a model processes or produces. That means repository discovery is part of the job's cost, even when no code has changed yet.
A tangled repository makes that discovery route longer. Ambiguous names produce wider searches. Duplicate implementations force the agent to decide which one is live. Dead code looks like a valid path until another file or failed check proves otherwise. Large mixed-purpose modules make the agent load unrelated behavior just to understand one boundary.
This is why repository search belongs in the agent budget. The model does not receive free understanding because the files are already on disk. Every search result and opened file consumes capacity, and irrelevant context can push the useful evidence farther away from the decision.
Which code cleanup actually helps an agent?
Cleanup helps when it makes the route from a work request to the responsible code shorter and less ambiguous. Remove code that cannot run. Give boundaries names that match the behavior they own. Split unrelated responsibilities where the checks can still prove the separation. Keep one maintained command for each routine repository task.
Prefer changes an agent can verify mechanically. If two helpers appear to do the same job, prove which callers use each one before deleting either. If a module is too broad, preserve its public behavior while moving one responsibility at a time. A cleaner shape that silently changes behavior is not an optimization. It is an unpriced feature change.
Stable task commands matter as much as elegant functions. A task runner gives agents a compact repository interface so they do not rediscover how to format, test or build on every run. Code structure narrows where the agent looks. Executable commands narrow what it must remember. Both reduce exploration without asking the model to guess less carefully.
How do you prove the cleanup lowered AI coding cost?
Run the same class of work through the repository before and after the cleanup, then compare the complete route to acceptance. Record search activity, model usage, attempts, elapsed time, failed checks and rework. Hold the task definition and acceptance checks steady. Otherwise a smaller token count may only mean the later run did less work.
Measure by work class rather than averaging the whole repository. A cleanup around authorization may help permission changes without affecting a visual component. A smaller build interface may help every task. Grouping unlike jobs together hides both results. Our approach to forecasting agent costs by work class gives each route its own expected attempts and proof burden.
The final denominator is accepted work. A run that uses fewer tokens but produces a defect is more expensive once diagnosis and repair return to the bill. A run that reads more context and passes on the first attempt may be the cheaper route. Token use is an input cost. Acceptance is the event that tells you the input bought something.
When is cleanup the wrong investment?
Cleanup is the wrong investment when the factory cannot name the recurring work it will improve or the check that will protect the change. Rewriting a stable area because an agent called it messy spends tokens now, creates review risk and may erase behavior nobody documented. Aesthetic discomfort is not a business case.
Do not let the producing agent expand a small task into an open-ended refactor. Give cleanup its own ticket, boundary and acceptance evidence. If a confusing path blocks the current job, make the smallest safe repair and record the larger issue separately. That keeps an agent from declaring its own detour necessary after it has already taken it.
Start where repeated work keeps paying the same discovery tax. Choose one task class, preserve its current behavior and shorten the route to the responsible code and checks. Then run another real job through it. Gunner Technology's position is simple: clean code earns investment when the factory can show fewer wrong turns and cheaper accepted results. Until then, cleanliness is a preference, not a control.
Quick answers
Should you refactor a repository before adding coding agents?
Not all at once. Start with a recurring task, measure where the agent gets lost and repair the specific boundary or command that causes the waste.
Is token count enough to compare two repository designs?
No. Include attempts, failed checks, rework and whether the result was accepted. A cheap failed run is still waste.
Can comments make up for confusing code structure?
Comments can explain intent, but they cannot remove duplicate paths or enforce a boundary. Prefer executable structure and checks, then use comments for context a machine cannot derive.