TuBrief
Subscribed Channels
Videos
Community

Fixing Spaghetti Code Created by AI Coding Agents and Reducing Token Costs

TuBrief Editorial
August 12, 2026
0
Computing/Software

Written with AI assistance from the source video. The video is the authority.

English한국어Español中文العربيةहिन्दीDeutschFrançaisPortuguêsРусскийBahasa Indonesia日本語

Related Video

Loop Engineering is the new hype ... and I hate it already8:12

Loop Engineering is the new hype ... and I hate it already

Maximilian Schwarzmüller

More from the community

사내 시스템에 llm api 붙일 때 마주하는 현실적인 한계와 대응법

September 13, 2026

레거시 백엔드에 GPT-6 Astra 붙일 때 예산 승인과 보안 통과를 먼저 끝내는 법이 있습니다

September 13, 2026

에이전트끼리 대화하다 6천만 원 청구서가 나오는 이유

September 13, 2026

사내 RAG 벡터 검색에 Okta 권한 필터를 직접 거는 방법

September 13, 2026

브라우저 에이전트에게 내 구글 계정을 통째로 넘기면 안 되는 이유

September 12, 2026

Apple Won the AI Race

September 12, 2026

Comments (0)

Log in to leave a comment

No posts yet

© 2026 . All rights reserved.

TuBrief
Subscribed Channels
Videos
Community
Log in

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.