Course navigation
Claude CodeLesson 14 of 25

Create a Sub-Agent

The previous lesson explained what sub-agents are and why they matter. Now you'll build one. A sub-agent is defined by a single Markdown file — it describes the agent's role, the tools it can use, and any rules it should follow. Claude Code reads that file and can invoke the agent by name.

What you'll build

A Migration Checker sub-agent. It reads your Prisma schema and recent migration files, then reports any field renames, deletions, or type changes that could break the running app. It never edits files — it only reads and reports.

PropertyValue
RoleRead-only auditor
ToolsRead files only
OutputRisk summary

Where sub-agent files live

Sub-agent definitions are Markdown files stored in a specific folder. Claude Code scans this folder on startup and makes every agent it finds available by name.

Project structure
my-project/
├── .claude/
│ └── agents/
│ ├── migration-checker.md ← your agent
│ ├── security-auditor.md
│ └── doc-writer.md
├── prisma/
├── src/
└── CLAUDE.md
The .claude/agents/ folder is the standard location. Files placed here are automatically discovered — no registration or config file needed.

The four parts of an agent file

Every sub-agent file is a Markdown file with a YAML frontmatter block at the top. The frontmatter declares the agent's identity and capabilities. The Markdown body contains its instructions.

FieldPurposeExample
nameIdentifier used to invoke the agentmigration-checker
descriptionWhen Claude should use this agent automaticallyAudits Prisma migrations for breaking changes
toolsAllowed capabilities — enforced by Claude CodeRead only (Write and Bash excluded)
bodyRole, task, output format, and constraints## Your role You are a read-only migration auditor...

The complete agent file

Here is the finished migration-checker.md agent. Create this file at .claude/agents/migration-checker.md in your project.

.claude/agents/migration-checker.mdYAML + Markdown
---
name: migration-checker
description: >
Audits Prisma schema and migration files for breaking changes
such as field renames, deletions, or type changes that could
break the running application. Use before running migrations.
tools:
- Read
---
## Your role
You are a read-only Prisma migration auditor. You inspect
prisma/schema.prisma and the files in prisma/migrations/ to
identify changes that pose a risk to the running app.
You must not edit any files. Your only job is to report risks.
## What to check
- Fields removed or renamed in the schema
- Column type changes (e.g. String → Int)
- Required fields added to existing tables (no default)
- Unique constraints added to columns that may have duplicates
## Output format
Return a short summary with two sections:
**Breaking changes** — list each one with the affected model,
field, and a one-line explanation of the risk.
**Safe to run** — confirm if nothing breaking was found.
The tools: [Read] restriction means this agent physically cannot write files or run shell commands — even if the instructions asked it to. Tool restrictions are enforced by Claude Code, not just by the instructions.

Built-in slash commands

Claude Code has two slash commands for working with agents. Type them directly in the prompt box — no file editing needed.

/agents — list all available agents

Shows every sub-agent Claude Code has discovered in .claude/agents/, including each agent's name and description.

Claude Code prompt
> /agents
# Available agents (3 found in .claude/agents/):
migration-checker
Audits Prisma schema and migration files for breaking changes
security-auditor
Reviews code for OWASP security vulnerabilities
doc-writer
Generates JSDoc and README documentation for modules

/create-agent — generate an agent file interactively

Starts an interactive flow where Claude asks what the agent should do, what tools it needs, and any constraints. It then writes the .md file in .claude/agents/ for you.

Claude Code prompt
> /create-agent
Claude: What should this agent do?
> Check Prisma migrations for breaking changes before deploy
Claude: Should it be able to edit files, or read-only?
> Read-only only
Created .claude/agents/migration-checker.md
/create-agent is the fastest way to get started. You can always open the generated file and refine the instructions manually afterwards.

Using the agent

Once the file exists in .claude/agents/, you can invoke the agent in two ways.

Invoke by name

Use the agent's name field directly in your prompt.

Claude Code prompt
> Use the migration-checker agent to review
the latest migration before I run it.
Claude: Running migration-checker agent...
Read prisma/schema.prisma
Read prisma/migrations/20240501_add_posts.sql
Breaking changes found:
- Post.authorId: renamed from userId — foreign key
references will break until app code is updated.
Action needed before running migration.

Automatic dispatch

When a task matches the agent's description, Claude Code invokes it automatically — you do not need to name it explicitly.

Claude Code prompt
> We're about to deploy. Is the latest migration safe to run?
Claude: I'll use the migration-checker agent to verify...
No breaking changes found. Safe to run.
Description quality matters. The clearer and more specific the description, the better Claude Code can decide when to invoke this agent automatically.

Common agent patterns

Most custom sub-agents fit one of these four patterns. Pick the one that matches your use case and adjust the name, description, and instructions.

PatternToolsGood for
Read-only auditorRead onlySchema checks, security reviews, license scans
Code generatorRead + WriteScaffold components, generate types, write boilerplate
Command runnerRead + BashRun test suite, lint, build, report results
ResearcherRead + WebSearchLook up docs, find package versions, check changelogs

Before you continue

  • Sub-agents live in .claude/agents/<name>.md and are auto-discovered.
  • YAML frontmatter sets name, description, and tools; the body holds instructions.
  • Tool restrictions are enforced — not just suggested in the instructions.
  • Use /create-agent for a fast start, or write the file by hand.
  • Next lesson: MCP in Claude Code.

What's Next

You've created a custom sub-agent. Next: MCP (Model Context Protocol) — connecting Claude Code to external data sources and tools.