DEV Community

Line numbers made the code I send to an LLM 42% more expensive

If you build a coding helper, you'll soon want the model to point at exact lines: "the bug is on line 212", "replace lines 40 to 52". The usual trick is to number every line before you paste the file into the prompt.

It works, and it's cheap to write: one enumerate call. It isn't cheap to send. I measured it, and in the most common format the numbers add 42% to the tokens of the code.

A few words first

A token is a chunk of text the model reads; APIs bill per million of them. A tokenizer is the program that cuts text into tokens; I used tiktoken with o200k_base, the one OpenAI's current models use. Input tokens are everything you send in the request, priced at the input rate. A list price is the vendor's official price per million tokens, before any discount. cat -n is the Unix command that prints a file with line numbers; many tools copy its layout: the number padded to six characters, then a tab.

The measurement

I took every top-level module of the Python 3.13 standard library, excluding tests and private modules: 131 files, 107,041 lines, 893,573 tokens. Then I counted the same files again with five ways of numbering the lines.

| Format of each line | Tokens | Extra |
|---|---|---|
| no numbers | 893,573 | - |
| 7<tab>code | 1,058,786 | +18.5% |
| 7 | code | 1,142,306 | +27.8% |
| 7: code | 1,159,092 | +29.7% |
| 7<tab>code (cat -n) | 1,272,868 | +42.4% |

The cat -n layout costs 3.54 extra tokens per line, and an average line of this code is only 8.35 tokens long.

The per-file overhead stays in a narrow band: the median file gets +41%, and 80% of files land between +35% and +48%. The textwrap module goes from 4,491 tokens to 6,084. A right-aligned 42 | code, another popular layout, is almost as bad at +39%.

Why the padding costs more than the number

A number below 1,000 is a single token. The cost is in what surrounds it. You can check this in four lines:

import tiktoken
enc = tiktoken.get_encoding("o200k_base")
for line in ["     7\tx = 1\n", "7: x = 1\n", "7\tx = 1\n", "x = 1\n"]:
    print(len(enc.encode(line)), repr(line))

The plain line is 5 tokens. The cat -n version is 8: the five padding spaces become two tokens, the digit is one more, and the tab happens to merge with the next character. 7: costs two extra tokens, the number and the colon. A bare number followed by a tab costs one, because the tab merges into the code that follows it. That's why the cheapest format in the table is the plainest one.

Two more details. Blank lines get numbered too. Without numbers a blank line is nearly free, because its newline merges with the previous one into a single token; with cat -n numbers it costs four or five. And at line 1,000 every format gets one token more expensive, because the tokenizer cuts four digits as 123 plus 4.

What it costs per month

Say your assistant sends three files of median size, 4,250 tokens each, with every request, and handles 1,000 requests a day. On gpt-5.6 or claude-sonnet-5.5 at the $2 per million list price (from the catalog snapshot of the LLM gateway I work on), the code alone is $765 a month. Numbered cat -n style it's $1,089.72, so the line numbers cost $324.72 a month. With 7<tab>code they cost $141.44.

On a small model the absolute amounts shrink: on gpt-5.6-mini the cat -n numbers cost $32.47 a month, on claude-haiku-5.5 $16.24. The share stays the same, because it's set by tokens, not by the price.

So if you need numbers, a few habits help:

  • Use number<tab>line with no padding. It gives you the same information for less than half the overhead of cat -n.
  • Number only the part you're asking about. If the question is about one function, send that function with numbers and the rest of the file without, or not at all.
  • If you need the numbers to apply edits, consider asking for search-and-replace blocks instead, which quote the old text and need no numbers at all.
  • Tell the model not to repeat the numbers in its answer. Output tokens usually cost several times more than input, and a numbered answer can't be pasted back as code anyway.

What this doesn't solve

Line numbers exist for a reason. They let the model refer to a place without quoting it, and they may make its answers more accurate. I didn't measure answer quality. If numbers save you one wrong edit and a retry, they can pay for themselves.

The figures come from one tokenizer. Claude and Gemini use their own tokenizers, so the exact percentages will differ, though padding and separators cost something everywhere. They also come from one kind of code: the standard library has longer lines than a typical config file, and short lines suffer more, because the prefix is a fixed cost per line.

Finally, if you send the same files again and again, prompt caching can cut their input price a lot, numbered or not. Caching makes the overhead cheaper, but the numbered file is still 42% bigger, and it still takes 42% more of your context window.

Read on DEV Community ↗ ← Back to News

Comments

No comments yet. Start the discussion.