Why Does the Same AI Agent Succeed Sometimes and Fail Other Times? Breaking Down the Stability Problem in Execution Frameworks

    This article examines a common but puzzling behavior in AI agent systems: flip tasks, where the same model, configuration, and tools produce success on one run and failure on the next. It explains why this instability usually originates not in the model's reasoning stage but in the execution framework, or harness, that sits between the model and the environment. The article breaks down four root causes—observation truncation, inconsistent context compression, overly loose completion detection, and missing error recovery paths—and outlines a four-step closed loop for improving the framework itself, from sampling and trajectory alignment to mechanism attribution and regression validation. Readers will also find a practical checklist for making agent execution more deterministic and reliable.

    1. The Symptom: The Capability Is There, but the Success Rate Keeps Fluctuating

    In engineering practice, developers often run into this situation: you ask an AI assistant to write code, run a script, or complete a set of terminal operations. The first run is flawless. Run the same task again later, and it stalls halfway through, or even “believes it has finished” when the goal has not actually been met.

    Tasks like this can be defined as flip tasks: under the same model, the same configuration, and the same set of tools, repeated executions switch back and forth between success and failure. The defining characteristic is not a lack of capability but unstable capability. The model has the knowledge required to solve the problem, yet the system cannot reliably turn that capability into results.

    To understand why, it helps to break a single agent task execution into three stages:

    1. The model handles reasoning and decision-making, producing the next action.
    2. The execution framework translates that action into real commands, runs them, and collects the output.
    3. The environment returns state, and the framework trims and organizes that state into context to feed back to the model.

    The flip behavior usually occurs in the second and third stages, not the first.

    2. The Overlooked Middleman: The Execution Framework

    In an agent system, there must be a runtime layer between the model and the real world. The industry commonly calls it a harness; in Chinese-language contexts it is often translated as execution framework or agent shell. It carries four responsibilities:

    • Command execution: turning the model’s structured actions into real shell commands or tool calls;
    • Observation feedback: collecting signals such as standard output, error output, exit codes, and file changes;
    • Context management: deciding which historical information to keep, which to compress, and which to discard;
    • Completion detection: judging whether the task has truly ended and whether the goal has been achieved.

    Over the past few years, industry investment has been concentrated almost entirely on “making the model smarter,” with methods such as reinforcement learning, trajectory synthesis, and reasoning enhancement emerging one after another. But here is a counterintuitive fact: the same model placed in different execution frameworks can perform dramatically differently. It is like hiring a translator with excellent foreign-language skills—if he frequently drops key information while relaying messages and fails to ask follow-up questions when he should, what the client hears is still distorted. The problem is not the language proficiency but the workflow.

    A cloud vendor’s team made an observation on a public benchmark containing 100 challenging terminal tasks: running a strong reasoning model with a common terminal execution framework, the average single-run pass rate across four repeated runs was about 50%, while the proportion of tasks that succeeded in all four runs was only about 39%. In other words, more than a third of the tasks failed in some of the attempts. The implication is clear: the bottleneck is not necessarily inside the model—it may well be in the translator’s craft.

    3. Four Root Causes of Flip Tasks

    Aligning failed trajectories with successful ones makes it possible to group the sources of instability into four categories.

    3.1 Observation Truncation and Information Loss

    Terminal output can be extremely long. If the framework truncates it at a fixed length, key errors, paths, or line numbers may be cut off exactly. The model cannot see the real signal and has to guess, so the same task takes different branches across different runs.

    Direction for improvement: prioritize keeping error messages and tail output, provide structured summaries (such as the failing line and exception type), and let the model pull the full log on demand instead of stuffing the context all at once.

    3.2 Inconsistent Context Compression Strategies

    Historical trajectories inevitably need to be compressed in long tasks. If the compression rules depend on incidental runtime factors (for example, remaining budget or concurrency timing), the context produced by different runs will differ, and decisions will naturally drift.

    Direction for improvement: make the compression strategy a deterministic rule that explicitly preserves four elements—the goal, constraints, verified facts, and unresolved issues—so that randomness does not enter the decision chain.

    3.3 Completion Detection That Is Too Loose

    Many frameworks use “the model declares completion” or “no new action” as the termination condition. Once the model develops the illusion that “I am done,” the task is closed prematurely, showing up as a “false success.”

    Direction for improvement: introduce verifiable completion conditions, such as the target file existing, tests passing, or an exit code of zero, and run a self-check action before terminating.

    3.4 Missing Error Recovery Paths

    Whether to retry, roll back, or switch paths after a failure is often determined by the framework’s implicit defaults. Inappropriate defaults can amplify a small, recoverable error into a full task failure.

    Direction for improvement: explicitly define retry limits, backoff strategies, and rollback points, turning recovery logic from “incidental behavior” into an “established process.”

    A schematic version of the detection logic can be written as:

    1. def is_task_done(state):
    2. # Do not rely on the model's self-report; rely on verifiable signals from the environment
    3. return (
    4. state.exit_code == 0
    5. and state.target_artifact_exists
    6. and state.self_check_passed
    7. )

    4. An Improvement Approach: Let the Execution Framework Evolve Itself

    For flip tasks, the viable solution is not to keep changing model weights but to build a closed loop with the framework as the optimization target. It can be summarized as a four-step cycle.

    Step 1: Sampling and filtering. Run the same task repeatedly under the same configuration, and filter out the set of flip tasks whose results are inconsistent. They are the most valuable diagnostic samples.

    Step 2: Trajectory alignment and difference localization. Using the successful trajectory as a reference, compare the failed trajectory step by step to find the first action point where divergence appears, and determine whether the divergence stems from missing observations, context differences, or premature detection.

    Step 3: Mechanism attribution. Map the divergence points to specific framework mechanisms, such as truncation thresholds, compression rules, termination conditions, and retry strategies, forming a candidate list of modifications.

    Step 4: Cross-solution borrowing and regression validation. Refer to common design ideas for similar mechanisms in other frameworks to make fixes, then rerun on the same benchmark to confirm that both the pass rate and the “all-success ratio” improve without introducing new regressions.

    The engineering value of this closed loop is that it turns “incidental failures” into an engineering problem that is measurable, attributable, and regression-testable. In practice, it is recommended to pair it with three foundational capabilities: a unified runtime sandbox and container platform to guarantee environment consistency, a logging service with monitoring and alerting to retain full trajectories, and experiment management with a metrics dashboard to record the success-rate differences before and after each framework change.

    5. A Practical Checklist

    • Fix randomness: fix sampling parameters and execution order so that differences come only from framework mechanisms rather than chance;
    • Verifiable termination: any “completion” must be backed by evidence from the environment;
    • Structured observation: prioritize errors and expand on demand, avoiding indiscriminate truncation;
    • Deterministic compression: compression rules must not drift with runtime state;
    • Explicit recovery strategy: retries, rollbacks, and path switches must all have limits and conditions;
    • Regression baseline: run the same set of flip tasks after every framework change, using the “all-success ratio” as the core metric.

    6. Conclusion

    Flip tasks remind developers of one thing: the stability of an agent system is a systems-engineering problem. The model determines the upper limit of capability; the execution framework determines how much of that limit can be reliably delivered. Only by making the four mechanisms—observation, context, detection, and recovery—explicit and deterministic, and by letting the framework iterate continuously through ongoing regression, can “sometimes successful” become “successful most of the time.”