# Multi-Agent Engineering at Scale: Running Thousands of Agents on Long-Horizon Tasks
A practical engineering guide to long-horizon, multi-agent systems. It explains why the bottleneck shifts from model capability to system engineering once tasks run for days instead of seconds, and walks through four common failure modes (context window limits, error accumulation, spec drift, coordination overhead), a five-layer orchestration model, task decomposition rules, state and conflict resolution, an illustrative orchestration skeleton, cost governance, security and permission design, a multi-dimensional evaluation framework, and a staged rollout path with common anti-patterns.
1. The Narrative Has Shifted: From Model Scores to System Capability
Over the past year, the way frontier models are presented has shifted noticeably: away from single-turn benchmark rankings and toward “agents autonomously completing engineering tasks that take days and involve hundreds or thousands of execution units.” Cases like these — working continuously for days, autonomously dispatching thousands of sub-agents, producing a complete compilable and runnable system from an empty directory — deserve developers’ attention not for their scores, but for a fact they expose:
When task duration stretches from seconds to days, the bottleneck moves from model capability to the engineering system.
Models still matter, but what decides success or failure is the surrounding infrastructure: context management, state persistence, concurrent scheduling, conflict resolution, and verification loops. The sections below follow the order in which these pieces are built in practice.
2. Why Long-Horizon Autonomous Tasks Are Hard: Four Real Failure Modes
1. “Context Window” Is Not “Effective Context”
The window is a capacity ceiling, not effective working memory. As context grows, models use middle-of-context information markedly less efficiently (commonly called “lost-in-the-middle”), and irrelevant history dilutes key constraints. An agent that runs continuously for days and relies on context as its only notebook will inevitably get more confused the longer it works.
Engineering countermeasure: treat context as a cache, not primary storage. Persist task state, interface contracts, and completed-work lists to disk; load only the current subtask, the relevant contracts, and the most recent failure reason into context on each execution.
2. Error Accumulation: The Mathematics of Long Chains
High per-step accuracy still collapses over long chains. With a 95% single-step success rate, the probability that 100 consecutive steps are all correct is roughly 0.95¹⁰⁰ ≈ 0.6%. This is exactly why “the demo runs, but production is off-limits.”
Engineering countermeasure: don’t chase a single long chain. Cut the work into many short chains plus verifiable checkpoints. Give every step a machine-checkable gate, turning a 100-step chain into 100 independently verifiable units so that errors surface locally.
3. Spec Drift
In long-running tasks, the original goal gets forgotten — or “quietly optimized away” — after dozens of revision rounds. Models tend to solve whatever problem is in front of them rather than hold global constraints.
Engineering countermeasure: write acceptance criteria as executable files (tests, type definitions, interface contracts, benchmark scripts) rather than natural-language descriptions. Specs shouldn’t depend on memory; they should depend on tests that actually run.
4. Coordination Overhead Outgrows the Benefit
Parallelism isn’t free. As the number of sub-agents rises, conflicts, duplicated work, and merge cost grow non-linearly. There is an optimal degree of parallelism; beyond it, adding agents reduces throughput.
3. A Reference Architecture: The Five-Layer Orchestration Model
For a multi-agent system you can actually operate, separate responsibilities into strict layers rather than “one do-everything agent”:
┌───────────────────────────────────────────────────┐│ L0 Planning goal → task DAG (with acceptance) │├───────────────────────────────────────────────────┤│ L1 Scheduling concurrency / priority / retries / ││ budget │├───────────────────────────────────────────────────┤│ L2 Execution coding and testing agents inside ││ isolated sandboxes │├───────────────────────────────────────────────────┤│ L3 Verification builds / unit tests / static ││ analysis / code review │├───────────────────────────────────────────────────┤│ L4 State workspace / contract store / memory ││ / audit log │└───────────────────────────────────────────────────┘
Key design principle: L3 must be run by agents that are independent of L2. Letting the agent that writes code evaluate its own code effectively removes quality control. Review agents should use a clean context and see only the diff, the contracts, and the test results.
4. Task Decomposition: Granularity Determines Success
Decomposition quality drives the cost of everything downstream. Three rules of thumb:
- Align granularity with “verifiable time”: each subtask should produce a machine-checkable result (compiles, tests pass, interface signature matches) within 15 minutes to 2 hours. Don’t dispatch tasks that can’t be verified.
- Contracts first: have the planning layer produce interface contracts (function signatures, data structures, error conventions, message formats) before implementations are filled in in parallel. This is the precondition for parallelism — without contracts, parallel work only manufactures conflicts.
- Every task carries four fields: inputs, outputs, constraints, and an acceptance command. A task without an acceptance command counts as an incomplete spec.
Task dependencies naturally form a DAG (directed acyclic graph). When scheduling, identify the critical path, guarantee resources for tasks on it first, and allow non-critical branches to be degraded or deferred.
5. State Management and Conflict Resolution
When multiple agents share one codebase, the most common incident is “overwriting each other.” Four practical constraints:
- Single writer per path (ownership): at any moment, only one agent may write to a given path; the scheduling layer enforces this with leases.
- Optimistic concurrency control: every write carries a version number or content hash; if server-side validation fails, the writer must re-read and retry.
- Frequent, small-step integration: merge each finished subtask to the main branch and run regression immediately, instead of “one big merge at the end.” The cost of resolving conflicts in a large merge rises sharply with how long branches stay alive.
- Externalize workspaces and artifacts: write intermediate outputs, logs, and test reports to external storage such as object storage, partitioned and archived by task ID for traceability and resumability.
6. An Orchestration Skeleton (Illustrative Pseudocode)
# Pseudocode: illustrative structure only, not tied to any specific framework or vendor APIdef run_project(goal: str, budget: Budget) -> Result:# 1) Planning: produce a task DAG with acceptance criteria and interface contractsspec = planner.decompose(goal, contract_first=True)store.commit_contracts(spec.contracts) # L4: persist contracts# 2) Scheduling: topological order + concurrency cap + budget controlwith Scheduler(max_parallel=16, budget=budget,retry=RetryPolicy(max_attempts=3, backoff="exp")) as sch:for task in spec.tasks_in_topo_order():sch.submit(task=task,executor=lambda t: agent_impl(t, sandbox=Sandbox(cpu="2", mem="4Gi",network="deny", # no outbound network by defaultreadonly=["contracts/"], # contracts cannot be modifiedworkdir=f"ws/{t.id}",)),# 3) Gate: failing the gate means the task is not donegate=lambda t: gate.run_all([build.check, unit_test.run(t.acceptance),lint.check, typecheck.run,]),on_fail=planner.replan_with_failure_context, # replan with the failure reason)# 4) Review: independent context + independent agentreviews = [reviewer.diff(r, spec.contracts) for r in sch.merged_changes]blocking = [r for r in reviews if r.severity == "blocking"]# 5) Global acceptance: executable specs are the source of truthreturn Result(ok=not blocking, report=store.finalize())
The core of this skeleton isn’t the API but three hard constraints: a failed gate means the task isn’t done, no network by default, contracts are read-only.
7. Cost Governance: Measure “Cost per Passing Result,” Not “Price per Token”
The bill is what long-running tasks most often underestimate. A practical cost model:
Total cost ≈ Σ(subtasks × input/output tokens per task × tier price) + retry overhead + storage and compute time
In long-running tasks, “retry overhead” is usually the largest variable — one failed replanning round can burn the budget of dozens of normal executions. So the optimization priority should be: reduce failure rate > increase cache hit rate > pick a cheaper model tier.
Four actionable cost-reduction levers:
- Tiered routing by reasoning intensity: classify tasks into retrieval, rewriting, unit-test generation, architecture design, and so on; lightweight tiers handle the earlier categories, and high-reasoning tiers go only to genuinely hard problems. In practice, medium reasoning intensity already covers most subtasks, and only a few critical tasks are worth the top tier, so end-to-end cost can be significantly lower than a “top tier for everything” approach.
- Prefix and result caching: contracts, codebase summaries, and repeated build artifacts should hit the cache stably; make sure the cache key includes dependency versions, otherwise stale results come back.
- Fail fast: run compilation and unit tests (milliseconds to seconds) before triggering expensive long-chain reasoning. Putting cheap checks ahead of expensive reasoning is the most effective stop-loss point.
- Hard budgets and circuit breakers: set token and cost caps per subtask, per branch, and for the whole project; on breach, pause and report instead of silently burning through.
8. Security and Permissions: The Same Underlying Capability, Different Policy Layers
Frontier model products generally ship in tiers using a “same base model + different policy layers” pattern: the public-facing version tightens abuse-prevention restrictions, while the institutional version that passes review retains more complete capabilities in high-risk domains. This is a pattern worth borrowing for any team building agent platforms — capability and safety policy must be decoupled.
At minimum, the engineering side should implement five things:
- Sandbox isolation: one independent container per execution unit, base image mounted read-only on the filesystem, the codebase mounted as a copy, destroyed as soon as execution ends.
- Minimal network access: deny outbound network by default, opening only an allowlist of dependency sources on demand; this keeps the model from pulling in unreviewed external code during generation.
- Short-lived credentials: use least-privilege, short-lifetime tokens, and never inject long-lived keys into the agent’s context.
- End-to-end audit: log each sub-agent’s inputs, actions, artifacts, and duration to a logging service with monitoring and alerting configured; anomalies (retry spikes, single-task timeouts, sudden cost increases) should automatically trigger human intervention.
- Human-in-the-loop gates: irreversible or high-impact actions (deleting data, changing production configuration, external publishing) must require mandatory human approval and cannot be completed autonomously by the agent.
9. Evaluation: Don’t Judge by a Single Aggregate Score
A leaderboard’s aggregate score has limited value for engineering decisions because it hides variance. Build your own multi-dimensional metrics:
| Dimension | Metric | Notes |
|---|---|---|
| Correctness | Task pass rate / first-pass rate | First-pass rate best reflects real cost |
| Stability | Completion rate without human intervention over long runs | Share of runs that don’t break over 8h / 24h continuous operation |
| Economics | Cost per passing test case | Not price per token |
| Maintainability | Average rework rounds, review blocking rate | Reflects the quality of the output |
| Isolation | Blocked unauthorized-action rate | Effectiveness of the security policy |
Two points matter. First, the evaluation set must be close to a real codebase (legacy code, messy dependencies, incomplete documentation); synthetic tasks systematically overestimate capability. Second, keep a private, unpublished evaluation set to avoid overfitting to public leaderboards.
10. Rollout Path and Common Anti-Patterns
Staged adoption is the key to controlling risk:
- Stage 1: don’t introduce multi-agent orchestration yet. First build up test infrastructure, CI, type checking, and static scanning. Putting multi-agent systems on top of weak test infrastructure just produces wrong code faster.
- Stage 2: low parallelism (4–16 execution units) plus mandatory human review, getting DAG scheduling, externalized state, and gating mechanisms working.
- Stage 3: scale into the hundreds; at that point the investment focus shifts to isolation strength, cost observability, and failure self-healing.
On the infrastructure side, a container platform provides elastic isolated execution environments, object storage carries workspace and artifact archiving, a message queue handles task distribution and peak shaving, logging plus monitoring and alerting form the observability base, and a secret manager handles credential lifecycle — these general-purpose capability categories are enough to support a complete multi-agent engineering stack, with no proprietary components required.
Five high-frequency anti-patterns to check yourself against:
- Over-decomposition: subtasks so fine-grained that coordination overhead exceeds execution overhead.
- No acceptance criteria: describing “make it a bit better” in natural language, with no way to determine completion.
- Self-review: the same agent and the same context write and review the code.
- Big-bang merge at the end: all branches stay alive until the end and conflicts erupt all at once.
- Judging by average score only: ignoring variance and the long tail, which makes production behavior unpredictable.
Conclusion
The value of cases like “thousands of agents working continuously for days” isn’t proving how strong a particular model is; it’s validating something else: once reliable state management, verification loops, and permission isolation are in place, agents can genuinely take on engineering tasks that traditionally require a team working for weeks. The barrier to this capability lies mainly on the engineering side, not the model side.
For development teams, the pragmatic path is: build deterministic tests and gates first, then orchestration and parallelism, and scale last. Reverse that order and scale will only amplify the chaos.
