What it is

A subagent is an AI assistant that runs in its own session, invoked by a parent agent (or directly by you) to handle a focused task. Unlike a primary agent, a subagent does not sit in the main conversation loop. It spawns, does its work, and returns results. Think of it as a specialist you hire for a job — they work independently and report back.

What it is in real OpenCode terms

In OpenCode, subagents are specialized assistants that primary agents launch via the Task tool. They create child sessions separate from the main conversation. You can also invoke them directly by typing @ followed by the agent name.

OpenCode ships with three built-in subagents:

Subagent Purpose Tools Available
General Multi-step tasks requiring full tool access All tools (read, edit, bash, etc.)
Explore Read-only codebase exploration and analysis Read-only tools (search, grep, file reading)
Scout External documentation research and web browsing Read-only tools plus web access

When a primary agent delegates work to a subagent, it creates a child session. You can navigate between sessions using keyboard shortcuts:

  • Leader+DownJump into a subagent session
  • Right / LeftCycle between sibling subagent sessions
  • UpReturn to the parent session

Why it exists

Primary agents handle the main conversation, but not every task belongs in that context. A primary agent trying to explore an entire codebase, research external docs, and write code simultaneously loses focus and burns context window on noise. Subagents solve this by:

  • Isolating context — Each subagent gets its own session. Research results don't clutter the main conversation.
  • Running in parallel — Multiple subagents can work simultaneously on different parts of a problem.
  • Limiting scope — A read-only Explore subagent cannot accidentally modify files while investigating.
  • Enabling specialization — A Scout subagent is optimized for external research. An Explore subagent is optimized for codebase traversal. Each does one thing well.

Anatomy / main elements

Subagents share the same configuration fields as primary agents, with these key distinctions:

Field How it works for subagents
mode Must be set to subagent (or all) to be invokable as a subagent.
permissions The parent agent's subagent permission controls which agents it may launch. The subagent's own permissions control what it can do once running.
description Critical — this is what appears in the Task tool description so primary agents know when to use it.
prompt System prompt for the subagent's behavior. Kept focused since subagents do one thing.

The nesting depth of subagents is controlled by subagent_depth in your config (default: 1). This prevents infinite recursion where subagents spawn subagents indefinitely.

Where it lives

Custom subagent configurations live in the same locations as primary agents:

Project-level:
.opencode/agents/my-subagent.md

User-level (global):
~/.config/opencode/agents/my-subagent.md

Or inline in opencode.json:

{
  "agent": {
    "test-writer": {
      "description": "Writes unit tests for a given module",
      "mode": "subagent",
      "model": "anthropic/claude-sonnet-4",
      "prompt": "You write thorough unit tests.",
      "permissions": {
        "read": true,
        "edit": true,
        "bash": false
      }
    }
  }
}

The subagent_depth config controls how many levels deep subagents can nest:

{
  "subagent_depth": 1
}

How to create one

  1. Decide what focused task this subagent handles (e.g., writing tests, exploring a codebase, researching docs).
  2. Create a Markdown file in .opencode/agents/ with mode: subagent in the frontmatter.
  3. Write a focused system prompt. Subagents work best with narrow, clear instructions.
  4. Set permissions appropriately. A research-only subagent should have edit: false.
  5. The subagent becomes available for the primary agent to invoke via the Task tool, or you can call it with @test-writer.
Tip: Look at the built-in Explore subagent for inspiration. It is a minimal, read-only subagent optimized for one thing: navigating and understanding code without modifying it.

Minimal example

Create a subagent that only researches documentation and returns findings:

---
name: doc-researcher
description: Researches external documentation and returns structured findings
mode: subagent
permissions:
  read: true
  edit: false
  bash: false
---

You are a documentation researcher. When given a topic or library name:

1. Search for official documentation
2. Find the most relevant pages
3. Extract key API details, gotchas, and examples
4. Return findings as structured markdown with source URLs

Do not write code. Do not modify files. Only gather and summarize information.

Save as .opencode/agents/doc-researcher.md. A primary agent can now invoke it with the Task tool, or you can type @doc-researcher research the Prisma ORM migration API.

Related concepts

Common confusion

Subagent vs. Agent: Every subagent is an agent, but not every agent is a subagent. The difference is mode: a primary agent is what you interact with directly (switched via Tab). A subagent is what gets delegated to via the Task tool or @ mention. Same config format, different invocation pattern.
Subagent vs. Skill: A subagent is an autonomous entity with its own session, context, and tools. A skill is a block of instructions injected into the current agent's context. A subagent can load skills; a skill cannot spawn subagents.
Why can't I switch to a subagent with Tab? Subagents run in child sessions, not the main conversation. They are workers, not conversation partners. You interact with them by invoking them (via @ or the Task tool) and reviewing their output when they return.