Skip to content

Subagents

Subagents let an agent delegate work to a child agent with its own isolated context window. The child runs in-process with its own token budget, executes with a scoped set of tools, and sends a distilled result back to the parent. The parent’s conversation stays clean and focused.

When the agent calls the subagent tool, PizzaPi:

  1. Discovers available agent definitions from multiple directories (see Agent discovery paths)
  2. Creates an in-process AgentSession via the pi SDK (no child process, zero overhead)
  3. Returns immediately so the parent can continue working
  4. Runs the child behind the scenes and injects its final output as a follow-up message when done

Unlike spawn_session, subagents run inside the parent process — no child process, no separate worker. They are still visible: each run is mirrored to the relay as an ephemeral child session, so it appears nested under its parent in the session sidebar with a live transcript, just like a linked session. Result delivery is automatic; the parent should not poll or wait.

While a subagent runs, PizzaPi registers a short-lived relay session named <agent>: <task> (<agent> #N: <task> for chain steps) with the parent as its parentSessionId. It streams the subagent’s transcript, and ends when the subagent finishes — after which the run remains in the parent’s conversation as the usual inline subagent card.

The mirror is best-effort and entirely optional: when the relay isn’t configured (local TUI, no API key, or PIZZAPI_RELAY_URL=off), subagents run exactly as before with no mirroring.

Create ~/.pizzapi/agents/researcher.md:

---
name: researcher
description: Read-only codebase research
tools: read,grep,find,ls
---
You are a research agent. Read files, trace dependencies,
and summarize findings without modifying anything.

The agent can invoke the subagent tool like this:

{
"agent": "researcher",
"task": "Summarize the authentication module and list all endpoints"
}

The subagent starts in its own context and the tool returns immediately. When the work finishes, the summary arrives automatically as a follow-up that resumes the parent session. Only the summary enters the parent’s context window.

Agent definitions are markdown files with YAML frontmatter. PizzaPi is compatible with Claude Code agent files.

Agents are discovered from multiple directories. Within each scope, .pizzapi/ paths are checked first (higher precedence), then .claude/ paths:

  • User scope: ~/.pizzapi/agents/*.md, ~/.claude/agents/*.md — available in all your sessions
  • Project scope: .pizzapi/agents/*.md, .claude/agents/*.md — repo-specific agents (walk-up search from cwd)

When agents with the same name exist in multiple directories, the first-found wins (.pizzapi before .claude, project overrides user).

FieldRequiredDescription
nameUnique identifier for the agent
descriptionBrief description (shown in agent listings)
toolsComma-separated list of allowed tools (e.g., read,grep,find)
disallowedToolsComma-separated tools to deny (removed from inherited list)
modelModel override (e.g., claude-haiku-4-5, haiku, anthropic/claude-haiku-4-5). inherit or omit to auto-select the cheapest available model (not the parent’s model).
maxTurnsMaximum agentic turns before the subagent stops
permissionModePermission mode: default, acceptEdits, dontAsk, bypassPermissions, plan
backgroundIf true, hints the agent should run as a background task

The markdown body after the frontmatter becomes the agent’s system prompt.

---
name: reviewer
description: Code review for bugs and style
tools: read,grep,find,ls
---
You are a code review agent. Identify bugs, security issues,
and style inconsistencies. Rate findings P0-P3 by severity.
Output "LGTM" if the code looks good.

A general-purpose task agent is always available without any definition file. PizzaPi also ships bundled example agents (researcher, reviewer, refactorer) in packages/cli/agents/ — copy them to ~/.pizzapi/agents/ to use them as custom agents.

AgentToolsPurpose
taskbuilt-in coding toolsGeneral-purpose delegation
researcherread, grep, find, lsRead-only codebase analysis and research
reviewerread, grep, find, lsCode review with severity-rated findings
refactorerread, write, edit, bash, grep, find, lsSafe incremental code transformations

Invoke one agent with one task:

{
"agent": "researcher",
"task": "What authentication strategy does this project use?"
}

Run multiple agents concurrently (up to 4 at once, max 8 tasks):

{
"tasks": [
{ "agent": "reviewer", "task": "Review src/auth/login.ts" },
{ "agent": "reviewer", "task": "Review src/auth/session.ts" },
{ "agent": "researcher", "task": "Find all SQL queries in the project" }
]
}

Run agents sequentially, passing each step’s output to the next via {previous}:

{
"chain": [
{ "agent": "researcher", "task": "Analyze the database schema and list all tables" },
{ "agent": "reviewer", "task": "Review the schema design described here:\n\n{previous}" }
]
}

If any step fails, the chain stops and reports which step failed.

Use the model field in agent definitions to route cheap tasks to fast models:

---
name: scout
description: Quick file exploration
tools: read,find,ls
model: haiku
---
Quickly scan files and report what you find. Be brief.

This saves money by using an inexpensive model for exploration while the parent session uses a more capable model for synthesis.

Control which agent directories are searched:

ScopeSearches
"user" (default)~/.pizzapi/agents/ and ~/.claude/agents/
"project".pizzapi/agents/ and .claude/agents/
"both"All directories (project overrides user on name conflict)

Project-scope agents (.pizzapi/agents/ and .claude/agents/) are loaded from the repository and could contain untrusted prompts. When agentScope includes project agents:

  • PizzaPi prompts for confirmation whenever a UI is available (TUI or Web UI).
  • In headless/runner contexts with no UI, the tool fails closed and refuses to run project agents.
  • Set confirmProjectAgents: false in the subagent tool parameters to skip the prompt (only for trusted repos).
  1. Create a .md file in ~/.pizzapi/agents/
  2. Add the required frontmatter (name, description)
  3. Optionally restrict tools and set a model
  4. Write a clear system prompt in the body
  • Be specific about the agent’s role and boundaries
  • Define output format so results are predictable
  • Restrict tools to the minimum needed (principle of least privilege)
  • Keep prompts short (< 500 words) — the agent’s context should be spent on the task, not the prompt
  • Use model routing for simple tasks — Haiku/Flash for exploration, Sonnet/Opus for analysis

The launch card records that the subagent started in the background. The parent session remains usable while it runs. On completion or failure, PizzaPi injects the result as a follow-up message and automatically resumes the parent agent. No polling is needed.

Featuresubagentspawn_session
CommunicationAutomatic follow-up resultTriggers + tell_child
ContextIsolated in-process sessionFully independent session
UIInline in parent sessionSeparate session view
OverheadNear-zero (in-process SDK)Higher (relay round-trip, new PTY)
Best forBackground delegation with automatic resultsInteractive child workflows, cross-runner tasks
Model selectionVia agent definition or parameterVia spawn parameter
Agent definitionsFile-based (~/.pizzapi/agents/)Prompt-based (inline in spawn call)

Use subagent when you only need the result. Use spawn_session when you need an independent session, cross-runner execution, or interactive trigger communication.

When using spawn_session, spawned sessions are automatically linked — child events surface as trigger messages in the parent’s conversation. Three trigger types are delivered:

Fires when the child session finishes. The trigger includes the child’s final output and an exitReason field:

  • exitReason: 'completed' — normal completion
  • exitReason: 'error' — the child hit a usage limit or provider failure

When a child session hits a usage limit or provider error, a session_error trigger fires immediately to the parent — before session_complete. This allows the parent to react early (e.g., retry with a different model, skip the task, or escalate to the user).

When a child calls plan_mode or AskUserQuestion, a trigger appears in the parent’s conversation. The parent responds with respond_to_trigger(triggerId, response).

Linked child sessions (spawned via spawn_session) can trigger push notifications by default. Child-session push suppression is opt-in per subscription (suppressChildNotifications), and the ntfy/native push path currently delivers all events regardless of isChildSession. If you are orchestrating many children and want fewer notifications, enable suppressChildNotifications on the relevant push subscriptions.

PizzaPi subagents are designed to be compatible with Claude Code agent files:

  • Agent definition files — The same .md files with YAML frontmatter work in both systems
  • Discovery paths — PizzaPi searches .claude/agents/ alongside .pizzapi/agents/
  • Frontmatter fieldsname, description, tools, disallowedTools, model, maxTurns, permissionMode, background
  • Tool name — The Web UI renders both subagent and Task tool calls with the subagent card
FeatureClaude Code (Task)PizzaPi (subagent)
ExecutionIn-processIn-process via pi SDK (isolated session)
Built-in agentsgeneral-purpose, Explore, Plantask (general-purpose)
Skillsskills frontmatter fieldNot yet supported
MCP serversmcpServers frontmatter fieldNot yet supported
Hookshooks frontmatter fieldNot yet supported
Memorymemory frontmatter fieldNot yet supported
ModesSingle onlySingle, parallel, chain

To use Claude Code agents with PizzaPi, simply ensure the agent .md files are in a directory PizzaPi scans. If you already have agents in .claude/agents/, they’ll be discovered automatically.

See also: Dynamic Workflows for large fan-out subagent orchestration that keeps intermediate output out of your context.