AI Agents That Write Code and Operate Computers: A Technical Breakdown of Multi-Model Co-Evolution
This article explains how AI agents move beyond chat to autonomously write code and operate computer interfaces through a four-stage execution loop: task abstraction, sample-driven self-validation, dynamic structure detection, and stateful execution. It details why multiple models improving together is viable, along with engineering essentials, common failure modes, and practical countermeasures.
1. The Starting Point: From Chatting to Getting Work Done
Over the past two years, the primary interaction pattern with large models has been “you ask, it answers.” But the moment enterprises hand real workflows to a model, the requirement changes entirely: it must be able to open a spreadsheet, understand the interface structure, locate the rows that match certain conditions, process them one by one, and self-correct when something goes wrong. Tasks like these are characterized by long chains, strong state, and verifiability—single-turn text generation is naturally insufficient.
A typical scenario looks like this: faced with a worksheet containing hundreds of questions, the model does not “stare” at each cell. Instead, it first abstracts a general-purpose solving function, validates it against a few official samples, confirms the logic is sound, then dynamically identifies every row in the sheet that meets the conditions, and finally completes all entries in batch. The key to the whole process is not that “a piece of code was generated,” but that the loop of generate–validate–execute–write back holds together.
2. Core Mechanism: The Four-Stage Execution Loop
2.1 Task Understanding and Plan Abstraction
The model first converts a natural-language goal into an executable plan: what the input is, where the output lands, and what the decision criteria are. The most important artifact at this stage is not code, but a constraint checklist (data scope, boundary conditions, failure handling).
2.2 Sample-Driven Self-Validation
Before doing anything real, run a small set of samples with known answers and compare the output against expectations. This is a low-cost way to “front-load unit testing,” and it significantly reduces the side effects of repeated trial-and-error on real data.
def solve(record):# Normalize input to avoid nulls and format differencestext = (record.get("q") or "").strip()if not text:return None# Business logic and computationreturn {"id": record["id"], "answer": compute(text)}# Sample self-validationsamples = load_official_samples()assert all(solve(s)["answer"] == s["expected"] for s in samples)
2.3 Dynamic Interface/Data Detection
Real-world sheets and interfaces are “alive”: row counts vary, column order may change, and there are merged cells and hidden rows. The reliable approach is to probe the structure first, then locate targets, rather than hard-coding coordinates or row numbers.
def iter_targets(sheet, predicate):header = sheet.detect_header()for row in sheet.iter_rows(min_row=header.end + 1):if predicate(row):yield row
2.4 Execution and State Write-Back
Persist after processing each item, recording status (pending/success/failure/needs manual review) so that interrupted runs can resume. This step determines whether the system is actually usable in real business settings.
3. Why “Multiple Models Improving Together” Works
Placing eight models with differing capabilities under the same task framework is not about ranking them, but about forming an evaluation ecosystem of mutual comparison:
- Differentiated problem exposure: Some models excel at code structure, others at interface semantic understanding; their weaknesses differ. Aggregating them helps pinpoint ambiguities in the task design itself.
- Unified scoring criteria: Use shared metrics—“sample pass rate, target-row recall, first-attempt success rate, average number of rollbacks”—to avoid subjective impressions.
- Failure sample feedback: Turn failure cases into new samples and constraints, so that in the next round all models answer under a clearer problem statement, and the overall level rises accordingly.
- Cross-validation: One model’s execution trace can serve as reference evidence for another, reducing single-point hallucination.
It is worth emphasizing that this progress comes from improvements in task definition and validation mechanisms, not from any model being “specially tuned.”
4. Engineering Implementation Essentials
- Sandboxing and least privilege: Run code execution and interface operations in an isolated environment, granting only the minimum permissions needed to complete the task, and prohibiting outbound connections and unauthorized writes.
- Idempotency and rollback: Repeated execution produces no side effects; take a snapshot or backup before write operations to enable rollback on failure.
- Full-chain observability: Record the plan, code, validation results, interface screenshots or structure snapshots, elapsed time, and failure reasons to form an auditable trace.
- Cost control: Perform structure probing and sample validation first, then enter batch execution; submit long tasks in segments to avoid unnecessary recomputation.
- Human-in-the-loop gates: Set manual confirmation checkpoints for operations involving monetary amounts, permissions, or external publishing.
- Cloud support capabilities: You can leverage container platforms to host sandboxes, object storage to save inputs, outputs, and snapshots, message queues for task distribution, and logging services with monitoring and alerting to ensure observability. These are all general-purpose capability categories that can be combined as needed.
5. Common Failure Modes and Countermeasures
- Sample overfitting: Too few samples, or samples that are too clean, causing real data to break on contact. Countermeasure: add boundary samples (nulls, extremely long text, duplicates).
- Overly strong structural assumptions: Hard-coding column order or row numbers. Countermeasure: detect headers and data types at runtime.
- Silent failure: Errors occur without raising exceptions, producing results that look plausible. Countermeasure: enforce assertions and sample-check results.
- State drift: Context is lost during long tasks. Countermeasure: externalize state storage and recover by batch.
6. Conclusion
The real barrier to “AI writing its own code to operate a computer” has never been generating a script, but making validation, detection, execution, and rollback into a reliable loop. When evaluation criteria are unified and failure samples continuously flow back, multiple models will improve together on the same exam. For developers, now is the right time to integrate these capabilities into internal workflows: start from read-only, low-risk, verifiable scenarios, gradually expand the boundaries of automation, and build a secure, auditable, and continuously evolving intelligent execution system on top of general cloud infrastructure such as Baidu AI Cloud.
