Skip to content

Building Agents on Claude

Part of: Build Workflows > Agents

Claude provides three approaches to building agents, each suited to different workflow patterns.

ApproachBest forComplexity
SubagentsFocused, single-responsibility tasksLow — one .md file per agent, no code required
Agent teamsMulti-agent coordination with parallel executionMedium — experimental feature, multiple agents collaborating
Agent SDKProduction applications and CI/CD automationHigher — Python or TypeScript code, full programmatic control

A subagent is a Markdown file that defines an agent’s role, instructions, and capabilities. Claude Code spawns it as a subprocess with its own context window, tools, and permissions.

Official docs: Claude Code Sub-agents

LocationScope
.claude/agents/ (in your project)Available to anyone working in this project
~/.claude/agents/ (in your home directory)Available across all your projects

Each agent is a single .md file with YAML frontmatter for configuration and Markdown body for instructions:

---
name: research-analyst
description: Use this agent to research companies and summarize findings
model: sonnet
tools:
- WebSearch
- WebFetch
- Read
- Write
---
# Research Analyst
You are a research analyst who investigates companies and produces structured briefs.
## Process
1. Search for the company using web search
2. Visit the company website and key pages
3. Summarize findings in a structured format
## Output Format
Write findings to `outputs/[company-name]-research.md` with sections for:
- Company overview
- Key products/services
- Recent news
- Relevant insights
FieldRequiredDescription
nameNoDisplay name (defaults to filename)
descriptionYesWhen to use this agent — Claude reads this to decide whether to activate it
modelNoWhich model to use (opus, sonnet, haiku)
toolsNoWhich tools the agent can access (e.g., Read, Write, WebSearch, Bash)
permissionModeNoHow tool permissions are handled (default, acceptEdits, bypassPermissions)
skillsNoSkills the agent can use (list of skill names)
mcpServersNoMCP servers the agent can connect to
maxTurnsNoMaximum number of turns before the agent stops
memoryNoPath to a persistent memory file the agent can read and update
hooksNoShell commands triggered by agent lifecycle events

Two options:

  1. Use the /agents command — Claude Code walks you through creating an agent interactively
  2. Create the file manually — Write a .md file in .claude/agents/ following the structure above

When you describe a task in Claude Code, it checks agent descriptions to decide whether to delegate. You can also invoke an agent directly:

Terminal window
# Run from the command line
claude --agent research-analyst
# Or describe the task and let Claude choose the right agent
claude "Research Acme Corp and write a brief"

Agents can run in the foreground (you see their work in real-time) or in the background (they work independently while you continue).

  • Tool restrictions — limit which tools an agent can use, preventing unintended actions
  • Skills — attach reusable skills that give the agent domain expertise
  • MCP servers — connect to external services (databases, APIs, browsers) via the Model Context Protocol
  • Persistent memory — give the agent a memory file it reads at startup and updates as it learns
  • Hooks — trigger shell commands when the agent starts, stops, or encounters specific events

Your Design phase produced a platform-agnostic agent blueprint. Here’s how each component maps to a Claude Code agent file:

Design blueprintClaude Code agent file
NameFilename (e.g., research-analyst.md) and optional name frontmatter
Descriptiondescription frontmatter — write it so Claude knows when to activate this agent
InstructionsMarkdown body — the agent’s role, process, constraints, and output format
Modelmodel frontmatter (opus, sonnet, haiku)
Toolstools frontmatter (list of tool names) + mcpServers for external connections
Contextskills frontmatter + reference files mentioned in the instructions
GoalCaptured in the description (activation trigger) and instructions (success criteria)

Agent teams let multiple Claude Code instances work together on complex tasks — a team lead coordinates while teammates handle specialized subtasks in parallel.

Official docs: Claude Code Agent Teams

A team lead (a Claude Code agent) can spawn teammates — independent Claude Code instances that each get their own context window and tools. The team lead coordinates work through a shared task list and inter-agent messaging.

Team Lead
├── Teammate A (e.g., research analyst)
├── Teammate B (e.g., writer)
└── Teammate C (e.g., editor)
  • Shared task list — the team lead creates tasks, teammates claim and complete them
  • Inter-agent messaging — teammates can send messages to the team lead and to each other
  • Parallel execution — teammates work simultaneously on independent tasks
  • Plan approval — the team lead can present a plan for your approval before dispatching work
