DEV Community

Evgenii Zinner
Evgenii Zinner

Posted on Originally published at ezinner.com

How to Save Tokens and Make Your App Work Faster: Static Pinpointing for AI Agents

When you ship fast with coding agents for a few days straight, your codebase quietly picks up two expensive habits.

First, your app starts working slower than it should as soon as real data hits it. Everything looked fine in unit tests with five mock items, but under the hood the agent casually dropped a .find() or .includes() inside a for loop, turning a simple list render into an O(n²) bottleneck, or buried the business logic four if/try/for blocks deep.

Second, you burn through your context window and token quota way faster on every turn. Why? Because LLMs love leaving play-by-play diary entries on every third line:

// 1. Fetch the user from the list
// 2. Check if the user is active
// 3. Return the formatted result

Every time your agent reads that file again over the next fifty turns, you are paying input tokens for the model to re-read its own useless narration.

And if you try to fix both problems by opening a chat and asking the agent "make this app work faster and clean up the code," it does the worst thing possible: it reads twenty files into context, burns 15% of your quota, renames a few variables for style points, and completely misses the .find() hiding inside the loop.

I got tired of wasting context on blind refactoring, so I built two skills that I now run right before publishing any project—and recently dropped them onto Agensi as the Clean Coding 101 bundle.

Instead of asking an LLM to hunt for bad code, both skills use fast, zero-dependency Python scripts to pinpoint the exact line numbers for zero tokens first, and only bring the agent in to do the surgical fix.

1. How to Make Your App Work Faster Without "Vibe Refactoring" (craftsman)

If you want to know how to make an app work faster after heavy AI code generation, 90% of the time the culprit isn't your database or framework—it’s hidden quadratic scans and tangled control flow that the LLM wrote because it looked short.

The problem with most refactoring prompts is anchoring bias. If you paste a list of 10 classic "code smells" into a system prompt and ask an LLM to refactor a file, it feels obligated to find them. It will invent abstractions you don't need and rewrite working code just to look busy.

That’s why I built craftsman around a Zero-Anchor Architecture.

Instead of the LLM reading your codebase to guess what looks ugly, it runs analyze_complexity.py locally first. It’s a pure Python script with zero external dependencies that scans TypeScript, JavaScript, Python, Go, and Rust files in milliseconds and spits out exact line numbers for four concrete issues:

  1. Hidden Quadratic Scans (O(n × m)): Calling .find(), .includes(), .indexOf(), or .filter() repeatedly inside an active loop.
  2. Nested Loops (O(n²)): Explicit loops inside loops that blow up when array sizes grow.
  3. Deep Nesting (Depth ≥ 3): Pyramids of if inside for inside try that force both humans and LLMs to hold too much state in their heads.
  4. Bloated Functions & Runaway Chains: Massive functions and six-step .filter().map().sort() chains that allocate intermediate arrays on every step.

Because the script pinpoints the exact function and line number for zero tokens, the agent never wastes context scanning clean files. And because of the Zero-Anchor protocol, the agent isn't given a vague "make this cleaner" goal—it opens that exact hotspot and applies mechanical fixes:

  • Hoisting lookups: Pulling .find() and .includes() out of the loop into a pre-computed Map or Set so lookups drop from O(n) to O(1).
  • Flattening pyramids: Flipping nested if/else blocks into flat early-exit guard clauses (if (!valid) return;).
  • Proving nothing broke: Running the test suite immediately to verify that return values, thrown errors, and side-effect order are 100% identical.

2. How to Save Tokens on Every File Read (clean-comments)

Once the code actually runs fast and reads flat, the second pass tackles token hygiene—getting rid of all the // Step 1 noise that bloats your context window.

When I first started stripping inline comments, I thought: why not just use grep "//" or a quick regex?

Anyone who has actually tried that on a real codebase learns the hard way why regex fails in five seconds. A naive // search immediately matches:

  • https:// inside URLs
  • "// this is inside a string"
  • // inside regular expression literals
  • Comment markers sitting inside /* ... */ block comments

If you run an automated strip with regex, you corrupt strings and URLs across your whole project.

To fix that properly in clean-comments, I wrote find_inline_comments.py as a single-pass character state machine. Instead of guessing with regex, it walks through the file character by character and tracks its exact lexical state:

  • NORMAL
  • IN_SINGLE_QUOTE / IN_DOUBLE_QUOTE / IN_TEMPLATE_LITERAL (properly skipping escaped \" characters)
  • IN_REGEX
  • IN_BLOCK_COMMENT (/* ... */)

It only flags a // sequence when the parser is in the NORMAL state. For every real inline comment, it outputs the exact 1-based (line, column), whether it’s a standalone LINE comment or TRAILING at the end of a statement, and calculates the estimated token waste (ceil(length / 3.8)).

When you run it on an agent-heavy repo, seeing the total wasted tokens added up across files is eye-opening.

From there, the agent doesn't just nuke every comment blindly. It triages the exact coordinates the scanner found into three buckets:

  1. Delete (--strip): All the play-by-play narration (// Fetch user), obvious restatements of the code, and commented-out dead code get removed in reverse-column order so line offsets never shift mid-edit.
  2. Move to JSDoc (/** ... */): If an inline comment is sitting on an exported function or interface and actually describes a useful contract for callers, it gets promoted into a proper JSDoc block.
  3. Keep Inline: The only inline // comments allowed to survive are rare, non-obvious "Why" notes—like a workaround for a specific Safari bug or a subtle security ordering requirement.

My Pre-Publish Routine

I run both of these right before publishing code or wrapping up a big feature branch:

  1. Run craftsman first: analyze_complexity.py finds the hidden .find() loops and deep nesting for zero tokens, and the agent flattens them into O(1) Map/Set lookups and guard clauses.
  2. Run clean-comments second: find_inline_comments.py maps every real // comment without choking on URLs or strings, and strips out the narrator bloat so future agent sessions don't pay a token tax every time they read the file.

If you want to use them in your own setup, I put both skills up on Agensi (or you can grab them together in the Clean Coding 101 bundle).

Top comments (0)