Post

Ten weeks of line budgets made my coding agents pack code

I measured six snapshots of a repo written by AI agents under a line budget. The code got denser every month. Here is the data, the fix, and how the gate works now.

Ten weeks of line budgets made my coding agents pack code

Two days ago I wrote that AI coders game line budgets. That was one snapshot. Today I switched the repo over for real, so I went back and measured how the packing built up.

Quick context. A personal AI agent I run writes most of its own code. The repo has a size budget enforced by Ratchet: a ceiling per group of files that only goes down. A change that goes over is refused, and the agent has to trim code or ask for a small grant. Until this morning the ceiling was in lines, and no file could pass 800 lines.

The code got denser every month

I pulled six commits of the repo, from September 20 to December 2, and measured the source files at each one. Comments and blank lines are left out of these numbers, so this is code only.

Two line charts over six dates from Sep 20 to Dec 2. Syntax nodes per line of code rose from 3.56 to 5.85. Lines longer than 79 characters rose from 2.4 percent to 13 percent. Dashed lines show the Dec 2 code after ruff format: 4.31 nodes per line and 7.6 percent long lines.

“Syntax nodes per line” is how many expressions and statements Python’s own parser finds per line of code. It went from 3.56 to 5.85. That is 64% more code crammed into each line. The share of lines longer than 79 characters went from 2.4% to 13%. Lines were the thing being counted, so lines were the thing being saved.

You could see the pressure in the grant log too. In the last four weeks under lines the agents asked for 390 grants. The median grant was 13 lines. That is an agent one function short of fitting, a dozen times a day.

To be fair to the agents, the repo also grew from 75 files to 213 in that time and the kind of code changed. So this is a trend, not a controlled experiment. But the direction never reversed once.

A real example

This is from a search function in the repo. It ranks results by mixing an embedding score with a keyword score. I changed one setting name, nothing else.

As the agent wrote it, 5 lines and 125 tokens:

1
2
3
4
5
bm = {i: b for b, i in ix.score(q)}
w = float(self.cfg.setting("recall_bm25")) if w is None else w
return sorted(((float(cos[i]) if v is not None and ix.vecs[i].any() else float(
    b >= BM_MIN)) + w * math.log1p(b), i) for i, b in bm.items()
    if not upto or ix.rows[i]["id"] <= upto)[::-1], raw, bm

Look at the third line. It ends in else float( and the argument is on the next line. The whole ranking rule is in there: use the cosine score if the row has a vector, otherwise a 0 or 1 from the keyword score, then add a weighted log of the keyword score. I had to read it three times.

Here is what ruff format does to it:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
 bm = {i: b for b, i in ix.score(q)}
 w = float(self.cfg.setting("recall_bm25")) if w is None else w
-return sorted(((float(cos[i]) if v is not None and ix.vecs[i].any() else float(
-    b >= BM_MIN)) + w * math.log1p(b), i) for i, b in bm.items()
-    if not upto or ix.rows[i]["id"] <= upto)[::-1], raw, bm
+return (
+    sorted(
+        (
+            (
+                float(cos[i])
+                if v is not None and ix.vecs[i].any()
+                else float(b >= BM_MIN)
+            )
+            + w * math.log1p(b),
+            i,
+        )
+        for i, b in bm.items()
+        if not upto or ix.rows[i]["id"] <= upto
+    )[::-1],
+    raw,
+    bm,
+)

19 lines and 127 tokens. The two extra tokens are the parentheses the formatter puts around the returned tuple. Under a line budget the formatted version costs 14 more lines, which is a real chunk of a 500-line weekly slack. Under a token budget it costs 2.

19 lines is not always nicer than 5. But now the condition, both branches and the filter each sit on their own line, and changing one is a one-line diff.

Before and after, whole repo

Same commit (Dec 2), all 213 source files, as the agents left them and after ruff format:

 As writtenAfter ruff format
Lines35,93248,441 (+34.8%)
Tokens469,251469,849 (+0.13%)
Average code line length53.1 chars40.8 chars
Syntax nodes per code line5.854.31
Lines over 79 chars13.0%7.6%
Lines with 3 or more calls4.65%2.76%
Files over 800 lines012
Files over 11,000 tokens00
Biggest file742 lines, 10,365 tokens1,110 lines, 10,391 tokens

The last three rows are why I could not just turn on the formatter. Under the old line rules, formatting would have put the repo 12,500 lines over budget and 12 files over the cap. The cheapest way out for an agent would have been to pack it again.

How the gate works now

Flow of one agent change. The agent finishes a change. Step 1 formats the changed files. Step 2 counts tokens per group and per file. Step 3 checks the ceiling. If over, the change is refused and goes back to the agent to trim or spend a small grant. If under, step 4 runs lint, types and the tests the change touches, then the change lands and the ceiling tightens.

Two changes went in this morning.

First, Ratchet counts tokens. Its counter is a small lexer, the same for every language. A word or a number is one token, each punctuation character is one, and whitespace is free. Strings and comments cost their length divided by five. I converted the ceilings at the measured size. The source group went from 35,697 lines to a ceiling of about 470,000 tokens. Slack went from 500 lines to 6,500 tokens and the per-file cap from 800 lines to 11,000 tokens, both at roughly 13 tokens a line.

Second, the gate formats every Python file a change touches, before it counts anything. It runs ruff check --fix and ruff format on those files, and a file that is still not formatted is refused. The order matters. The count only ever sees formatted code, so packing buys nothing.

I did not reformat the whole repo in one commit. Each file gets formatted the next time an agent touches it, so the diffs stay readable and nobody has to review a 12,000-line whitespace change.

What I don’t know yet

This is one repo, mostly Python, written mostly by agents. The quality numbers are readability proxies, not bug counts. I did not measure whether packed code had more bugs, and I would like to.

Tokens can be gamed too. A comprehension is cheaper than the loop it replaces. I think that is fair, it really is less code.

The switch is only a few hours old. I will check the trend chart again in a month and see if the line goes back down.

Ratchet is open source at github.com/krishansubudhi/ratchet. Token mode is in v0.3.0 on PyPI: pip install ratchet-size, then ratchet init --unit tokens.

Follow the journey & stay updated

Working notes on building autonomous agent loops, code size budgets, and self-improving AI assistants.


This post is licensed under CC BY 4.0 by the author.