Use single agents when…Use agent teams when…
The task has one focus areaThe task spans multiple expertise domains
Steps are sequentialSteps can run in parallel
One agent can handle everythingDifferent steps need different tools or knowledge
You want simple, predictable executionYou want to maximize throughput on complex tasks
  • Parallel research — multiple teammates research different aspects simultaneously
  • Competing approaches — teammates try different solutions, team lead picks the best
  • Cross-layer coordination — frontend, backend, and testing teammates work on related changes
  • Pipeline processing — research → write → edit with specialized teammates for each stage

For multi-agent workflows, the Design phase produced configurations for multiple agents. On Claude Code:

  1. Each specialist agent becomes a teammate definition
  2. The orchestrator becomes the team lead’s instructions
  3. Handoff points map to task list entries and messaging patterns
  4. Human review gates map to plan approval checkpoints

The Claude Agent SDK lets you build agents in Python or TypeScript with full programmatic control. It gives you the same tools, agent loop, and context management that power Claude Code — but as a library you can embed in your own applications.

Official docs: Agent SDK Overview

Use Markdown agents when…Use the Agent SDK when…
You’re working interactively in Claude CodeYou’re building a production application
You want no-code configurationYou need programmatic control over agent behavior
One-off or ad-hoc tasksCI/CD pipelines and automation
You don’t need custom logic between agent stepsYou need hooks, callbacks, or custom orchestration
Terminal window
npm install @anthropic-ai/claude-agent-sdk

Set your API key as an environment variable:

Terminal window
export ANTHROPIC_API_KEY=your-api-key

The SDK also supports Amazon Bedrock, Google Vertex AI, and Azure AI Foundry as alternative providers.

import { query } from "@anthropic-ai/claude-agent-sdk";
for await (const message of query({
prompt: "Find all TODO comments and create a summary",
options: { allowedTools: ["Read", "Glob", "Grep"] }
})) {
if ("result" in message) console.log(message.result);
}

The SDK handles the full agent loop — Claude autonomously reads files, runs commands, and produces results without you implementing tool execution.

The SDK exposes the same capabilities as Claude Code’s Markdown agents, plus programmatic features:

CapabilityDescription
Built-in toolsRead, Write, Edit, Bash, Glob, Grep, WebSearch, WebFetch — no tool implementation needed
SubagentsSpawn specialized agents with their own tools and instructions
MCP serversConnect to external systems (databases, browsers, APIs) via the Model Context Protocol
HooksRun callback functions at key points in the agent lifecycle (PreToolUse, PostToolUse, Stop, etc.)
SessionsMaintain context across multiple exchanges — resume or fork conversations
PermissionsControl exactly which tools the agent can use, from read-only to full access
Skills and memoryLoad project skills and CLAUDE.md instructions via settingSources

Define custom subagents programmatically — useful when you need multiple specialized agents coordinating within your application:

import { query } from "@anthropic-ai/claude-agent-sdk";
for await (const message of query({
prompt: "Use the code-reviewer agent to review this codebase",
options: {
allowedTools: ["Read", "Glob", "Grep", "Task"],
agents: {
"code-reviewer": {
description: "Expert code reviewer for quality and security reviews.",
prompt: "Analyze code quality and suggest improvements.",
tools: ["Read", "Glob", "Grep"]
}
}
}
})) {
if ("result" in message) console.log(message.result);
}

asyncio.run(main())

### Mapping your Design blueprint
Your [Design phase](../../../business-first-ai-framework/design.md) produced a platform-agnostic agent blueprint. Here's how each component maps to the Agent SDK:
| Design blueprint | Agent SDK |
|-----------------|-----------|
| **Name** | Key in the `agents` dictionary |
| **Description** | `description` field in the agent definition |
| **Instructions** | `prompt` field — the agent's role, process, constraints, and output format |
| **Model** | `model` option (`opus`, `sonnet`, `haiku`) |
| **Tools** | `allowedTools` list + `mcpServers` for external connections |
| **Context** | `settingSources` to load skills and CLAUDE.md, or inline in the prompt |
| **Goal** | Captured in the `description` (activation trigger) and `prompt` (success criteria) |
## What's Next
- [Agents overview](../../../business-first-ai-framework/build/index.md) — the platform-agnostic agent decision framework
- [Design Your AI Workflow](../../../business-first-ai-framework/design.md) — produce the agent blueprint that feeds into these implementations
- [Scheduling Subagents](../subagents/scheduling-subagents.md) — run agents automatically on a schedule
- [Agent SDK Quickstart](https://platform.claude.com/docs/en/agent-sdk/quickstart) — build a bug-fixing agent in minutes
- [Agent SDK Example Agents](https://github.com/anthropics/claude-agent-sdk-demos) — email assistant, research agent, and more