Subagents
Subagents let the Spectral coding agent delegate work to specialized sub-agents. Each sub-agent runs in an isolated context window with its own system prompt, filtered tool set, and optional model override — keeping the main conversation lean and focused.
How It Works
┌──────────────────────────────────────────┐
│ Main Agent Session │
│ │
│ User: "Review all auth files" │
│ │ │
│ ▼ │
│ subagent({ agent: "reviewer", │
│ task: "Review src/auth/" }) │
│ │ │
│ ▼ (isolated context) │
│ ┌──────────────────────────┐ │
│ │ Sub-agent: reviewer │ │
│ │ Tools: read, grep, ls │ │
│ │ Model: claude-haiku-4-5 │ │
│ │ → reads auth files │ │
│ │ → produces review │ │
│ └──────────┬───────────────┘ │
│ │ │
│ ▼ │
│ Main agent receives review result │
│ → continues with the output │
└──────────────────────────────────────────┘
Defining an Agent
Agents are .md files with YAML frontmatter followed by a system prompt body.
Project-level agents
your-project/
└── .spectral/
└── agents/
└── reviewer.md
User-level agents (global)
~/.spectral/agent/agents/reviewer.md
Example: Code Reviewer
---
name: reviewer
description: Reviews code changes for bugs, anti-patterns, and style issues
tools: read, grep, find, ls, bash
model: claude-haiku-4-5
---
You are a senior code reviewer. When given a file path or directory:
1. Read all relevant source files
2. Check for: logic errors, missing error handling, type safety issues,
security concerns, performance problems
3. Produce a concise review with actionable suggestions
4. Use conventional commit style for your findings
Frontmatter Fields
| Field | Required | Type | Purpose |
|---|---|---|---|
name | ✅ Yes | string | Unique identifier (snake_case). Used when calling subagent({ agent: "…" }). |
description | ✅ Yes | string | Sent to the LLM so it knows when to delegate to this agent. |
tools | ❌ No | string | Comma-separated tool names (e.g. read, grep, find, ls, bash). Without this, the agent only has recall and extension tools. |
model | ❌ No | string | Model override (e.g. claude-haiku-4-5, anthropic/claude-sonnet-4-5). Falls back to session model if omitted. |
Available Tools
Built-in tools: read, bash, edit, write, grep, find, ls.
Extension-registered tools (like recall, kanban_*, designer_*) are also resolved dynamically.
Execution Modes
The agent invokes subagents through the subagent tool with three modes:
Single Mode
Delegate one task to one agent:
{
"agent": "reviewer",
"task": "Review all route handlers in src/routes/ for missing error handling"
}
Parallel Mode
Run up to 8 tasks in parallel (max 4 concurrent):
{
"tasks": [
{ "agent": "reviewer", "task": "Review src/auth/" },
{ "agent": "reviewer", "task": "Review src/api/" },
{ "agent": "doc-writer", "task": "Generate docs for packages/parser/" }
]
}
Chain Mode
Sequential execution where each step sees the previous output via {previous}:
{
"chain": [
{ "agent": "investigator", "task": "Find all type errors in the project" },
{ "agent": "fixer", "task": "Fix the following errors: {previous}" }
]
}
If any step fails, the chain stops and returns the error.
Context Isolation
Each sub-agent invocation gets:
| Resource | Isolated? | Detail |
|---|---|---|
| Messages | ✅ Fresh | Starts with only the task message |
| System prompt | Combined | Main prompt + sub-agent instructions |
| Context window | ✅ Separate | No access to main conversation history |
| Tools | ✅ Filtered | Only tools listed in the agent definition + recall |
| Model | Overridable | Agent-configured model or session default |
| Abort signal | Propagated | Main session's abort signal passed through |
Agent Discovery
Agents are discovered from two locations based on agentScope:
agentScope | User-level (~/.spectral/agent/agents/) | Project-level (.spectral/agents/) |
|---|---|---|
"user" (default) | ✅ Always loaded | ❌ |
"project" | ❌ | ✅ Always loaded |
"both" | ✅ Loaded first | ✅ Loaded; user-level wins on name conflict |
Project-level agents are searched from the current working directory upwards until a .spectral/agents/ directory is found.
Security: Project Agent Confirmation
When agentScope is "project" or "both", the agent prompts for confirmation before running project-local agents (unless in headless/relay mode). This prevents untrusted repositories from silently running arbitrary agent definitions.
Model Resolution
Sub-agents resolve their model in this order:
- Session model (if available and has configured auth)
- Agent-configured model (from the
modelfrontmatter field) - Error — if neither is available
The model string supports two formats:
- Short:
claude-sonnet-4-5(fuzzy match by model ID) - Fully qualified:
anthropic/claude-sonnet-4-5(exact provider + model ID)
Example Agents
Documentation Writer
---
name: doc-writer
description: Generates API documentation from source code
tools: read, grep, find, ls
model: claude-sonnet-4-5
---
You are a technical writer. When given a source file or directory:
1. Read the code thoroughly
2. Extract the public API surface (exports, functions, classes, types)
3. Write clear, concise documentation with examples
4. Output valid Markdown suitable for API references
Test Generator
---
name: test-gen
description: Generates unit tests for uncovered code paths
tools: read, grep, find, ls, bash, write
model: claude-sonnet-4-5
---
You are a test engineer. When given a source file:
1. Read the file and its existing tests (if any)
2. Identify uncovered branches and edge cases
3. Generate thorough unit tests following project conventions
4. Write the test file using the `write` tool
Agents vs. Extensions
| Aspect | Agents | Extensions |
|---|---|---|
| Definition | Markdown + YAML | TypeScript (index.ts) |
| Purpose | Specialized LLM sub-processes | Tools, commands, event hooks |
| Context | Isolated per invocation | Shared with main agent |
| Tool access | Filtered subset | Full access + register new tools |
| Locations | .spectral/agents/, ~/.spectral/agent/agents/ | .spectral/extensions/, ~/.spectral/agent/extensions/ |