Why AI Agents Succeed Sometimes and Fail Other Times: Breaking Down the Stability Problem in Execution Frameworks

    This article examines why AI agents produce inconsistent results when running the same task with identical model, prompt, and tool configurations. It introduces the concept of a "flipping task," explains the often-overlooked execution framework (harness) that sits between the model and the environment, and breaks down four root causes of instability: poor observation truncation, unbalanced context management, weak completion detection, and missing retry and recovery mechanisms. Readers will find practical stability metrics, pseudocode for measurement and differential attribution, and an engineering checklist for turning occasional success into reliable delivery.

    1. The Symptom: What Is a “Flipping Task”?

    In engineering practice, developers often run into a frustrating situation: the same agent, using the exact same model, prompt, and tool configuration, completes a task cleanly on the first run, then stalls halfway through on the second—or even “believes it is done” without actually delivering anything. The industry refers to this as a flipping task: a task whose outcome swings back and forth between success and failure across repeated runs under an unchanged configuration.

    It is important to note that a flipping task is not just another word for random noise. Its defining characteristic is “same configuration, different results,” which tells us the uncertainty does not come from the requirements themselves but from the implementation layer of the system. Identifying these tasks and tracking them separately is the first step in any stability effort.

    2. The Overlooked Middle Layer: The Execution Framework (Harness)

    For a large language model to operate a terminal, call tools, read and write files, and actually finish a task, there must be a runtime system between the model and the real environment—commonly called the execution framework, or harness. It carries four responsibilities:

    • Command and tool execution: translating the model’s structured intent into real, executable actions;
    • Observation feedback: collecting execution results, exit codes, and output, trimming them, and feeding them back to the model;
    • Context management: deciding what history to keep, how to compress and summarize it, and how much of the context window to consume;
    • Completion detection: judging whether the task is truly finished and deciding whether to continue, retry, or stop.

    Over the past few years, most industry investment has gone into “making the model smarter”—for example, reinforcement learning and trajectory synthesis. But here is a counterintuitive fact: the same model placed in different execution frameworks can perform very differently. It is like hiring a highly fluent translator who, when relaying a message, drops key details and fails to ask follow-up questions—the client still walks away with a distorted version. The problem is not language proficiency; it is the workflow.

    A research team at a cloud vendor observed a public benchmark of one hundred challenging terminal tasks. With one common terminal execution framework paired with a given model, the average single-run pass rate across four repeated runs was about 50%, while the proportion of tasks that succeeded on all four runs was below 40%. In other words, more than one-third of the tasks failed on at least some attempts. The core contradiction emerges: the model has the ability to solve the problem, but the system cannot reliably cash that ability out.

    3. Four Root Causes of Instability

    Attributing failures to specific mechanisms in the execution framework usually comes down to four categories:

    1. Improper observation truncation. Terminal output can easily run to tens of thousands of lines. If the truncation strategy is crude, critical error messages get cut off, and the model is forced to make decisions based on incomplete information.
    2. Unbalanced context management. Keeping too much history crowds the context window and dilutes attention; keeping too little loses early constraints and leads to “forgetting the goal.”
    3. Weak completion detection. Without verifiable termination conditions, the model easily mistakes “I have figured out what to do” for “I have finished the task.”
    4. Missing retry and recovery. After a failure, there is no rollback or state-rebuilding mechanism, so the second attempt continues from a polluted intermediate state and the failure is amplified.

    Together, these four issues create a significant gap between the single-run success rate and the all-runs-pass rate.

    4. Measuring Stability: Turning “Sometimes Works” into Quantifiable Metrics

    You cannot govern what you cannot measure. At a minimum, collect the following metrics:

    • Pass@1: the average success rate across multiple runs;
    • All-green rate (Pass@k): the proportion of tasks that succeed on all k repeated runs;
    • Flip rate: the proportion of tasks whose outcome switches state;
    • Average step count and timeout rate: to check whether defects are being masked by “just trying more times”;
    • Failure stage distribution: to pinpoint whether problems cluster in planning, execution, or wrap-up.

    Illustrative measurement logic (pseudocode):

    1. def evaluate(task, run_fn, k=4):
    2. results = [run_fn(task) for _ in range(k)]
    3. pass1 = sum(results) / k
    4. all_green = all(results)
    5. flips = any(results[i] != results[i-1] for i in range(1, k))
    6. return {"pass@1": pass1, "all_green": all_green, "flip": flips}

    5. A Continuous Improvement Loop: Let the Execution Framework Evolve Itself

    One workable approach is to keep improving the execution framework itself without modifying model weights. The loop can be summarized in four steps:

    1. Sampling and filtering: run the same task multiple times and pick out the samples that “sometimes succeed and sometimes fail”;
    2. Differential attribution: use successful trajectories as a reference, align the step sequences of failed trajectories, and locate the first point of divergence;
    3. Mechanism mapping: map the divergence point to a specific mechanism, such as observation truncation, context compression, or completion detection;
    4. Borrowing and patching: draw on proven design ideas from other frameworks to replace or enhance the mechanism, then run regression validation.

    Illustrative implementation of differential attribution:

    1. def first_divergence(success_trace, fail_trace):
    2. for i, (a, b) in enumerate(zip(success_trace, fail_trace)):
    3. if a.signature != b.signature:
    4. return i, a, b
    5. return None

    6. Engineering Checklist

    • Introduce verifiable signals for termination conditions (tests passing, artifacts existing, API return codes) instead of relying on the model’s self-report;
    • Use structured truncation of “head + tail + key error lines” for observation output to preserve failure clues;
    • Apply layered summarization for context, and lock long-term constraints into system-level prompts that cannot be dropped;
    • Roll back to a clean snapshot on failure, and never retry from a polluted state;
    • Wire stability metrics into logging services and monitoring alerts to enable continuous regression;
    • Separate training from evaluation: solve stability problems at the framework layer first, and avoid blindly changing the model.

    7. Conclusion

    The essence of a flipping task is the gap between the model’s capability and the system’s ability to deliver on it. Treating the execution framework as a first-class citizen—measuring it, attributing its failures, and iterating on it—often improves delivery certainty more than continuing to pile on model capability. For teams looking to deploy agents in enterprise environments, building quantifiable stability metrics and a closed-loop improvement mechanism is the key path from “occasional success” to “reliable delivery.”