Engineering Practices and Evaluation for AI Agents in Long-Running Tasks

    As large language models move from single-turn Q&A to multi-step execution, AI agents can now take a development requirement, break it down into tasks, modify files, run commands, and adjust based on results. For development teams, judging whether an AI coding tool is usable means looking beyond whether it can write one correct function—what matters is whether it stays on track across long task chains, handles errors properly, and keeps costs within acceptable limits. This article takes an engineering perspective on how to evaluate these capabilities and provides a reproducible testing methodology.

    New Characteristics of Long-Running Tasks

    Traditional code completion involves a narrow interaction loop: the developer provides context, the model returns a snippet, and a human decides whether to accept or discard it. Long-running tasks are different. A single requirement often spans the UI, data fetching, state management, and testing. The agent must read the outcome after each step, determine whether the goal was met, and decide on the next action.

    This pattern introduces three new categories of problems. The first is context continuity: at step ten, does the agent still remember the interface conventions and directory structure established at step one? The second is error recovery: dependency installation failures, test errors, and port conflicts are almost inevitable, and the agent must diagnose root causes from error messages rather than rerunning the same command repeatedly. The third is cost control: every inference round consumes compute resources and usage quota, and a loop without termination conditions can rapidly amplify spending.

    How the Iterative Loop Works

    Most agents are built around a “perceive, plan, execute, observe, reflect” loop. Understanding this loop helps in designing better constraints.

    1. def run_agent_task(goal, workspace, max_steps=50):
    2. state = init_state(goal, workspace)
    3. for step in range(max_steps):
    4. plan = planner.next_action(state)
    5. if plan.type == "done":
    6. return verify(state)
    7. result = executor.run(plan, sandbox=workspace)
    8. state = state.update(plan, result)
    9. if state.repeated_failures(threshold=3):
    10. return escalate_to_human(state)
    11. return escalate_to_human(state)

    This illustrative code highlights three engineering points. First, the loop must have an upper bound to prevent endless resource consumption. Second, completion should be determined by verifiable conditions—such as all tests passing—rather than declared by the model itself. Third, when consecutive failures reach a threshold, the task should be handed back to a human, because repeated errors on the same problem usually indicate that the agent’s understanding has drifted from reality.

    Iterating and Fixing Within Existing Projects

    When working on an existing codebase, an agent’s strength lies in reading the existing structure, naming conventions, dependency versions, and test cases, then producing changes consistent with the project’s style. Compared with generating a new project from scratch, these tasks are easier to validate because a clear reference exists.

    The risks are concentrated here as well. The larger the scope of changes, the harder they are to review. For this reason, define the directories and file types the agent is allowed to modify before the task begins, require the agent to split commits by feature, and use version control to keep a snapshot of every step. Operations such as deleting files, modifying configuration, or changing dependencies should require a human confirmation checkpoint.

    Evaluating Automated Testing Capability

    Having an agent write and run end-to-end tests is an effective way to assess its execution ability. The testing process typically involves launching the application, simulating user actions, checking page elements and state, and fixing code based on failure messages. During evaluation, keep the test environment isolated, using separate data and test accounts so that accidental operations cannot affect production data.

    Evaluation can focus on the following metrics:

    • First-pass rate: the proportion of tests that pass on the first run without human intervention.
    • Fix iterations: the number of iterations required from the first failure to the final pass.
    • Unrelated-change rate: the proportion of runs where the agent modifies files unrelated to the task or introduces new defects.
    • Report quality: whether the agent’s conclusions after testing match the actual run results.

    A Cost-Effectiveness Evaluation Framework

    Looking only at benchmark scores leads to one-sided conclusions. If a model scores slightly higher on a leaderboard but each task costs substantially more, real production efficiency may not improve. A more meaningful metric is cost per unit of accepted output:

    Cost per accepted task = Total cost (fees, quota, time) ÷ Number of tasks that pass acceptance

    Total cost should include rework. If the agent’s code requires extensive manual patching, each call may look cheap, but the overall economics are poor. Quota management also matters. Long-running tasks typically consume far more quota than single-turn conversations, so teams should record actual consumption per task type during the trial phase and budget accordingly for production use.

    Testing Stability and Environment Awareness

    Stability, instruction following, exception handling, and environment awareness are four dimensions that can each be tested with independently designed cases:

    1. Stability: run the same task multiple times and measure consistency. Large variance across runs indicates weak output controllability.
    2. Instruction following: add explicit constraints to the task description—such as “do not modify the configuration directory” or “follow the specified output format”—and check whether they are respected.
    3. Exception handling: inject faults deliberately, such as cutting off a dependency source, creating permission errors, or triggering port conflicts. Observe whether the agent identifies the problem, proposes reasonable alternatives, and stops and reports when it cannot resolve the issue.
    4. Environment awareness: require the agent to detect the operating system, runtime version, and available tools before deciding how to execute. Agents that correctly identify environmental differences are more reliable when deployed across machines.

    For every dimension, record the inputs, process logs, final results, and human scores to build a comparable dataset.

    Recommendations for Adoption

    First, apply the principle of least privilege. The agent should only access the directories, network resources, and credentials required for the task. Inject secrets through environment variables or a secrets management service—never embed them in code or logs.

    Second, run high-risk operations in a sandbox. Database changes, deployments, and bulk deletions should be validated in an isolated environment and confirmed by a human before execution.

    Third, keep complete audit logs. Record every instruction, tool call, and returned result to support later review and accountability.

    Fourth, clarify the division of labor between humans and agents. Agents are well suited to repetitive implementation, testing, and fixing work, while requirement definition, architectural trade-offs, and release decisions remain with engineers.

    Overall, the value of long-running agents depends on engineering discipline—constraints and evaluation—rather than on a model’s leaderboard ranking. Only after a team establishes a shared test suite, cost metrics, and security boundaries can it make objective choices among different tools.