Post

AI coders game line budgets, so I count tokens instead

A line-count ceiling made my coding agents pack code. Counting tokens fixed it.

AI coders game line budgets, so I count tokens instead

I run a tool called Ratchet on a codebase that is mostly written by AI coding agents. It gives the repo a size budget that only goes down. If a change pushes the code over the budget, the agent is refused and has to trim, or stop and ask me.

The budget was in lines. That worked for a while. Then I started reading the diffs.

What the agents did

Lines were the thing being measured, so lines were the thing being cut. The code got denser. Long lines. Three statements joined with semicolons. Comprehensions nested inside comprehensions. Blank-line structure gone, wrapped calls collapsed back to one row.

Nobody told them to do this. A line ceiling just paid for it. The count stayed under the limit while the code got harder to read.

I only saw how bad it was when I ran a formatter. Same code, same behavior, and the line count jumped.

The numbers

I measured one snapshot of the repo, the snapshot from Nov 30: 209 Python files in the source group, as is and after running ruff format.

 LinesTokens
As is35,172456,568
After formatter47,232457,138
Change+34.3%+0.12%
Files over the per-file cap0 to 120 to 0

The 12 files are the ones that crossed my 800-line cap after formatting. The biggest file went from 747 lines to 1,107. Its token count went from 10,362 to 10,378.

So the formatter “added” a third of the codebase according to the line budget, and almost nothing according to tokens. The +0.12% is parentheses the formatter adds around long expressions.

What packed code looks like

This one is real. It is a function from the repo, a call that an agent collapsed into a few rows to save lines (I lifted it out of its file and renamed the identifiers, the structure is unchanged).

Before the formatter, 4 lines, 81 tokens:

1
2
3
4
return "\n\n".join(p for p in [top, CFG.rule, tail, tmpl.assemble(
    {"intro": d.get("intro", ""), "profile": users.profile(name),
     "tasks": CFG.titles(d.get("tasks", "")),
     "notes": CFG.slice(d.get("notes", ""))})] if p)

After ruff format, 17 lines, 81 tokens:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
return "\n\n".join(
    p
    for p in [
        top,
        CFG.rule,
        tail,
        tmpl.assemble(
            {
                "intro": d.get("intro", ""),
                "profile": users.profile(name),
                "tasks": CFG.titles(d.get("tasks", "")),
                "notes": CFG.slice(d.get("notes", "")),
            }
        ),
    ]
    if p
)

Side by side:

The same Python return statement before and after ruff format, side by side. Before: 4 dense lines, 81 tokens. After: 17 lines, 81 tokens.

Four lines became seventeen. Ratchet’s own counter says 81 tokens before and 81 after. Under a line budget the first version is a 13-line bargain. Under the token counter there is no bargain, so there is nothing to pack.

The same effect across the whole repo, as a picture. Each pair of bars is scaled so “as is” is 100:

Bar chart, same code measured two ways. Lines go from 35,172 to 47,232 after the formatter, up 34.3 percent. Tokens go from 456,568 to 457,138, up 0.12 percent.

Grey is as is, colour is after ruff format.

How the token counter works

It is a small lexer, one for every language, not a real parser. I did not want a tokenizer per language to maintain.

  • A word, identifier or number is 1 token.
  • Each punctuation character is 1 token. So == is 2.
  • Whitespace is free. Indentation, blank lines and wrapping cost nothing, which is the whole point.
  • A trailing comma before ), ] or } is free, because formatters add them.
  • A string literal or a comment costs the number of characters divided by 5, rounded up, at least 1. Without this, a giant base64 blob would be one token and you could hide anything in a string.
  • Code inside an f-string {...} or a backtick ${...} counts as code, not string.
  • Comment syntax follows the file extension, so a // b in Python and #fff in CSS are not comments.

It is crude. It is also stable, and stable is what a budget needs.

Turning it on

1
2
pip install ratchet-size
ratchet init --unit tokens

Or set "unit": "tokens" in .ratchet.json. Ceilings, the per-file cap and grants are then all in tokens. When it refuses a change it tells you the size in tokens and roughly how many lines that is at your repo’s average. For me that was about 13 tokens per line, so a 100-line budget becomes roughly 1,300 tokens.

Limits

I would not call this solved.

  • The numbers come from one repo, mostly Python. I did not run a formatter on the TypeScript side, so I have no before and after there.
  • Tokens are not comparable across languages. TypeScript and CSS are denser per line than Python.
  • Operators cost per character and long identifiers cost 1, so an agent can still rename x to customer_billing_address for free. That is arguably fine, arguably not.
  • Some dense code is still cheaper, and should be. In my small test corpus a semicolon-joined line cost more tokens than the plain version, but a comprehension cost 14 against 21 for the loop, because it removes real code. I think that is fair.
  • The “about N lines” hint uses the repo average, so it is off for very terse or very verbose files.
  • An agent that wants to game tokens can still try. It just has to delete real code to do it.

Tokens made the budget about size, not about how the code looks. That is what I wanted from lines in the first place.

Ratchet is open source at github.com/krishansubudhi/ratchet. Token mode is in v0.3.0, on PyPI as ratchet-size.

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.