Instead of one generalist agent doing everything, delegate to specialized agents. This keeps context windows focused—preventing the clutter that degrades performance.
Task tool, part of the spring-ai-agent-utils toolkit, is a portable, model-agnostic Spring AI implementation inspired by Claude Code’s subagents. It enables hierarchical agent architectures where specialized subagents handle focused tasks in dedicated context windows, returning only essential results to the parent. Beyond Claude’s markdown-based format, the architecture is extensible—supporting A2A and other agentic protocols for heterogeneous agent orchestration (more information will be provided in Part 5).
This is Part 4 of our Spring AI Agentic Patterns series. We’ve covered Agent Skills, AskUserQuestionTool, and TodoWriteTool. Now we explore hierarchical subagents.
Ready to dive in? Skip to Getting Started.
How It Works
The main agent delegates tasks to specialized subagents through the Task tool, with each subagent operating in its own isolated context window. The subagent architecture consists of three key components: 1. Main Agent (Orchestrator) The primary agent that interacts with users. Its LLM has access to theTask tool and knows about available subagents through the Agent Registry—a catalog of subagent names and descriptions populated at startup. The main agent automatically decides when to delegate based on each subagent’s description field.
2. Agent Configuration Files
Subagents are defined as Markdown files (e.g., agent-x.md, agent-y.md) in an agents/ folder. Each file specifies the subagent’s name, description, allowed tools, preferred model, and system prompt. These configurations populate both the Agent Registry and Task tool at startup.
3. Subagents
Separate agent instances that execute in isolated context windows. Each subagent can use a different LLM (LLM-X, LLM-Y, LLM-Z) with its own system prompt, tools, and skills—enabling multi-model routing based on task complexity.
The diagram below illustrates the execution flow:
- Loading: At startup, the Task tool loads the configured subagent references, resolves their names and descriptions, and populates the agent registry.
- User sends a complex question to the main agent
- Main agent’s LLM evaluates the request and checks available subagents in the registry
- LLM decides to delegate by invoking the
Tasktool with the subagent name and task description - Task tool spawns the appropriate subagent based on the agent configuration
- Subagent works autonomously in its dedicated context window
- Results flow back to the main agent (only essential findings, not intermediate steps)
- Main agent synthesizes and returns the final answer to the user
- Dedicated context window - Isolated from the main conversation, preventing clutter
- Custom system prompt - Tailored expertise for specific domains
- Configurable tool access - Restricted to only necessary capabilities
- Multi-model routing - Route simple tasks to cheaper models, complex analysis to capable ones
- Parallel execution - Launch multiple subagents concurrently
- Background tasks - Long-running operations execute asynchronously
Built-in Subagents
Spring AI Agent Utils provides four built-in subagents, automatically registered whenTaskTool is configured:
| Subagent | Purpose | Tools |
|---|---|---|
| Explore | Fast, read-only codebase exploration—find files, search code, analyze contents | Read, Grep, Glob |
| General-Purpose | Multi-step research and execution with full read/write access | All tools |
| Plan | Software architect for designing implementation strategies and identifying trade-offs | Read-only + search |
| Bash | Command execution specialist for git operations, builds, and terminal tasks | Bash only |
style-checker, security-scanner, and test-coverage simultaneously during code review.
Getting Started
1. Add the Dependency
2. Configure Your Agent
description fields.
3. Multi-Model Routing (Optional)
Route subagents to different models based on task complexity:Creating Custom Subagents
Custom subagents are Markdown files with YAML frontmatter, typically stored in.claude/agents/:
Subagent File Format
Configuration Fields
| Field | Required | Description |
|---|---|---|
name | Yes | Unique identifier (lowercase with hyphens) |
description | Yes | Natural language description of when to use this subagent |
tools | No | Allowed tool names (inherits all if omitted) |
disallowedTools | No | Tools to explicitly deny |
model | No | Model preference: haiku, sonnet, opus |
skills and permissionMode.
Important: Subagents cannot spawn their own subagents. Don’t includeTaskin a subagent’stoolslist.
Loading Custom Subagents
Background Execution
Long-running subagents can execute asynchronously. The main agent continues working while background subagents execute. UseTaskOutputTool to retrieve results when needed. For persistent task storage across instances, see the TaskRepository documentation.
Conclusion
The Task tool brings hierarchical subagent architectures to Spring AI, enabling context isolation, specialized instructions, and efficient multi-model routing. By delegating complex tasks to focused subagents, your main agent stays lean and responsive. Next up: In Part 5, we explore the Subagent Extension Framework—a protocol-agnostic abstraction that lets you integrate remote agents via A2A, MCP, or custom protocols, extending Spring AI’s portability beyond LLM providers to agent communication protocols.Resources
- GitHub Repository: spring-ai-agent-utils
- TaskTools Documentation: TaskTools.md
- Example Project: subagent-demo
Related
- Claude Code Subagents - Original inspiration
Series Links
- Part 1: Agent Skills - Modular, reusable capabilities
- Part 2: AskUserQuestionTool - Interactive workflows
- Part 3: TodoWriteTool - Structured planning
- Part 4: Task tool & Subagents (this post)
- Part 5: Subagent Extension Framework (coming soon) - Protocol-agnostic agent orchestration