How to Build a Multi-Agent Orchestrator from Scratch (Part 2)

  • Erwin Part 1 just crossed 500K views! Yet if you only wire up those basics, all you have is JUST A DEMO TOY. Part2 shows you the real engineering philosophies to take your orchestrators to REAL PRODUCTION. Article cover image

    How to Build a Multi-Agent Orchestrator from Scratch (Part 2)

    8 min read In Part 1, I broke down the foundational modules of a multi-agent orchestration tool. You can revisit Part 1 here. https://x.com/ErwinWu000/status/2127418399672463751 Once those foundational modules work, what you have is STILL A DEMO TOY. The moment you drop coding agents from different vendors into the same repository to work in parallel, you hit much harder production engineering problems.

    How does an orchestrator actually drive different native harnesses? How do you isolate files and control workflows under multi-agent concurrency? This post walks through the second half of building an orchestration layer from scratch, covering the pieces that determine whether it survives real production work.


    MCP? ACP? How do you drive a harness?

    First, let's clear up a point that might confuses you. Part 1 covered how an orchestrator lets a model invoke the orchestrator's own capabilities. Here, we are talking about the opposite direction- how the orchestrator controls underlying native harnesses like Claude Code. In Part 1, I broke down the foundational modules of a multi-agent orchestration tool. You can revisit Part 1 here. Mapped to protocols, MCP is an outward tool protocol where the orchestration tool acts as a server and exposes collaboration capabilities for the model to call. ACP is a downward control protocol where the orchestration tool acts as a client to drive and interrupt the underlying harness while taking over its session stream and permission callbacks. The diagram below makes this directional difference clear. When it comes to how orchestrators drive harnesses, engineering practice has moved through three stages. Stage 1. Headless CLI The earliest practical approach was calling each provider's headless CLI for structured output. The orchestrator sends a command, and the underlying agent streams its reasoning, tool calls, and final response back over stdout. It is simple to wire up, but at its core it is still a one-way pipeline built around discrete commands and turns. Anything requiring dynamic input injection or blocking permission approvals jams the pipe easily. Early bridge tools that integrated coding agents into Telegram, or Slack relied on this pattern, and anyone who used them knows how sucks it felt. In Part 1, I broke down the foundational modules of a multi-agent orchestration tool. You can revisit Part 1 here. Stage 2: native SDKs Next, providers began shipping native SDKs and local server protocols. Bidirectional interaction finally worked smoothly, making steering and dynamic parameters possible. Orchestration tools quickly ran into a different wall, though. Every provider's SDK looks completely different, so supporting five or six mainstream agents meant writing and maintaining five or six separate adapter layers. Stage 3: ACP The newest approach is ACP (Agent Client Protocol), now gaining traction in the community. You can think of it as LSP for coding agents, wrapping provider-specific SDK capabilities behind one uniform interface. What makes ACP even more interesting is that it does not stop at messaging and replies. It also pulls surrounding capabilities like file io, terminal execution, and permission approval requests into the same standard protocol.

    To be realistic, ACP adoption across agents is still far from complete. Because of that, mature orchestration tools have converged on a pragmatic split. They keep the top tier of flagship harnesses like Claude Code and Codex on their native private protocols to guarantee full feature coverage, while routing other harnesses through ACP to cut down on adapter maintenance.


    Key Problem: Managing Multi-Agent Concurrency

    Once you have multiple agents running, the next immediate problem is obvious. They are all editing the same repository, so how do you stop them from stepping on each other? Ordered from narrowest to broadest isolation, there are three tiers of technical solutions. In Part 1, I broke down the foundational modules of a multi-agent orchestration tool. You can revisit Part 1 here.
    • Directory locks and file locks are the most basic idea in computer science. The orchestrator keeps a ledger, locks auth/ the moment an agent claims it, and guarantees zero concurrency on the same file or folder. It is trivial to implement, yet it destroys parallelism (especially when agents take far longer to run than normal programs), and still lets Git staging areas and build artifacts collide across shared modules..
    • Git worktrees are the most common layer of isolation today. The repository gets split into separate working copies so each agent edits freely inside its own worktree, then merges back into main and resolves conflicts into a single codebase. However, worktrees only isolate source files on disk and do nothing about shared runtime resources. Two agents can still fight over port 8088, overwrite global caches, or share the same local API credentials.
    • Worktrees in Docker containers take isolation one step further by placing each working copy inside its own container. At this stage, source files, ports, and environment variables are genuinely separated, at the cost of noticeably heavier memory overhead.

    Is Isolation a good choice?

    Use these tools long enough and you realize something humbling. Even when every agent is locked inside its own container, logical conflicts STILL happen. A backend agent refactors a response field on its branch while a frontend agent writes calls against the old field on another branch. Both branches look completely clean in isolation, and the build breaks the second they merge. Trying to invent clever mechanism to catch every logical conflict is high effort with low payoff. The smarter move is to borrow directly from how experienced human engineering teams keep dozens of developers from colliding in a large repo. Human teams solved this years ago, and an orchestration tool simply needs to bake those norms into its scheduling logic. In Part 1, I broke down the foundational modules of a multi-agent orchestration tool. You can revisit Part 1 here.
    • CONTRACT FIRST. Before dispatching parallel tasks, add an upfront step that locks down shared interface definitions or type files. Downstream workers build in parallel against that same contract while those interface files stay read-only. This single rule eliminates roughly 80% of integration conflicts.
    • Directory ownership boundaries. Just like CODEOWNERS in a large team, define explicit writable path whitelists for each worker. If a worker tries to touch shared files like package.json, blocks the edit and routes it to the coordinator to handle centrally.
    • Merge Queue. Workers never merge directly into main. Every completed branch enters a merge queue where the orchestrator rebases them onto the latest main one by one and runs CI. If a later branch conflicts, the orchestrator sends the conflict context straight back to that worker for a targeted fix.

    Automation Philosophy: Trade-Off Between Determinism and Autonomy

    Once production concurrency is under control, your software works well, but hit the central systems design tension. How rigid should the workflow be, and how much freedom should you leave to the AI? This is a classic automation dilemma. Lock every step down, and the agent cannot adapt when an unexpected error or requirement change shows up. Hand full control to the model, and it skips norms, forgets reviews, or traps two agents in an endless useless loop. Plot mainstream tools along a spectrum from determinism to autonomy, and three camps emerge. 1. Deterministic graph orchestration (nodes, edges, and DAGs). This space starts with pioneers like LangGraph and extends to tools like Microsoft Conductor that define DAGs (Directed Acyclic Graphs) via declarative configs. Developers hardcode state schemas, execution nodes, and transition conditions into an explicit graph. Execution is predictable, easy to audit and replay, and routing burns zero extra tokens. It shines in well-defined business state machines, but coding is high-exploration work where you can never enumerate every branch upfront. Hardcoding an exploratory process quickly costs more to maintain than it saves. In Part 1, I broke down the foundational modules of a multi-agent orchestration tool. You can revisit Part 1 here. 2. Hybrid orchestration (deterministic outer frame with an autonomous inner loop). This is currently the sweet spot for coding orchestrators, with tools like Orca's DAG scheduling and T3 Code's workflows moving in this direction. The outer layer uses a deterministic state machine to lock down major phases and hard gates (plan→implement→run tests→independent review→merge), while each node hands off to a native harness so the AI decides how to actually get the work done inside that phase. You keep hard engineering guardrails without stripping away the model's ability to solve messy problems. 3. High-autonomy soft guidance (Prompts and Skills). Instead of hardcoding a state graph, you use system prompts and prebuilt skills to teach a coordinator how to split work and when to consult teammates, much like Claude Code's Agent Teams or the collaboration Skills inside Paseo. This approach has the highest ceiling on ambiguous, open-ended tasks and lets you reshape workflows by editing a few lines of prompts. The cost is a shakier floor that depends heavily on how reliably the model follows instructions.

    There is no universal right answer across the three camps, but the guiding principle is straightforward. Hand dependencies, quality gates, and cost caps to deterministic code, and leave execution freedom to the model.


    Sandbox at the Orchestration Layer

    Flagship harnesses like Codex and Claude Code already ship with OS-level sandboxes, so why not just reuse theirs instead of building another sandbox layer in the orchestrator? Put multiple harnesses together on a real project and you quickly see that native sandboxes alone cannot hold the line.
    • First, sandbox maturity varies wildly across harnesses. Frontier provider CLIs may enforce tight boundaries, but the moment your setup includes a lightweight open-source CLI with a weak sandbox or none at all, your security gate breaks at the weakest link.
    • Second, a native sandbox only protects the host machine and does nothing to keep multiple agents from interfering with each other. Single-agent sandboxes exist to stop an agent from wiping critical host files, yet under cross-harness concurrency they have no visibility into shared caches or local system resources.
    • Third, different agents on a team should not share the same permissions. A builder writing code needs read-write access, while a reviewer doing code review only needs read-only access. Treating every worker identically leaves too much surface area open. Because you are orchestrating across harnesses, you need one unified outer sandbox boundary at the project level that wraps different providers and roles inside a single controllable shell. In Part 1, I broke down the foundational modules of a multi-agent orchestration tool. You can revisit Part 1 here. In practice, that outer sandbox has three core jobs, and they plug directly into the file isolation layer from earlier.
    • Keep credentials ouside. API keys, Git tokens, and cloud secrets live in the outer sandbox or proxy layer rather than being passed in plaintext into worker environments. When a worker makes an outbound request through the proxy, credentials get injected according to policy. Many personal assistant bots now rely on this same pattern.
    • Enforce role-based permissions and tools/file isolation. This ties straight into container+worktree mounting. When the orchestrator spins up a container slot, it decides which directories to mount, io permissions, and tools exposure based on the worker's role. Enforcing boundaries at the OS and container layer is a hundred times more reliable than begging the model in a prompt not to edit code.😆
    • Intercept and unify the approval flow. The outer sandbox catches permission requests bubbling up from every underlying harness (such as the ACP permission callbacks mentioned earlier) and routes them into the orchestrator's own centralized approval UI.

    Closing Thoughts

    With that, Parts 1 and 2 put all core pieces of a multi-agent orchestration tool on the table. Part 1 focused on getting the orchestrator's internal mechanics working, while this post tackled the production engineering problems you cannot ignore once agents touch a real codebase. If you plan to build a lightweight multi-agent orchestration layer yourself, there is no need to over-engineer every module on day one. Getting ACP harness drivers, worktree isolation, and hybrid workflow gates running smoothly already covers the vast majority of daily engineering work, and you can layer in the outer sandbox and unified approval flow as team size or automation depth grows. Today you can happily hand implementation code off to AI, but

    ⚠️Architectural trade-offs still have to be thought by humans My post aims NOT to hand down one rigid blueprint. I'm guiding you through the real engineering trade-offs at each layer so you can pick the design that fits your own product goals. Erwin Engineer (ex-Alibaba, Shopee) & HCI PhD Building videoscript.uk Writing deep dives on agent workflows, system design & human-AI interaction. Join 3,803 followers

  • Emanuel Castillo @EmanuelAtAAP 15h This is the gap I see in AI engineer interviews. Plenty of people have built the demo. Very few can talk through retries, evals and what happens when an agent fails in production. That's the part that gets offers.
  • 16h 看着就稳,worktree 隔离真香……

Home - Wiki
Copyright © 2011-2026 iteam. Current version is 2.156.0. UTC+08:00, 2026-12-03 17:51
浙ICP备14020137号-1