Agents & Multi-Agent Orchestration
CodeCLI agents are AI-driven workers that execute tasks using tools, file operations, shell commands, and other agents. The multi-agent system supports primary agents, subagents, swarm orchestration with wave-based DAG scheduling, mainframe terminal control, and worktree isolation for safe parallel execution.
Agent Types
| Type | Description | Invoked By |
|---|---|---|
| Primary Agent | The main conversational agent that receives user prompts and orchestrates work | User input |
| Subagent | A child agent spawned by the primary agent to handle a specific subtask | Task tool |
| Swarm Agent | One of many agents in a coordinated swarm executing a DAG of tasks | Swarm scheduler |
| Mainframe Agent | An agent running inside a terminal pane in mainframe mode | Mainframe controller |
Primary Agents
The primary agent is the top-level agent you interact with in CodeCLI. It receives your messages, decides which tools to use, and can spawn subagents for parallel or independent work. The primary agent has full access to all tools and permissions configured in your session.
When you type a prompt into CodeCLI, the primary agent:
- Reads your message and any attached context
- Consults its system prompt, CLAUDE.md files, and loaded skills
- Plans a sequence of tool calls to accomplish the task
- Executes tool calls, checking permissions as needed
- May spawn subagents for independent subtasks
- Aggregates results and presents the final response
Subagents
Subagents are spawned by the primary agent (or other subagents) using the task tool. Each subagent runs independently with its own context window and can use the full set of available tools. Subagents are ideal for tasks that can be parallelized or isolated.
Agent Configuration
Agents are configured through frontmatter in markdown files or through thecodecli.json configuration file. The configuration controls the model, temperature, permissions, available tools, and execution behavior.
Configuration Schema
| Field | Type | Default | Description |
|---|---|---|---|
| model | string | claude-sonnet-4-20250514 | AI model to use for this agent |
| temperature | number | 0.0 | Sampling temperature (0.0 - 2.0) |
| maxTokens | number | 16384 | Maximum tokens in the response |
| maxSteps | number | 75 | Maximum tool-use turns before stopping |
| maxTurns | number | 200 | Maximum total conversation turns |
| permissions | object | {} | Per-tool permission overrides |
| tools | string[] | all | Explicit tool whitelist or blacklist |
| systemPrompt | string | default | Custom system prompt for this agent |
| allowedCommands | string[] | [] | Shell commands the agent is allowed to run |
| blockedCommands | string[] | [] | Shell commands the agent is blocked from running |
| timeout | number | 600 | Maximum execution time in seconds |
| retryOnFailure | boolean | false | Whether to retry on tool failures |
| maxRetries | number | 2 | Maximum number of retries per tool call |
| worktree | boolean | false | Run in an isolated git worktree |
| description | string | — | Human-readable description of the agent |
Example: Agent Configuration File
Example: Agent as Markdown Frontmatter
Agent Selection and Invocation
Agents are selected automatically based on the task or explicitly using slash commands and the task tool. The selection process follows these rules:
- Explicit invocation — Use
/agent NAMEto activate a specific named agent - Task tool — The primary agent spawns a subagent with
{"agent": "name"} - Automatic routing — CodeCLI selects the best agent based on the task description and agent descriptions
Invoking Agents with the Task Tool
Multi-Agent Orchestration
The task tool enables the primary agent to delegate work to subagents. Subagents can be spawned sequentially or in parallel depending on task dependencies. CodeCLI manages the lifecycle, context windows, and result aggregation automatically.
Sequential Execution
When tasks have dependencies (task B requires output from task A), the primary agent chains them sequentially:
Parallel Subagent Execution
When tasks are independent, the primary agent can spawn multiple subagents that run concurrently. Each subagent gets its own context window and executes independently. Results are collected and merged when all subagents complete.
Swarm Orchestration
For large-scale tasks, CodeCLI supports swarm mode with wave-based DAG (Directed Acyclic Graph) scheduling. A swarm is a collection of agents that execute tasks according to a dependency graph, where each "wave" contains tasks that can run in parallel.
How Wave-Based DAG Scheduling Works
DAG Visualization
Swarm Configuration
| Setting | Type | Default | Description |
|---|---|---|---|
| maxConcurrentAgents | number | 4 | Maximum number of agents running in parallel per wave |
| defaultAgentModel | string | claude-sonnet-4-20250514 | Default model for swarm agents |
| timeoutPerAgent | number | 300 | Timeout in seconds per individual agent |
| mergeStrategy | string | sequential-rebase | How to merge results: sequential-rebase, patch-merge, or fail |
| worktreeIsolation | boolean | true | Run each agent in an isolated git worktree |
| onConflict | string | fail-and-report | Action on merge conflicts: fail-and-report, prefer-newest, or prefer-oldest |
| retryFailedWaves | boolean | true | Retry failed tasks in a wave before failing the entire swarm |
| maxWaveRetries | number | 2 | Maximum number of retries for failed wave tasks |
Mainframe Terminal Control
Mainframe mode provides a split-pane terminal interface where multiple agents run simultaneously, each in its own terminal pane. This gives you real-time visibility into what each agent is doing and allows direct interaction with any agent.
Starting Mainframe Mode
Mainframe Commands
| Command | Description |
|---|---|
| :focus NUMBER | Switch focus to a specific agent pane |
| :broadcast MESSAGE | Send the same message to all agent panes |
| :pause NUMBER | Pause execution in a specific pane |
| :resume NUMBER | Resume a paused pane |
| :kill NUMBER | Terminate a specific agent pane |
| :spawn AGENT | Add a new agent pane |
| :layout LAYOUT | Change the pane layout (2x1, 2x2, 3x1, etc.) |
| :merge | Merge all agent results into a summary |
| :quit | Exit mainframe mode |
Worktree Isolation
When multiple agents need to edit files in the same repository, worktree isolation prevents conflicts by giving each agent its own git worktree. Each worktree is a separate checkout of the repository at the same commit, allowing agents to work independently.
How Worktree Isolation Works
.claude/worktrees/onConflict settingMerge Strategies
| Strategy | Description | Use When |
|---|---|---|
| sequential-rebase | Rebases each agent branch onto main one at a time | Agents edit overlapping files |
| patch-merge | Generates patches per agent and applies them in order | Agents edit different files |
| fail | Stops on first conflict and requires manual resolution | Safety-critical codebases |
Agent-Specific Tools and Permissions
Each agent can be configured with a specific set of tools and permission levels. This allows you to create agents with narrowly scoped capabilities for security and reliability.
Tool Availability by Agent Type
| Tool | Primary Agent | Subagent | Swarm Agent | Mainframe Agent |
|---|---|---|---|---|
| bash | Yes | Yes | Yes | Yes |
| read / write / edit | Yes | Configurable | Configurable | Configurable |
| glob / grep | Yes | Yes | Yes | Yes |
| task (spawn subagents) | Yes | Yes | No | Yes |
| webfetch / websearch | Yes | Configurable | Configurable | Configurable |
| computer_use | Yes | Configurable | No | No |
| skill | Yes | Yes | Yes | Yes |
| todowrite / todoread | Yes | Yes | Yes | Yes |
| kanbanwrite / kanbanread | Yes | No | No | No |
| mcp_* (MCP tools) | Yes | Configurable | Configurable | Configurable |