Debugging and Governing Model Misbehavior in Multi-Agent Collaboration
In multi-agent coding systems, a main agent can spawn sub-agents for code analysis, test writing, documentation, or debugging. While this improves parallelism on complex tasks, it also introduces new control problems: sub-agents using the wrong model, the main agent being unable to select predefined agent roles, unbounded context propagation, and even recursive sub-agent spawning. This article breaks these issues into two core categories—invisible scheduling metadata and uncontrolled invocation behavior—and provides a practical methodology for diagnosis and governance, covering tool parameter schemas, protocol versioning, permission inheritance, depth limits, and context passing strategies.
In a multi-agent coding system, the main agent can create multiple sub-agents to handle code analysis, test writing, documentation, or issue triage. Compared with a single-agent setup, multi-agent collaboration improves parallel processing of complex tasks, but it also introduces new control problems: sub-agents using the wrong model, the main agent being unable to select predefined agent roles, context being passed without boundaries, and even recursive behavior where sub-agents keep spawning more sub-agents.
These problems are usually not caused by a single prompt. They are jointly determined by “capability versions, configuration switches, tool schemas, and context strategies.” Below is a general framework for analyzing and governing them.
1. Distinguish Two Core Problems First
When a multi-agent system misbehaves, start by splitting the problem into two categories:
Category 1: Scheduling information is invisible. The main agent has the ability to create sub-agents, but the tool parameters do not expose fields such as model, agent type, reasoning effort, or service tier. As a result, the main agent can only use system defaults—it cannot select a specific agent or assign different models to different tasks.
Category 2: Invocation behavior is uncontrolled. Even when the main agent can create sub-agents, those sub-agents may keep creating new ones, causing the call depth to grow. The system then exhibits duplicated tasks, context bloat, rising resource consumption, and results that are hard to trace.
These two failure modes require different remedies: the former calls for inspecting tool parameters and metadata switches; the latter calls for inspecting call depth, agent permissions, and prompt constraints.
2. Why Sub-Agents Cannot Specify a Model
In some multi-agent implementations, the system has different capability generations. Newer versions often hide part of the agent metadata to simplify tool parameters or reduce scheduling complexity. When the “hide agent metadata” switch is enabled, the structured parameters for creating a sub-agent may no longer include:
- Agent type;
- Model in use;
- Reasoning effort;
- Service tier;
- Other scheduling-related attributes.
This does not mean the system entirely lacks multi-model support—it means the main agent cannot explicitly specify these parameters through tool calls. The resulting sub-agents inherit a default or system-level model, so users observe that “no matter what task is assigned, every sub-agent uses the same model.”
When troubleshooting, focus on the following:
- Which generation of the multi-agent protocol is currently enabled;
- Whether the sub-agent creation tool’s parameter schema includes a model field;
- Whether the “hide agent metadata” configuration is enabled;
- Whether predefined agent configurations are loaded;
- Whether the default model overrides task-level model selection.
If the system supports configuration files, you can apply a general configuration pattern like this:
[features.multi_agent]hide_spawn_agent_metadata = falsetool_namespace = "agents"
The intent here is to preserve the necessary scheduling metadata when spawning sub-agents and to place the related tools in a dedicated agent tool namespace. Actual field names vary across implementations—always consult the configuration documentation for your current version.
Note that exposing metadata does not guarantee the main agent will pick the right model. The main agent also needs to know which models suit which tasks. Therefore, add explicit constraints in a project-level rules file, for example:
- Use fast models first for code locating tasks;
- Use high-reasoning models for complex architecture analysis;
- Do not create extra sub-agents for documentation work;
- Test tasks may only invoke specified agent types;
- When nothing is specified, use the default model instead of guessing.
This reduces situations where the main agent assigns resources arbitrarily based on vague semantics.
3. Controlling Complexity with an Older Protocol
If your project does not require continuous communication with sub-agents, dynamic interruption, or re-dispatch, there is no need to force a more complex newer protocol.
A lighter protocol typically only handles:
- Creating a sub-agent;
- Passing in a task description;
- Waiting for the result;
- Collecting the task output.
A more complex protocol may additionally support:
- Sending new messages to an existing sub-agent;
- Appending tasks to a running sub-agent;
- Interrupting a running agent;
- Querying the current agent list;
- Managing the lifecycle of multiple parallel tasks.
These capabilities suit long-running collaborative workflows, but they also add scheduling state and behavior paths. If the system does not provide adequate global documentation for these tools, the main agent may not know when to use them—and may even make incorrect calls due to incomplete tool descriptions.
Therefore, if your workload only needs “create once, execute once, return once,” prefer the simple protocol. Some systems can automatically generate or load a legacy model catalog configuration when the newer multi-agent feature is disabled, for example:
model_catalog_json = "~/.config/models-v1.json"[features.multi_agent_v2]enabled = false
The key idea is not to lock into a specific version, but to match protocol capability to task complexity. The more capabilities you expose, the more you need a mature permission model, state management, and auditing. For experimental projects or personal development environments, a lightweight protocol is often more stable and easier to debug.
4. The Root Causes of Recursive Sub-Agent Creation
When sub-agents repeatedly spawn sub-agents, it looks like a prompt constraint failure on the surface, but there is usually a deeper mechanism problem underneath.
1. The Maximum Depth Setting Is Not Actually Enforced
Many systems provide a maximum call depth to limit agent nesting. For example, the main agent is layer zero, sub-agents it creates are layer one, and agents created by those sub-agents are layer two.
Ideally, once the maximum depth is reached, the agent creation tool should be rejected or automatically disabled. However, in some multi-agent protocols, the depth parameter may only apply to the main agent and is not correctly propagated to downstream sub-agents—so every layer believes it can still dispatch tasks.
Therefore, do not merely check whether max_depth exists in the config file. Verify runtime behavior:
- Can a first-layer sub-agent create a second-layer agent?
- Does the tool return a clear error once the limit is reached?
- Is depth information passed to sub-agents?
- Is depth counting consistent across parallel creations?
- Do failed tasks trigger repeated retries?
If the setting exists but calls can still nest indefinitely, treat it as a version compatibility issue or implementation defect—do not keep relying on that parameter.
2. Sub-Agents Inherit Full Agent Permissions
Some systems let sub-agents inherit all of the main agent’s tool permissions by default, including the permission to create new agents. As a result, any sub-agent can keep splitting tasks, eventually forming a tree—or even exponential—explosion.
A safer approach is the principle of least privilege:
- The main agent has permission to create sub-agents;
- First-layer sub-agents only get code reading, analysis, or testing tools;
- Sub-agents are prohibited from creating agents unless strictly necessary;
- Grant permissions separately for scenarios that genuinely require re-dispatch;
- Set a global cap on the total number of agent creations.
Permission control is more reliable than relying on prompts alone. Prompts are soft constraints that can be influenced by context, task goals, or model behavior; tool permissions are hard constraints—even if the model actively tries, it cannot invoke unauthorized capabilities.
5. How Project Rules Files Should Constrain Behavior
Beyond system configuration, multi-agent behavior should be explicitly defined in a project-level rules file. Rules should not simply say “do not create sub-agents infinitely”—they should describe executable decision conditions.
For example:
1. Do not create sub-agents by default; create them only when a task can be clearly split and executed in parallel.2. Sub-agents must not create further sub-agents.3. Each sub-agent handles exactly one clear, verifiable task.4. Do not pass the full conversation history by default; pass only the task goal, necessary files, and constraints.5. When context turn count is unspecified, use the most recent three turns of relevant information.6. After sub-agents return results, the main agent consolidates them; do not re-dispatch identical tasks.7. If task boundaries are unclear, confirm with the user first—do not solve it by adding more agents.
“Nesting prohibition” and “context limits” should coexist. Prohibiting nesting without limiting context still allows a single sub-agent to carry large amounts of history; limiting context without prohibiting nesting cannot stop the agent tree from growing.
6. Context Passing Strategies
How context is passed directly affects both result quality and resource consumption. There are generally three modes.
No history context
The sub-agent receives only a structured task description, target files, and output requirements. This is the most resource-efficient mode and suits independent tasks such as code scanning, format checks, and single-file analysis.
The drawback is that the sub-agent lacks background, so the main agent must write sufficiently complete task descriptions.
Recent-turns context
For example, pass only the last three turns of conversation. This balances information volume and task continuity, suiting tasks that need recent discussion but not full history.
Note that “turns” should not simply equal message count. A single tool call can produce multiple intermediate messages, so the system should clearly define how turns are counted.
Full context
Full context suits tasks that strongly depend on historical decisions, such as continuous debugging or long-running design work. But it carries three risks:
- Input size keeps growing;
- Irrelevant information interferes with model judgment;
- Sensitive content gets copied to more agents.
Therefore, full context should not be the default strategy. Prefer summaries, task checklists, relevant file snippets, and explicit acceptance criteria instead of raw conversation.
7. A Recommended Troubleshooting Order
When you encounter “models cannot be specified” or “sub-agents nesting wildly,” work through the following order:
Step 1: Record the actual call chain. Do not look only at final results—log the creation relationships among the main agent, first-layer sub-agents, and subsequent agents.
Step 2: Inspect the tool schema. Confirm whether the structured parameters for creating sub-agents include model, agent type, and depth-related fields.
Step 3: Confirm the configuration version. Compare the capabilities of old and new protocols and decide whether the current task truly needs advanced lifecycle management.
Step 4: Disable unnecessary metadata hiding. Only expose the corresponding scheduling fields when you genuinely want the main agent to choose models autonomously.
Step 5: Tighten sub-agent permissions. Prefer prohibiting sub-agents from creating agents outright, rather than relying on natural language prompts.
Step 6: Limit context passing. Default to no-history or limited-history modes to prevent irrelevant information from spreading.
Step 7: Set up observability. Log agent hierarchy, task identifiers, model selection, input/output summaries, latency, and failure reasons.
8. Design Principles for Stable Operation
A reliable multi-agent system should not pursue maximum agent count. It should emphasize clear task boundaries, controllable permissions, and traceable invocations.
In practice, follow these principles:
- Tasks the main agent can complete should not be split into sub-agents;
- Tasks one sub-agent can complete should not be split into multiple agents;
- Sub-agents execute only; they do not change the overall task goal;
- Model selection should be driven by explicit rules, not left to the agent’s discretion;
- Complex protocols should be used only where continuous collaboration is genuinely required;
- All nesting, retries, and concurrency should have upper limits;
- After any configuration change, verify actual behavior with a minimal test task.
In summary, sub-agents failing to use the specified model usually stems from hidden scheduling metadata or insufficient protocol capability, while unbounded sub-agent nesting is typically tied to unenforced depth limits, overly broad permission inheritance, and incomplete rule constraints. By choosing an appropriate protocol version, exposing the necessary scheduling fields, restricting agent permissions, controlling context scope, and adding runtime auditing, you can transform a multi-agent system from “automatic expansion” into “controlled collaboration”—improving stability, predictability, and maintainability without sacrificing processing power.
