Enforcing a hard line-of-code delta limit forces AI agents to write leaner code and actively delete obsolete logic.
Adapted from @paoloanzn# 𝛥LOC <= 𝑁 will fix all the software slop of agents If you want AI agents to stop endlessly growing your codebase, you need to introduce programmable meta-rules. Give them one hard constraint and a budget: \Delta LOC \le N This one alone fixes a surprising amount of agentic slop. LOC means lines of code, and: \Delta LOC = LOC(added) - LOC(removed) 𝑁 is an arbitrary maximum you define. For a normal feature or refactoring, I might use 𝑁 ≈ 300. For a larger implementation, maybe 600–700. The important part is that this is a hard invariant, not a suggestion. The agent implements the feature fully end-to-end, with no TODOs, placeholders, mocks, or deferred work. At the end of every turn, it runs a tool that computes the actual ΔLOC over the source files. The agent is forced to iterate until the constraint is satisfied. And this creates some very interesting side effects. (1) First, the agent naturally has to think harder about the implementation instead of jumping straight to the obvious path suggested by its training data or current context. Adding another helper, abstraction, adapter, compatibility layer, or special-case branch is no longer free. Every additional line consumes a scarce budget. (2) More importantly, if the agent determines that the feature cannot be implemented with less than 𝑁 LOC(added), there is only one other variable it can change: \Delta LOC = LOC(added) - LOC(removed) If LOC(added) cannot go lower, the only way to reduce ΔLOC is to increase LOC(removed). Hence why this method is extremely effective to mitigate the “agent won't delete code” problem. The agent now has an actual incentive to find obsolete code. And because models are extremely conservative about breaking existing behavior—to the point of often avoiding deletion altogether—they will usually make a serious effort to identify code that can safely disappear: old paths, duplicated logic, superseded abstractions, compatibility code, and behavior that is no longer needed, which is exactly the behavior we want. Build a tool or an automatic hook like this one: Example output: ## Why agents keep growing your codebase The first thing you need to understand is that AI coding agents tend to increase a codebase’s entropy. Your “keep the implementation minimal” or “do this in just a few lines of code” prompts usually won’t save you from this phenomenon. Because this is not something that you can fix post-training. You need to thing at models as something that gets preprocessed before being shipped to you. Think of a "raw" language model as a completion machine. It does not inherently understand the concept of a conversation, a coding task, or a tool-running agent. You provide text, and it predicts the next most probable tokens. To make that model behave consistently like a turn-based assistant—or an agent that edits files, runs tests, and keeps working until something is fixed—it goes through post-training where through supervised fine-tuning (SFT) (usually) where it get's biased towards acting in a certain a way. One of those behaviors is: when given a task, change something. Another is conservatism toward existing behavior. These two tendencies combine badly in software. Suppose a piece of logic is obsolete. A human maintainer might simply remove it. An agent is much more likely to do this: instead of deleting old_behavior() entirely. From the model’s perspective, this is safer. It satisfies the new requirement while preserving the old path in case something still depends on it. This behavior has also now been observed empirically. Research on deletion-heavy coding tasks found that agents often locate exactly the code that should disappear, but still avoid deleting it, instead adding guards, conditions, or alternate paths around it. And it's clear that simply telling the model to “keep changes minimal” does little to fix this. ## A programmable approach The practical method is to introduce what I call computed coding meta-rule checks**. These are automatic measurements over the actual source files of the codebase. They do not measure what the software does; they measure properties of the source itself—hence “meta.” Examples could include: ΔLOC <= 300 The agent runs these checks at the end of every turn and uses the failures as a feedback loop. A computed rule will fail the task. That difference changes the agent’s optimization problem. The agent now has to answer: “How do I make the tests pass while keeping the system from growing beyond this bound?” And once code growth has an explicit cost, deletion stops being something you hope the model remembers to do. It becomes economically useful to the agent. That is the core idea: don’t ask agents to write less code. Make excessive code growth an invalid solution. ** I call them meta-value because they do not measure code performance or more in general what the code does. Instead they are measurement over the source of that code. Hence the word “meta”. See dictionary.cambridge.org (https://dictionary.cambridge.org/dictionary/english/meta)