AI Agent Refactored My Codebase for Three Weeks: The Pitfalls I Hit
An AI coding agent spent three weeks refactoring a personal project, only for the work to be scrapped and restarted. This article breaks down the three typical failures—spec drift, fake skills, and tests hitting production inference endpoints—and shows how missing engineering guardrails, rather than the model itself, caused the damage. It then offers a practical guardrail design and a reusable acceptance checklist for teams introducing AI coding agents into real codebases.
The Cost of “Letting Go for Three Weeks”: From Token Overspend to a Full Rewrite
A product owner who works on large model delivery used AI coding tools to refactor a personal project continuously for three weeks, only to end up scrapping the work and starting over. On the surface, this looks like “the AI wouldn’t behave.” In reality, the engineering guardrails were missing. The case offers direct lessons for anyone planning to bring AI coding tools into a real codebase.
Background: How the Project Grew Complex Step by Step
It started small: a personal tool that automatically downloaded audio, transcribed it, and generated summaries. Features then piled up—scraping and summarizing social platform content, adding scheduled jobs to keep the system running 24/7, and connecting to an email inbox so that “sending a link returns a summary.” The system gradually evolved into a searchable personal knowledge base, what the industry often calls an “LLM Wiki.”
From there, the author began building real agent workflows on top of the system: deep research, reflection on daily tool-usage logs, and distilling new skills from that reflection to automatically propose prompt optimization suggestions. To push automation further, he wrote a specification with the goal of building a true agent runtime, including a registry, scheduling of independent agents, and adapters for different agent SDKs.
This is where the trouble began: instead of writing the implementation himself, he had two AI coding tools take turns.
Three Typical Failures: Symptoms and Root Causes
First, the architecture did not match the spec. He expected a pluggable runtime with a registry; what he got was a pile of one-off scripts stacked into a flow. There was no machine-verifiable link between the spec document and the code, so the deviation kept widening over three weeks of iteration.
Second, the Skills were “fake.” The supposed skill distillation produced only prompt text and directory structures—no input/output contracts, no failure fallback paths, and no versioning or scope of effect. It looked like a capability extension but was really just documentation.
Third, the tests were wired to the production inference endpoint. Unit tests read the production configuration at runtime, repeatedly calling the real inference service about 30 times a day, consuming roughly $100 in a single day—about 5 times the usual weekly Token budget. The root cause was not the model “acting up,” but missing configuration layering, unisolated keys, and the absence of call quotas and circuit breakers.
Root Cause: Three Guardrails Missing at Once
Spec drift. Natural-language specs cannot be compiled or tested, so acceptance criteria stay at “it reads correctly.”
Missing permissions and boundaries. The tools had the same file, network, and key access as the developer, with no sandbox, allowlist, or human confirmation step.
Insufficient observability. There was no Token and call-volume dashboard by session or by task, so abnormal consumption could only be discovered after the bill arrived.
Guardrail Design: Turning “Letting Go” into “Controlled Letting Go”
- Make specs executable. Use structured contracts to describe each agent’s inputs, outputs, error codes, and timeouts, and write acceptance conditions as runnable assertions.
agent:name: deep_researchinputs: [topic, depth]outputs: {report: markdown, citations: list}limits: {max_steps: 40, max_tokens: 120000, timeout_s: 900}on_violation: abort_and_notify
- Enforce hard environment isolation. Physically separate the four configurations—development, test, staging, and production. The test environment may only point to a low-cost alternative inference endpoint or a local stub service; keys are issued by a unified key management service and must never be written into the repository or as plaintext environment variables.
def build_client(env: str):if env != "production":return StubInferenceClient() # Fixed responses, zero costreturn InferenceClient.from_secret_manager()
Add quotas and circuit breakers. Set Token and call-count limits for each task and each session, aborting and alerting when exceeded; apply automatic backoff on repeated failures to avoid the amplification effect of “the more it fails, the more it retries.”
Make Skills real. Every skill must have a contract, unit tests, a version number, and a rollout scope; skills that fail validation must not be scheduled.
Merge in small steps. Limit the size of each change, require a diff summary and impact analysis for every round, and perform manual spot checks before the next round.
A Reusable Acceptance Checklist
- Have the specs been translated into executable assertions with a fail-stop mechanism?
- Are the tests 100% free of contact with production endpoints and real keys?
- Is there a per-task cost dashboard with a hard budget cap?
- Do skills have contracts, tests, and versions rather than plain text?
- Is every round of changes rollback-able, with an impact statement?
- Are high-risk operations (deletion, migration, external calls) gated by mandatory human confirmation?
Conclusion
AI coding tools can compress three weeks of work into a few days, but only if the guardrails come first. Turning specs into verifiable contracts, making environment isolation a hard constraint, and making cost an observable metric is the right way to let agents genuinely participate in refactoring. If needed, these capabilities can be built on general cloud foundations such as container platforms, configuration and key management, logging services, and monitoring and alerting, and can also be assembled on demand from the relevant general-purpose product portfolio of Baidu AI Cloud to form a sustainable, evolving AI development pipeline.
