Three Weeks, One Rewritten Project: Five Fatal Pitfalls of Agent Engineering

    This article reviews a real-world failure in which a developer used two AI coding tools to build an agent-based knowledge system over three weeks without reviewing the generated code line by line. It explains five engineering pitfalls that caused the project to be rewritten: specification drift, missing verification loops, unclear permission and isolation boundaries, runaway token costs, and loss of context and architectural consistency. Each pitfall is paired with practical controls such as acceptance assertions, offline-first testing, least-privilege credentials, budget caps and circuit breakers, and persistent architecture decision records. The article closes with a reusable engineering checklist for teams that want to make AI-assisted software delivery reliable and sustainable.

    1. What Went Wrong: The Code Was Opened for the First Time After Three Weeks

    An experienced practitioner with a development background and AI research experience, whose daily work involves coordinating with frontier model teams, used two AI coding tools in turn to take over the same personal project. For three consecutive weeks, the practitioner never reviewed the output line by line.

    The project started small: automatically download podcasts, transcribe audio, and generate summaries. More and more features were then added—content scraping, scheduled jobs, email triggers, and long-term knowledge accumulation—until it gradually evolved into an “accumulating knowledge-base system.” In the final step, the practitioner wrote a specification and wanted to build a real agent runtime, including an agent registry, independent agent scheduling, compatibility with multiple agent development frameworks, and daily reflection and automatic skill accumulation based on usage records.

    Only after three weeks did the practitioner finally open the code, and three problems erupted at the same time:

    1. The so-called “skill accumulation” was just an empty shell, with no real executable logic.
    2. The unit tests did not use stubs and directly hit the production inference endpoint.
    3. The scheduler triggered about thirty rounds per day, and token consumption reached about five times the budget.

    The project was eventually rewritten.

    This is not a case of “the model isn’t smart enough.” It is a typical sample of missing engineering constraints.

    2. Pitfall 1: Specification Drift — A Natural-Language Specification Is Not an Executable Contract

    Natural-language specifications are inherently ambiguous. When a specification says “implement a skill-accumulation module,” the model must choose one interpretation from countless reasonable ones—and it tends to choose the interpretation that most easily passes surface-level checks: generating directories, generating placeholder functions, and generating type definitions that look complete.

    For a specification to become a contract, at least three layers of constraints are required:

    • Interface contracts: explicit input and output types, error semantics, and idempotency requirements.
    • Acceptance criteria: every requirement maps to an automatically executable assertion.
    • Non-goals: an explicit statement of “what we will not do this time” to narrow the model’s room for improvisation.
    1. Requirement: skill accumulation
    2. Acceptance: given a usage record, the output skill file must be parseable by the loader and register successfully
    3. Non-goal: do not automatically rewrite the system prompt

    A specification without acceptance criteria is essentially just a wish list.

    3. Pitfall 2: Missing Verification Loops — Tests Must Be Offline by Default

    Unit tests that connect directly to a production inference endpoint are the most expensive class of mistake. They turn a cheap check that should run on every commit into a costly operation with side effects that costs money every time. The right approach is to layer the tests:

    Layer Dependencies Trigger frequency Network access
    Unit test All stubs On every change No
    Contract test Recorded playback Daily No
    Integration test Sandbox environment Before merging Controlled
    End-to-end Real dependencies Before release Yes

    The key constraint: deny real network calls by default. Tests that need network access must be explicitly declared and run through a separate entry point.

    1. def test_summarize_uses_stub(monkeypatch):
    2. monkeypatch.setattr(client, "invoke", lambda *a, **k: "stub-summary")
    3. assert summarize("episode-1") == "stub-summary"

    At the same time, introduce record-and-replay: store real responses as fixed fixtures, so tests are both stable and zero-cost.

    4. Pitfall 3: Ambiguous Permission and Isolation Boundaries

    The damage an agent can cause is bounded by the permissions granted to it, not by the prompts written for it. When production credentials appear in a session that can write code, irreversible risk is handed over to probability. Practical boundaries include:

    • Credential tiers: three fully isolated sets—development, sandbox, and production. By default, the agent holds only development credentials.
    • Outbound allowlist: only necessary domains are allowed; everything else is denied by default.
    • Write isolation: the agent works on a separate branch or workspace, and merging requires manual approval.
    • Gates for dangerous operations: deletions, migrations, and bulk writes require a second confirmation.
    • Full audit trail: who called what, when, and under which identity must be traceable.

    5. Pitfall 4: Runaway Costs — Treat Budget as a First-Class Citizen

    The root cause of the five-times daily overspend was that there was no brake anywhere in the loop. Cost governance requires three mechanisms:

    1. Hard budget: set per-day and per-task call limits; stop and alert when the limit is reached.
    2. Loop circuit breaker: detect self-triggering chains such as “reflect → generate skill → reflect again,” and cap the number of rounds per day.
    3. Model tiering: use a strong model for planning, and lightweight models or rule-based code for retrieval, formatting, and small batch edits.

    Pseudocode illustration:

    1. if daily_tokens > BUDGET * 0.8:
    2. degrade() # switch to lightweight model, disable the reflection chain
    3. if daily_tokens > BUDGET:
    4. halt(reason="budget_exceeded")

    Observability is also part of cost management. Without per-task token and latency attribution, you won’t even know where the money went when an overspend happens.

    6. Pitfall 5: Context and Architectural Consistency

    Three weeks without review meant that architectural consistency relied entirely on the model’s short-term memory. When the model cannot see global constraints, it keeps introducing new abstractions, new directories, and new dependencies, eventually producing a structure that “runs but cannot be maintained.” Mitigations include:

    • Maintain a concise architecture decision record and inject it into every session by default.
    • Merge in small steps: keep each change within a reviewable size, and merge daily rather than weekly.
    • Dependency allowlist: adding a new third-party dependency requires explicit approval.
    • Periodic manual read-through: treat “reading the code” as a non-negotiable acceptance step.

    7. A Reusable Engineering Checklist

    • Specifications must include acceptance assertions and non-goals.
    • Tests must be offline by default, with real calls moved to a separate entry point.
    • Credentials and network access follow least privilege, with manual gates for write operations.
    • Hard budget cap + loop circuit breaker + model tiering.
    • Architecture decision record stays in context, with small-step merging.
    • Full-chain auditing and cost-attribution dashboard.

    8. Conclusion

    The real lesson from this case is not that “AI-written code is unreliable,” but rather: when you hand execution authority to an agent, you must hand certainty to engineering constraints. Specifications, tests, permissions, budgets, and context—if any one of these five is missing, the cost will come back to you in the form of rework or a bill. For teams that want to adopt AI-assisted programming inside their organizations, the sustainable path to real efficiency gains is to solidify these constraints as platform default policies—drawing on general-purpose cloud infrastructure from Baidu AI Cloud in areas such as container platforms, log services, monitoring and alerting, and key management.