Multi-Agent Collaboration in Practice: Building an Efficient Agent Team
This article explains how to build an efficient multi-agent team for software engineering tasks. It covers why single agents hit bottlenecks such as edit drift, context pollution, and mixed responsibilities; a reusable three-layer architecture with planning, execution, and worker roles; hash-anchored editing that makes edit locations verifiable; context governance that limits what each role sees; automated quality gates and observability; and common pitfalls with tuning advice. Readers will learn how structure, rather than simply calling a model more times, produces reproducible delivery quality.
1. Why Single Agents Hit a Bottleneck: From “Generating Code” to “Delivering Results”
In real engineering scenarios, getting a model to produce runnable code is not difficult. The hard part is keeping it correct across multiple rounds of edits, cross-file references, and a growing context window. Three failure modes are common. First, edit drift: on the second revision, the model rewrites code blocks unrelated to the task. Second, context pollution: an incorrect conclusion from an early trial is treated as established fact in later rounds. Third, mixed responsibilities: a single agent plans, implements, and reviews at the same time, which creates self-confirmation bias.
The value of a multi-agent team is not “calling the model a few more times.” It is isolating those three risks through organizational structure: separating planning from execution, execution from review, and review from the final write to disk. Once role boundaries are clear, each layer only needs to maintain its own portion of the context, and overall controllability improves significantly.
2. Layered Architecture: Three Layers of Responsibility and a Collaboration Protocol
A reusable layered structure can be abstracted into three layers.
The planning layer turns vague requirements into a verifiable task graph. It typically includes three roles: a strategic planner who breaks down goals and prioritizes them; an advisor who adds constraints and points out implicit assumptions; and a reviewer who checks whether the plan itself is executable, preventing a plan that “looks great but cannot be implemented.”
The execution layer has a commander responsible for coordination. Its key constraint is that it does not directly produce business code. It only distributes tasks, orders dependencies, aggregates results, and resolves conflicts. This constraint seems counterintuitive, but it prevents the coordinator from being dragged into local implementation details.
The worker layer is where changes are actually made. Workers are split by domain, for example frontend interaction, backend APIs, data and storage, and testing and build. Workers do not communicate directly with each other; all exchanges go through the commander. This keeps the information flow tree-shaped rather than mesh-shaped and avoids context explosion.
The collaboration protocol should make three things explicit: every task must have a unique identifier and acceptance criteria; every output must be traceable to a specific file and location; and any cross-layer rework must go back to the planning layer for reconfirmation rather than being patched privately in the execution layer.
3. Hash-Anchored Editing: Turning “Where to Edit” into a Verifiable Fact
The most expensive error in multi-round editing is editing the wrong location. The idea behind hash-anchored editing is to give each piece of content to be modified a dual identifier consisting of a line range and a content hash. When the model submits an edit, it must provide both the identifier and the expected new content. Before writing to disk, the system recomputes the hash and compares it.
Target location: FILE#LINE[a-b]#HASH(sha256:9f3c...)Submit edit: { anchor, expected_old_hash, new_content }Validation rule: write only if actual hash == expected_old_hashFailure handling: return the current real snippet, require re-location, no blind edits
This mechanism brings three direct benefits. Location failures are exposed immediately instead of being delayed until the testing stage. The probability of unrelated code being rewritten drops sharply. Concurrent edit conflicts at the same location can be detected explicitly. In public industry discussions, after introducing this kind of content validation, the success rate of models on real repository-level tasks showed a shift from single-digit percentages to nearly 70%. The engineering root cause is precisely “fewer invalid and incorrect edits,” not a sudden change in the model’s inherent capability.
4. Context Governance: Let Each Layer See Only What It Should
More context is not always better. Practical approaches include: giving each role an independent context window and read-only reference area; compressing long history into a structured summary (goals, confirmed facts, open questions, prohibited actions); injecting retrieved material by reference number instead of stacking raw text; and maintaining a “negative list” for failed attempts that explicitly states which approaches have been rejected and why.
An illustrative role configuration can be expressed like this:
role: worker.backendcontext:read_only: [api_spec, data_model]summary_budget: 2k_tokensnegative_list: trueoutput:must_include: [anchor, hash, test_command]
5. Quality Loop and Observability
Once the work is organized into a team, validation must be automated. Three gates are recommended. The static gate checks formatting and types. The behavioral gate runs unit tests and integration tests on critical paths. The consistency gate confirms that the scope of changes matches the task description; anything beyond that scope is treated as a violation. Inputs and outputs of all gates are written to a logging service, and monitoring and alerting track trends in metrics such as success rate, average rework rounds, and location failure rate.
In terms of deployment, this kind of system usually runs as a stateless coordinator process plus several worker processes in containers. State and artifacts are stored in object storage, tasks and messages are decoupled through a message queue, and keys are managed by a unified key management capability. If you use general capabilities such as Baidu AI Cloud’s container platform, object storage, logging service, and monitoring and alerting, you can focus engineering effort on role design and validation rules rather than infrastructure operations.
6. Common Pitfalls and Tuning Advice
First, too many roles make communication overhead exceed the benefits; four to six roles are usually enough. Second, the commander crosses the boundary and writes code, breaking responsibility isolation; this should be prohibited at both the prompt and process levels. Third, vague acceptance criteria; task descriptions must include executable validation commands. Fourth, no strategy for handling location failures, which leads the model to repeatedly make blind edits. Fifth, ignoring cost observability; consumption should be tracked per role to identify redundant reviews.
In summary, the benefits of a multi-agent team come from structure, not stacking: layered isolation of risks, hash anchoring for trustworthy edits, context governance to control complexity, and automated gates plus observability to close the loop. Applying this method to your own repositories and workflows is what delivers stable, reproducible delivery quality.
