AI · Engineering
I tested if code formatting wastes LLM tokens. Short answer, it does.
Five languages, one tokenizer, and a 7–30% input saving the model never needed

I use LLMs every day for analysis and coding. Mostly Salesforce Apex, but also JavaScript (for Salesforce as well), Python for scripts for analysis and automation, PHP on some legacy stuff, C++ so I'll not forget. When I ask AI to review or refactor some code, I don't consider "quantity" on my prompts, nor "how large" is my source code. Full indentation, blank lines between methods, all clean and readable, the way any code reviewer would want it. Then one day I asked myself: am I paying for something the model does not even need?
How I measured it. My first instinct was to use Claude's own tokenizer, since that is what I actually pay for. But Anthropic does not give you an offline tokenizer library the way OpenAI gives tiktoken. To get an exact Claude count you need to call their count_tokens API for every single text, which means a key, a network call, and cost for every measurement. For a quick personal test, that was too much friction. So I used tiktoken with the o200k_base encoding instead, the same one GPT-4o uses. It is free, offline, and fast. The absolute numbers will not be identical to what Claude would report, but the pattern, how much formatting adds, moves the same way no matter which tokenizer counts it.
Small Python script: read the file, run tiktoken.encode(), count. Then I made a stripped version of the same file by hand, removed leading whitespace, removed blank lines, joined short statements where the language syntax allowed it. Ran the tokenizer again, compared. Before calling it done, I also checked that the stripped code still compiled or ran, no point counting tokens on broken code.
JavaScript. An LWC component, around 180 lines, 2-space indent, blank lines between hooks. Formatted, 1212 tokens. Stripped, 852 tokens. About 29% down.
Python. Tricky one, since Python needs indentation to run, you cannot delete it. I only removed extra whitespace and blank lines, reduced indent to one space per level. Formatted, 388 tokens. Stripped, 361 tokens. Only 7% down, which makes sense, the language depends on that whitespace to function.
PHP. An old class from a legacy project. Braces everywhere, lots of room to compress. Formatted, 505 tokens. Stripped, 354 tokens. That is 30% down, the biggest drop of the group.
C++. A short class, constructor plus two methods. Formatted, 298 tokens. Stripped, 217 tokens. Around 27% down.
Apex. The one I care about most, since this is what I work with daily and what feeds our internal tools constantly. A trigger handler class, formatted the way our team style guide expects. Formatted, 540 tokens. Stripped, 397 tokens. 26% down. Apex behaves close to Java here, braces do the structural work, indentation is just for humans.
After stripping each file, I sent both versions to Claude and asked it to complete a missing method or fix a small bug. Compared outputs by eye and by running the code. Every language, same result functionally. No broken syntax, no missing logic, no drop in quality I could find.
One thing repeated in every test: even when I sent the stripped version, Claude answered back with nicely formatted code anyway. It seems to default to whatever style it learned in training, regardless of what I feed it. So the saving lives on the input side, not the output, unless you explicitly instruct the model to skip formatting in its reply too.
At this point I wanted to know if someone had already studied this properly, because it felt too obvious to be new. I found this paper: The Hidden Cost of Readability: How Code Formatting Silently Consumes Your LLM Budget, from researchers at Monash University and Singapore Management University. They did this at real scale, ten models, four languages, thousands of samples, proper statistical testing. Their average input token reduction was 24.5%. My numbers are not the same as theirs, different files, different tokenizer, one guy with a script versus a research team, but the direction and the size of the effect line up close enough that I trust both results now. They also confirmed the same thing I saw, performance holds steady after stripping formatting, but output stays formatted unless you tell the model otherwise.
For anyone building tools around LLMs, code review automation, internal assistants, anything sending code as part of a prompt repeatedly, this is not a minor detail. Strip non-essential formatting before the prompt goes out, keep the quality, cut the cost. Just do it carefully, especially in C++ or C#, where some whitespace is required by syntax around preprocessor directives. A formatter-aware strip is safer than a blind regex.
My conclusion after testing five languages myself: formatting is for the human reading the code, not for the model reading the prompt. Strip it before you send it, let it come back formatted if you want it that way.