Перейти к содержимому

Token Efficiency in LLM Code Generation: The Complete Research Picture

Собрано из 4 исследований и публикаций. Дата: 25 мая 2026.


OptimizationPotentialEffort
Choose languageup to 2.6× differenceStrategic
Choose frameworkup to 1.5× differenceStrategic
Remove formattingup to 35% tokensEasy
Semantic shorthand (Token Sugar)up to 22% tokensHard (requires retraining)

Источник: Martin Alderson, RosettaCode dataset, GPT-4 tokenizer

#LanguageAvg TokensCategory
1J~70Array, pure ASCII
2Clojure109Functional
3APL110Array (Unicode hurts)
4Haskell115Functional
5F#118Functional
6Python130Dynamic
7Ruby~135Dynamic
8JavaScript148Dynamic
9Go~160Statically typed
10C182Procedural

2.6× gap between most (J) and least (C) efficient.

Dynamic languages win — no type declarations. But JavaScript is an outlier: most verbose dynamic language despite being dynamic.

Functional languages punch above weight — Haskell and F# match dynamic efficiency thanks to excellent type inference. The compiler infers types, so you don’t write them.

The APL/J paradox — APL’s famous terseness hurts LLMs. Its Unicode glyphs (⍳, ⍴, ⌽) each become multiple tokens. J uses ASCII and wins at just 70 tokens.

Typed languages still win for LLM dev — compile-time catch of hallucinations, LSP integration, rapid feedback. Not about raw tokens: about keeping the agent honest.

ContextRecommended
Long AI-assisted sessionsF#, Haskell, Clojure, Ruby
Ecosystem + AI toolingPython
Maximum efficiencyJ (niche)
Performance-criticalC / Rust (separate from LLM)
Avoid for token-constrainedC, Java, plain Go

Hybrid approach: token-efficient orchestration (Clojure/F#) + performance modules (C/Rust).


Источник: Martin Alderson, follow-up study

“Frameworks matter even more than languages.”

The token gap between Flask and Spring can exceed the gap between Python and C.

  • Boilerplate volume — Rails generates much more than Flask
  • Convention density — implicit conventions reduce what the LLM must track
  • Pattern frequency — CRUD patterns repeat across files
  • File count — more files = more tokens for imports/routing
FrameworkLanguageToken Cost
FlaskPythonLowest
FastAPIPythonLow
DjangoPythonMedium
RailsRubyMedium-High
LaravelPHPHigh
SpringJavaHighest

Flask (Python) beats Rails (Ruby). Framework effect dominates language effect.

  1. Prefer Flask/FastAPI for AI-assisted projects
  2. Use scaffolding sparingly — generate only what you need
  3. Prefer implicit conventions (resources :posts) over explicit routing
  4. Avoid XML config — code-based config consumes fewer tokens
  5. Keep controllers thin — business logic in models/services, not controllers

Источник: arXiv:2508.13666 — How Code Formatting Silently Consumes Your LLM Budget

Formatting consumes ~24.5% of input tokens but provides negligible performance benefit.

LanguageInput ReductionWhy
Java34.9%Verbose syntax + heavy formatting
C++31.1%Same
C#25.3%Significant
Python6.5%Indentation required
ElementClaude-3.7GPT-4o
Newlines14.6%7.5%
Whitespace10.7%
Indentation7.9%9.6%

No — and sometimes it helps.

ModelFormattedUnformattedChange
DeepSeek-V379.1%80.0%+0.9%
GPT-4o (Python)66.4%71.5%+5.1%

1. Strip formatting before sending:

Окно терминала
astyle --style=khman --input-file=code.cpp | tr -d '\n\t '

2. Prompt for unformatted output:

P2: "Output code without formatting — delete all spacings,
newlines, and indentations, keeping syntax valid."

This gives GPT-4o 27.2% output token reduction on Java/C++/C#.

3. Fine-tune (50 samples) → up to 35.9% output reduction, -0.4% quality.

GPT-4o: $2.50/1M input, $10.00/1M output

  • 23% input reduction = $0.69 savings per 1M tokens
  • Team doing 10M/month: $6,900/month

Exception: Python only saves 6.5% — indentation is syntactically required. And Gemini-1.5 shows sensitivity to formatting removal.


Источник: arXiv:2512.08266 — Token Sugar: Making Source Code Sweeter for LLMs

Only 25.5% of Python tokens are syntax (keywords, symbols). The rest is semantic content — API calls, variable names, expressions.

Existing methods (SimPy) only compress syntax. Token Sugar attacks the semantic layer.

Replace verbose patterns with compact, reversible shorthand:

Original: for i in range(n): arr.append(x) → 40 tokens
Token Sugar: i⟨1001⟩n;arr;x⟨END⟩ → 23 tokens

The shorthand desugarizes after generation — no human ever sees it.

  1. Parse LeetCode solutions into generalized ASTs
  2. Normalize variable names to placeholders (p0, p1, p2…)
  3. Extract patterns appearing in ≥5 solutions, saving ≥1 token
  4. Result: 799 (pattern, shorthand) pairs

Example patterns:

Pattern: for p0 in range(p1): p2.append(p3)
Shorthand: p0⟨1001⟩p1;p2;p3
Pattern: p0 * (p0 + 1)
Shorthand: ⟨1002⟩p0⟨END⟩
DatasetOriginalSimPy onlyToken Sugar onlyCombined
LeetCode2.0m15.3%↓15.1%↓22.4%↓
HumanEval12.8k13.3%↓12.9%↓20.0%↓

Token Sugar and SimPy are complementary — stacking gives 20%+ reduction.

ModelBaselineToken SugarTokens Saved
Qwen-2.5 1.5B26.2%25.6%11.2%

Stronger models save more. Zero desugarization failures.

  • Currently Python-only (proof of concept)
  • Requires model retraining
  • ~1.3ms desugarization overhead per shorthand

  1. Strategic (high impact, one-time) — Choose your language and framework before the project starts
  2. Easy (high impact, recurring) — Strip formatting from code before sending to LLM
  3. Medium (moderate impact) — Use concise language idioms, avoid boilerplate
  4. Hard (requires tooling) — Token Sugar / custom shorthand for high-volume code generation
Worst: Spring (Java) + formatted + verbose patterns = ~350 tokens
Best: Flask (Python) + unformatted + idiomatic code = ~80 tokens
Ratio: 4.4× difference
  • New project: Flask/FastAPI + Python → reasonable token efficiency + massive ecosystem
  • AI-heavy project: Clojure or F# → best raw efficiency + type safety
  • Existing heavy framework: Strip formatting before LLM calls, use concise prompts
  • High-volume code generation: Fine-tune models on unformatted code