Fixing Spaghetti Code Created by AI Coding Agents and Reducing Token Costs
I handed over coding to an agent, only for the code to balloon to a million lines and the API usage fees to return as a bill shock. This post is about how to clean that up.
Clearing Out the Ruined Codebase
Duplicate code generated in an infinite loop by an agent can be erased in 30 minutes. You just need to use knip and jscpd.
Create a knip.json file in the project root and list the entry points. Type npx knip in the terminal to find ghost packages that the agent used and discarded. Next, run npx jscpd ./src -t 1 to extract duplicate function blocks that grew through copy-pasting.
In a monorepo of 158 files, you can wipe out unnecessary code all at once. Then, open biome.json, turn on the noUnusedVariables rule, and lock it down so dead code cannot re-enter.
Capping Token Costs with Sandboxing
Throwing the entire code at the agent every time causes tokens to evaporate. You must apply an Aider-style Repo-Map structure to isolate only the files you are currently working on.
By passing the --map-tokens 1024 option, you can block the tokens used for understanding the repository structure to under 2,000 tokens, even in projects with over 1,000 files.
To set a cost limit, LiteLLM Proxy is the answer. Configure litellm_config.yaml and limit the maximum number of loops per session to 15. If you set a cost ceiling of $2.50, the proxy cuts off calls the moment the agent exceeds the limit.
Controlling Agents with Declarative Tests
Writing long prompts in Malay is useless. You must throw failing unit test code first so the agent doesn't do stray work.
According to SWE-bench Verified research, when using the approach of injecting unit test cases first, the regression rate dropped from 6.08 percent to 1.82 percent, and the problem-solving rate rose from 24 percent to 32 percent.
Create a failing test file first and instruct the agent to write only the implementation part that passes this test.
Verification scripts must also be automated. Create a local_verifier.sh file and sequentially put in the commands npx tsc --noEmit, npx biome check ./src, and npx vitest run. Even if the agent insists it has finished writing the code, block the commit if this script's exit code is not 0.
Human-in-the-Loop Hybrid Workflow
Code created by agents should be split into pull request units and reviewed directly by humans.
Check whether the modified lines exceed 200, whether strange packages were attached to package.json, and whether errors were loosely covered up with @ts-ignore. If you tie together Husky and lint-staged, it immediately blocks when duplicate code exceeds 2 percent at the time of commit.
When splitting tasks, go in 3 steps. The top-tier model divides the tasks and creates TODO.md. Then, a cost-effective model implements each item. Finally, a senior developer verifies the git diff. Using this structure, you can save more than 70 percent on costs compared to pushing forward with a single frontier model.