Your Dead Code Is Costing You Tokens: How Unused Functions Inflate AI Spend

TokenCheat Team

5/3/2026

#context-engineering#cost-analysis#jcodemunch
Your Dead Code Is Costing You Tokens: How Unused Functions Inflate AI Spend

When Claude Code reads a file, it reads the entire file. Every deprecated function, every unused export, every commented-out block that someone left "just in case" — all of it converts to input tokens on your bill.

The problem at scale

Most production codebases accumulate dead code steadily; how much varies wildly with age and hygiene, so the arithmetic below states its assumptions. Here is what dead code costs in an AI-assisted workflow:

Your agent reads utils.ts (500 lines). Of those, 200 lines are functions nothing imports anymore. That is 40% waste — roughly 270 tokens at ~1.35 tokens per line of code — on every single read. If the agent reads that file 8 times during a debugging session across multiple turns, you have burned 2,160 tokens on code that does nothing.

Scale this across a codebase with 200 source files averaging 300 lines each, assuming 20% dead code:

200 files × 300 lines × 20% dead × 1.35 tokens/line = 16,200 wasted tokens per full traversal

The per-turn resident cost compounds. An agent that reads 30-50 files in a session carries 2,400-4,000 wasted tokens in context (30-50 × ~81 wasted tokens/file), and conversation context is re-sent as input on every subsequent turn — so over a 20-turn session that is roughly 48,000-81,000 billed wasted tokens, before cache discounts:

AssumptionValue
Files read per session30-50
Wasted tokens resident in context~2,400-4,000
Turns re-billing that context20
Wasted tokens billed per session~48,000-81,000
Cost per session @ Claude Opus 4.8 ($5/M input)$0.24-$0.41

Multiplied by every developer, every day.

Why agents cannot skip dead code

Unlike a compiler that tree-shakes unused exports, an AI agent has no way to know which functions are dead without analyzing the entire dependency graph first. The agent reads files as text. It cannot selectively ignore the deprecated parseConfigV1() sitting between two active functions.

Even if the agent is only looking for one specific function, most retrieval strategies (Read tool, grep, file search) pull full files or large chunks. Dead code rides along for free — free for the code, expensive for your token budget.

Finding dead code with jCodeMunch

jCodeMunch's find_dead_code tool performs static analysis to identify symbols (functions, classes, constants, types) that have zero inbound references across your project. It reports:

  • Exported symbols never imported by another module
  • Internal functions never called within their own file
  • Types and interfaces declared but never used as annotations
  • Constants defined but never referenced

The companion tool get_symbol_importance scores each symbol by reference count, helping you distinguish between genuinely dead code and rarely-used-but-critical utilities (error handlers, fallback paths).

Practical remediation steps

1. Run dead code analysis on your hottest files. Start with the files your AI agent reads most frequently. These have the highest ROI for cleanup.

2. Delete unused exports aggressively. If find_dead_code flags an export and git blame shows it has not been touched in 6+ months, delete it. Version control is your backup.

3. Split large files. A 800-line file forces the agent to read everything even when it needs one function. Break it into focused modules. The agent reads a 150-line file instead of an 800-line file — that is an 81% reduction in tokens per read.

4. Use structured retrieval over full-file reads. Tools like get_symbol_source in jCodeMunch let agents retrieve individual function bodies instead of entire files. This bypasses the dead code problem entirely for targeted lookups.

5. Audit regularly. Dead code accumulates after every refactor. Add dead code analysis to your CI pipeline or run it monthly.

Measuring the impact

TokenCheat's retrieval efficiency scoring compares tokens read vs. tokens that contributed to the agent's output. A low score typically indicates significant dead code or overly broad file reads; re-run the measurement after cleanup and the delta is your per-session saving. The free repo context score is the quickest place to start.

Dead code is not just a code quality issue anymore. In the age of AI-assisted development, it is a recurring operational expense.