Master-Slave Division of Labor and Cost Control in Multi-Agent Collaboration

    This article explores the master-slave architecture in multi-agent collaboration, detailing how to split tasks, select models, manage context, and control concurrency to optimize performance and reduce costs.

    1. Why Master-Slave Architecture is a Common Choice for Multi-Agent Collaboration

    Multi-agent collaboration refers to multiple language model instances with reasoning and tool-calling capabilities working together to complete a complex task. Compared to a single conversation, its core advantages lie in parallelization and responsibility isolation. A master agent is responsible for understanding goals, decomposing tasks, assigning execution, and aggregating results, while multiple worker agents handle clearly bounded subtasks.

    The value of this structure is mainly reflected in three points. First, parallel processing shortens the overall time, such as simultaneously retrieving materials, modifying multiple modules, or writing test cases. Second, responsibility isolation reduces the burden on a single context; each working unit only needs to master information relevant to itself. Third, the master node centralizes quality judgments, facilitating unified acceptance.

    The master-slave structure also brings new challenges. Improper task splitting can lead to redundant work or omissions, the master node can become an information bottleneck, and costs increase as the number of working units rises. The following sections elaborate on these issues.

    2. Task Splitting: From Macro Goals to Verifiable Subtasks

    The quality of splitting determines the upper limit of a multi-agent system. Good subtasks usually meet the following conditions:

    • Clear input boundaries: Only rely on a small number of known files, interfaces, or data.
    • Fixed output format: For example, a patch, a structured conclusion, or a test file.
    • Checkable completion criteria: Can be determined whether completed via tests, validation scripts, or manual checklists.
    • Few mutual dependencies: Avoid having two working units modifying the same file simultaneously.

    When splitting, you can adopt the approach of “define interfaces first, then dispatch implementation.” The master node first determines the data structures, function signatures, and directory conventions, and then lets each working unit implement them in parallel within the agreed scope. This reduces merge conflicts and facilitates later replacement of an execution unit.

    Here is a recommended structure for a subtask description:

    1. task_id: refactor-order-module-03
    2. goal: Migrate order amount calculation logic to an independent module
    3. inputs:
    4. - Source files related to amount calculation in the order module
    5. - Corresponding amount calculation test files
    6. constraints:
    7. - Do not modify external function signatures
    8. - Do not introduce new external dependencies
    9. acceptance:
    10. - All unit tests pass
    11. - Output a change description listing affected call points
    12. deliverable: Patch file and change description

    Structured descriptions prevent execution units from guessing boundaries, allowing the master node to check item by item whether deliverables are complete.

    3. Model Tiering: Selection for Master and Executor Roles

    Different roles have different capability requirements. The master node undertakes planning, judgment, and conflict resolution, demanding the highest reasoning and understanding capabilities. Executor nodes handle well-defined, repetitive work, placing more emphasis on response speed, unit cost, and output stability.

    Therefore, a common practice is tiered configuration: the master uses a more capable model, while execution units use cheaper or lighter models. When selecting, consider the following five dimensions:

    1. Reasoning complexity: Whether the task requires cross-module trade-offs or long-chain derivation.
    2. Output determinism: Whether the task has a standard answer or can be automatically verified.
    3. Context requirements: The amount of material that needs to be read in a single call.
    4. Call frequency: The number of times the role is called during the entire process.
    5. Cost of failure: Whether the repair cost after an error is high.

    Costs can be estimated with a simple model: total cost is approximately the sum of the number of calls for each role multiplied by the cost per call. When there are many execution units, even if the single-call cost difference is small, the cumulative gap will be significant. Pricing rules vary across services; actual estimates should be based on the official billing documentation of the corresponding platform.

    Routing logic can be written as explicit configuration:

    1. ROUTING_POLICY = {
    2. "planning": "high_capability_model",
    3. "review": "high_capability_model",
    4. "implementation": "efficient_model",
    5. "formatting": "lightweight_model",
    6. }
    7. def pick_model(task_type):
    8. # Fallback to high capability model for unknown types, prefer spending more over downgrading easily
    9. return ROUTING_POLICY.get(task_type, "high_capability_model")

    Pay special attention to the fallback strategy. Defaulting to a high-capability model for unknown tasks prevents critical decisions from being handed over to a weaker model due to misjudgment.

    4. Context Management: Growth Rate and Compression Strategies

    Long-running agents continuously accumulate conversation history, tool outputs, and intermediate conclusions. The context growth rate often exceeds expectations. Excessively long contexts bring three consequences: increased single-call cost, dilution of key information, and increased response latency.

    There are four common coping methods:

    First, artifact persistence. Write intermediate results to files or databases, keeping only paths and summaries in the conversation. Read them on-demand in subsequent steps rather than carrying them entirely.

    Second, phased summarization. After completing a milestone, the master or a dedicated summarization step generates a status record in a fixed format, including completed items, unresolved issues, and next actions. Earlier detailed processes can be discarded.

    Third, restricted transmission scope. Working units only receive context relevant to their tasks, rather than broadcasting the entire history to every executor.

    Fourth, periodic resets. Set checkpoints for long tasks; after reaching a certain number of rounds, start a new session with the status record as the bootstrapping material.

    1. def checkpoint(state):
    2. # Compress running state into a fixed structure for new session recovery
    3. lines = [
    4. "Completed: " + ", ".join(state["done"]),
    5. "Open issues: " + ", ".join(state["open_questions"]),
    6. "Next step: " + state["next_step"],
    7. "Artifact locations: " + ", ".join(state["artifacts"]),
    8. ]
    9. return "\n".join(lines)

    The benefit of a fixed structure is predictability. Each compression outputs the same fields, making it easy to verify whether key information is missing.

    5. Concurrency and Quota Control

    Concurrency is not always better when higher. As working units increase, the master needs to process more returned results, and the probability of merge conflicts rises. Meanwhile, most invocation channels have rate limits; exceeding them causes requests to be rejected or queued, potentially increasing overall time.

    It is recommended to follow these principles:

    • Start small, observe the completion quality and time of each stage, and gradually expand concurrency.
    • Set maximum steps and timeout limits for each execution unit to prevent infinite loops.
    • Use exponential backoff for retries when encountering rate limits, and set a maximum retry count.
    • Lock write operations or allocate exclusive directories for each unit to avoid parallel modifications to the same resource.

    ```python
    import asyncio
    import random

    async def call_with_backoff(fn