Computer-Use Agents: Moving from Batch Instructions to Code Execution

    This article examines the shift in computer-use agents (CUAs) from batch instruction lists to code execution. Using a spreadsheet task where one model confidently answers only 85 of 125 questions, it explains why treating a task as a verifiable program outperforms a fixed action list. It covers the differences between the two interaction paradigms, why code execution benefits all models, a five-layer architecture for building code-executing agents, context and state management, security and permission boundaries, evaluation methods, and common pitfalls to avoid.

    Computer-Use Agents: Moving from Batch Instructions to Code Execution

    1. What a “Stopped After 85 Answers” Case Reveals

    Consider a spreadsheet task: a worksheet contains 125 questions that need answers. A model generates a program that looks well-structured, with loops and function encapsulation. When it finishes, it prints “computed answers: 85.” The code throws no errors and the logs are clean, but the range of rows it scanned never covered the location of the remaining 40 questions. More importantly, the model is completely unaware that it did 40 fewer questions; it believes it has completed the task.

    Give the same task to a more capable model and the behavior path is entirely different: it first writes a general-purpose solving function, validates it against the seven sample cases provided officially, confirms the logic is correct, then dynamically identifies all qualifying question rows in the worksheet. In the end, all 125 questions are completed, with a verifiable summary result.

    The difference between these two trajectories cannot be summed up by a vague conclusion like “who is smarter.” It reflects a methodological difference in how an agent operates a computer: the former treats the task as a one-time generated action list, while the latter treats it as a verifiable program that can branch and be traced back. Starting from this difference, this article systematically breaks down the drivers, mechanisms, and engineering implementation points behind the migration of computer-use agents (CUAs) from the batch instruction paradigm to the code execution paradigm.

    2. The Essential Difference Between the Two Interaction Paradigms

    Over the past few years, the default approach to building computer-use agents has been: capture a screenshot, let the model “look at it,” then output where to click and what to type. The advantage of this pure GUI interaction method is its generality, requiring almost no cooperation from the target system. The drawback is that every step depends on visual understanding, and errors accumulate along the operation sequence.

    In the last two years, a hybrid approach has gradually become the mainstream direction: let the model directly write a small program that uses GUI automation libraries or browser automation libraries, mixing interface operations and programmatic operations in the same piece of code. This change may look like an implementation detail, but it is actually a turning point in interaction logic. It can be compared across three dimensions:

    • Batch action mode: the model generates a fixed sequence of instructions all at once, for example “click A, type B, click C.” The instructions are fully determined at the moment they are generated and cannot be adjusted based on intermediate results during execution.
    • Code execution mode: the model writes a real program that can include conditional logic (close a popup if a certain type appears), loops (fill three fields for each row in a table), and validation (check whether the state actually changed to “saved”). Only when the program reaches a given step does it decide what to do next.
    • Failure semantics: in batch mode, when a precondition for a step does not hold, subsequent steps still execute mechanically and errors are silently amplified. In code mode, a failed precondition can trigger a branch, a retry, or an explicit error, so failure is visible.

    An everyday analogy: ask an assistant to update every qualifying customer record in a file into a system one by one. Batch mode requires you to write out in advance a detailed list of where to click and what to type at each step, and the assistant follows it exactly, continuing even when the precondition at step three does not hold. Code mode only requires stating one rule: open each record one by one, check the status field, update if pending, skip if already processed, and summarize the number of processed records when all are done. The rule operates at a higher level of abstraction and has stronger fault tolerance and adaptability.

    3. Why Code Execution Brings “Collective Progress”

    A common misconception needs clarifying here: code execution is not an exclusive capability of any single model, but a way of representing a task. Once a task is represented as a program, the benefits are shared across models, for three reasons.

    First, verification cost is spread out. Sample-driven development lets a model first validate a function’s logic against a small number of known answers, decoupling errors in “understanding the problem” from errors in “performing operations.” The former is exposed at the sample stage, while the latter can be located during execution.

    Second, intermediate artifacts are reusable. Functions, selectors, and parsing rules accumulate as retrievable context, so later tasks do not have to reason from scratch, and even weaker models can build on existing results to get the work done.

    Third, errors are locatable. A program has clear line numbers and variable states, so debugging shifts from “guessing intent from screenshots” to “reading the stack to locate the issue,” which directly lowers the debugging overhead of the overall system.

    4. Building a Code-Executing Agent: A Layered Architecture

    To put this paradigm into practice, a five-layer structure is recommended:

    1. Perception layer: responsible for acquiring the interface structure and document structure, including the interface element tree, accessibility information, and the row and column coordinates of tables. Visual screenshots serve only as a fallback, not as the sole basis.
    2. Planning layer: transcribes the natural-language goal into “rules + acceptance criteria,” clarifying the input scope, filter conditions, termination conditions, and output format.
    3. Code generation layer: produces an executable program, mandatorily including parameter validation, exception handling, and logging instrumentation.
    4. Execution sandbox layer: runs the program in a controlled environment, restricts the scope of file system and network access, and automatically terminates on timeout.
    5. Validation and rollback layer: after execution, compares expected versus actual differences and outputs a structured report; on failure, rolls back to checkpoints.

    Illustrative code (pseudocode that only expresses structure and does not point to any specific tool):

    1. def run_task(sheet, rule, samples):
    2. solve = build_solver(rule) # Generate a general-purpose solving function
    3. assert all(solve(s) == a for s, a in samples) # Sample validation
    4. rows = detect_target_rows(sheet, rule) # Dynamically identify target rows
    5. results = []
    6. for r in rows: # Loop over rows instead of a fixed range
    7. try:
    8. results.append((r, solve(read(sheet, r))))
    9. except Exception as e:
    10. log(r, e) # Failures are visible, not silently skipped
    11. report(len(rows), len(results)) # Summarize for easy verification
    12. return results

    5. Context and State Management

    The part of long tasks most prone to problems is not reasoning but state drift. Practical points include: persist completed, pending, and failed items separately to avoid duplicate processing; keep a checkpoint for each step to support resuming from a breakpoint; use stable structural locators rather than volatile coordinates for interface elements; split large documents into retrievable fragments and load them on demand to control context length; and write all intermediate conclusions into structured logs for later auditing.

    6. Security and Permission Boundaries

    Code execution means greater capability and greater risk. The principle of least privilege must be followed: default to read-only, and require explicit authorization for write operations; set up secondary confirmation and threshold alerts for actions involving funds, permissions, and bulk deletion; prohibit access to unnecessary network addresses within the sandbox; perform static checks on generated code to intercept dangerous calls; and retain a complete operation audit trail that is traceable and rollback-capable. In enterprise scenarios, container platforms can be used for isolation, logging services and monitoring alerts can handle auditing and anomaly detection, and object storage can hold execution artifacts and checkpoints.

    7. Evaluation Methods: Don’t Look Only at the Final Answer

    Evaluation should cover four levels: result correctness (whether everything was processed and whether the numbers can be verified), process soundness (whether sample validation and scope identification were performed), robustness (whether it can recover when the interface changes, popups interfere, or some rows are abnormal), and security (whether it overstepped permissions or performed dangerous operations). It is recommended to build an adversarial sample set containing cases of “surface completion but actual omission,” specifically to test whether a model will confidently do 40 fewer questions as in the opening example.

    8. Common Pitfalls and How to Avoid Them

    One is hard-coding a fixed row range; instead, dynamically identify the target scope so the program does not silently miss rows outside a preset interval.