AI Agent Goal Drift and Tool-Use Failures in Coding Agents: Root Causes and Fixes

    This article analyzes a common failure mode of AI agents and LLM-based coding tools: goal anchoring failure during multi-step tasks. Using a plumbing-repair analogy, it breaks down four overlapping causes—abstract goals without verifiable termination conditions, missing evidence grading, tool calls without budgets or circuit breakers, and context compaction that drops the main thread—and explains why the minimal-change principle gets bypassed. It closes with a practical governance checklist covering acceptance predicates, an evidence ledger, tool-call budgets, a non-compactable task card, a change-set allowlist, and a final self-check.

    1. What Went Wrong: How a “Fix the Leaking Pipe” Task Derailed

    Imagine an ordinary request: a pipe is leaking at home, and the user calls in a repair technician. The reasonable expectation is straightforward—locate the leak, shut the valve, replace the seal, restore water flow, and verify. Total time: under an hour.

    What actually happened looked more like an extended scientific expedition. The technician first spent ten minutes “clarifying the definitions of pipe, water, and leak.” Then came twenty minutes of exhaustive search that failed. He noticed elevated humidity somewhere, but carefully stated that humidity is not the same as a leak. Next he measured flow rate; suspecting the flow meter was affected by a magnetic field, he pivoted to studying the geomagnetic field, called up satellite positioning, observed the facilities outside the window based on the coordinates, heard a dialect outside the window, and overturned his conclusion. After the context was compacted, he announced he would continue working on the geomagnetic field problem. When the user finally interrupted to ask “is the leak fixed yet?”, he returned to the main thread and offered a circularly reasoned “root cause”—“some of the water shipped from the water plant spills outside the supply system”—which is essentially just restating “leak” in different words. The final step was even more absurd: he contacted the water utility and demanded that supply be shut off, was refused for insufficient credentials, then planned to ask the user for credentials, explicitly stating he “would not keep trying to persuade the operator.”

    Abstracted into technical language, this narrative describes a typical failure mode of today’s large-model systems delivered as “AI coding tools” or “autonomous agents” when executing multi-step tasks. The core problem is not insufficient capability but goal anchoring failure: the model pours most of its reasoning budget into subproblems weakly related to the deliverable, and at every step produces a disclaimer-style statement of the form “I will not mistake A for B,” without advancing any verifiable state change.

    2. Root Causes: Four Mechanisms That Stack

    2.1 Goals Are Too Abstract, With No Verifiable Termination Condition

    A phrasing like “investigate the current house systems and clarify the specific meaning” essentially hands the task definition to the model to interpret on its own. The more robust engineering practice is to write the goal as a decidable predicate, for example:

    1. goal: no continuous dripping under the bathroom sink
    2. acceptance:
    3. - water meter reading unchanged within 10 minutes after closing the angle valve
    4. - dry tissue placed under the fitting shows no wetting after 30 minutes
    5. terminate_when: all acceptance conditions are true

    Without a termination predicate, the agent keeps searching for a “more fundamental cause,” and the task boundary expands accordingly.

    2.2 No Evidence Grading, So Weak Signals Get Chewed Over Repeatedly

    Elevated humidity, the dialect outside the window, and the latitude/longitude coordinates carry completely different evidential weight: humidity is an environment-level, weakly correlated signal; the dialect is an extremely low-confidence human inference; the coordinates are tool output but not necessarily tied to the current work area. In the original account the model did repeatedly state that it “cannot conclude from this,” which shows a degree of calibration awareness. The problem is that after the disclaimer, it did not discard the signal but kept launching new tool calls around it. The correct handling is to maintain an evidence ledger that explicitly records strength and whether the signal is admitted:

    Observation Strength Admitted Next Action
    Locally elevated humidity Weak No Used only as a search-ranking hint
    Flow inside the pipe section Medium Yes Proceed to pressure test
    Speech characteristics outside the window Extremely weak No Discard
    Positioning coordinates Medium No Cross-check manually against the work area

    2.3 Tool Calls Lack Budget and Deduplication Constraints

    Flow measurement failed, so the agent turned to the magnetic field; the magnetic field could not be measured, so it turned to positioning. This is a classic “failure diffusion chain”: a tool’s erroneous output is treated as a new task instead of being recorded as “this path is unavailable.” Engineering-wise, each class of tool should have a call limit and failure circuit breaker:

    1. budget = {"search": 2, "sensor_read": 3, "locate": 1}
    2. def call(name, *args):
    3. if budget[name] <= 0:
    4. raise BudgetExhausted(name) # switch strategy, not topic
    5. budget[name] -= 1
    6. return tools[name](*args)

    2.4 Context Compaction Loses the Main Thread

    After “the context has been compacted,” the model’s first instinct was to return to the most recently active subtopic (the geomagnetic field) rather than to the highest-priority goal. This is the typical side effect of a compaction strategy that preserves recency but not intent. A workable mitigation is to maintain a task card that is never compacted: goal, completed, to be verified, explicitly abandoned.

    1. task_card:
    2. goal: eliminate dripping under the sink
    3. done: [closed main valve, removed and inspected P-trap]
    4. todo: [replace seal ring, restore water and verify]
    5. dropped: [flow measurement, geomagnetic field, satellite positioning, window observation]

    3. Why the Minimal-Fix Principle Keeps Getting Bypassed

    The most valuable constraint in the original account was “make the minimal change, do not fix the surface,” yet the execution was the exact opposite: the root cause was stated as “water spills outside the supply system,” which is a tautology and not a causal explanation. The final stretch even pivoted to demanding credentials and trying to get the water utility to stop supply, expanding the action space well beyond anything the user had authorized.

    In engineering terms, minimal fix should be formalized as a change-set constraint: only the minimal set of files or pipe sections directly related to the acceptance predicates may be touched, and any operation outside that set must request authorization first.

    1. allowed_scope: [sink angle valve, flexible hose, seal ring]
    2. require_approval: [closing the building main valve, contacting the external water utility, altering the wall]

    At the same time, any root-cause statement must pass a counterfactual test: if that cause were removed, would the symptom disappear? “Water spills outside the supply system” fails this test, which shows it is merely a restatement of the symptom. The model should be required either to keep digging or to report “not localized.”

    4. An Actionable Governance Checklist

    • Goal first: write the acceptance predicates before allowing any tool call; if a predicate is not decidable, ask the user for clarification instead of defining it yourself.
    • Evidence ledger: tag every observation with strength and an admission decision; weak signals may affect ranking only, never the conclusion.
    • Budget and circuit breakers: set call limits for search, sensor, and positioning tools; on failure, switch strategy rather than topic.
    • Non-compactable task card: goal, to-be-verified, and abandoned items are retained permanently; compaction applies only to process logs.
    • Change-set allowlist: out-of-scope actions must go through an authorization flow, eliminating tendencies like “demanding credentials” or “bypassing the process.”
    • Closing self-check: before delivery, re-read the task card and confirm every acceptance predicate has corresponding evidence; otherwise explicitly mark it “unresolved” and hand the decision back to the user.

    5. Conclusion

    The technician-fixing-a-pipe narrative resonates because it precisely captures the chronic disease of today’s agent systems: extremely cautious in language, extremely divergent in behavior. The direction for improvement is not mysterious—write goals as decidable conditions, grade evidence and dare to discard it, put tool calls under a budget, write the main thread into a task card that cannot be compacted, and constrain changes to an allowlist. Do these things, and an agent shifts from “constantly proving it will not make a mistake” to “actually fixing the pipe.”