Agent Creator
Guide for creating effective Claude Code agents. An agent is a markdown file with YAML frontmatter stored in .claude/agents/. The frontmatter configures behavior; the markdown body is the system prompt.
Agent File Structure
---
name: my-agent
description: What this agent does and when to use it.
tools: [Read, Grep, Glob]
model: sonnet
permissionMode: dontAsk
maxTurns: 15
---
# Agent System Prompt
Instructions for the agent go here. Write like you're briefing a colleague.
The more specific and structured, the more consistent the behavior.
Frontmatter Fields
Required
| Field | Description |
|---|
name | Unique identifier — lowercase letters and hyphens only (e.g. code-reviewer). Referenced in prompts and CLI flags. |
description | Short sentence explaining what agent does. Claude matches messages against every agent's description to decide which to spawn — make it specific and unambiguous. |
Optional
| Field | Values | Default | Description |
|---|
tools | [Read, Grep, Glob, Edit, Write, Bash, WebFetch, WebSearch] | All tools | Allowlist. Agent can only use listed tools. Omit to inherit all (use sparingly). |
disallowedTools | Tool names to remove | None | Denylist — "everything except X". |
model | haiku, sonnet, opus | inherit | AI model. See Model Selection below. |
permissionMode | default, acceptEdits, dontAsk, bypassPermissions, plan, delegate | default | How agent handles permission prompts. Independent of parent session. |
maxTurns | Positive integer | No limit | Max API round-trips. Safety net against runaway token consumption. |
skills | Array of skill names | None | Skills injected into context at startup. Not inherited from parent — must list explicitly. |
mcpServers | Server names or inline configs | None | MCP servers available to this agent. |
hooks | PreToolUse, PostToolUse, Stop | None | Lifecycle hooks scoped to this agent. See references/memory-and-hooks.md. |
memory | user, project, local | None | Persistent memory scope. See references/memory-and-hooks.md. |
Tools Reference
| Tool | What it does | Danger level |
|---|
Read | Read file contents | Safe (read-only) |
Grep | Search inside files for text patterns | Safe (read-only) |
Glob | Find files by name pattern | Safe (read-only) |
Edit | Modify existing files (find-and-replace) | Modifies files |
Write | Create new files or overwrite existing | Modifies files |
Bash | Run terminal commands | Runs arbitrary commands |
WebFetch | Fetch content from a URL | Network access |
WebSearch | Search the web | Network access |
Read-only set: [Read, Grep, Glob] — use for reviewers, auditors, explorers.
Standard write set: [Read, Write, Edit, Bash, Grep, Glob] — use for implementers.
No nesting: Agents cannot spawn other agents. Even if Task is listed, it has no effect.
Permission Modes
| Mode | Behavior | Use for |
|---|
default | Ask approval for each action | General purpose |
acceptEdits | Auto-accept file edits, still ask for shell commands | Trusted writers |
dontAsk | Block any action needing approval | Read-only agents (second safety layer) |
bypassPermissions | Skip all checks | Extreme caution |
plan | Read-only exploration | Planning agents |
delegate | Team coordination only | Team leads |
Model Selection
| Model | Cost/task | Accuracy | Best for |
|---|
haiku | ~$0.03 | ~60% | File lookups, formatting, bulk ops. 10 Haiku ≈ 1 Sonnet cost. |
sonnet | ~$0.24 | ~85% | Standard dev work, code reviews. |
opus | ~$1.20 | ~95% | Architecture, security-sensitive code. |
inherit | (session) | (session) | Default when not specified. |
Use Sonnet or Opus for sensitive code: auth, payments, database ops, crypto — accuracy matters for security.
Where to Save
| Location | Scope | Shareable |
|---|
.claude/agents/ | Project | Yes (via Git) |
~/.claude/agents/ | User (all projects) | No |
agents/ (in plugin) | Plugin | Yes (via plugin) |
Agents load at session start. If created mid-session, restart or use /agents to reload.
Agent Lifecycle
- Spawn — Fresh context with: agent's system prompt + CLAUDE.md + environment details. Parent summarizes relevant context in the task prompt (lossy).
- Execute — Works independently using only its granted tools.
- Return — Results come back as a summary, not full output.
- Resume (optional) — Pass agent's ID to continue where it left off with full history intact.
What agents receive
- Their custom system prompt (markdown body)
- CLAUDE.md project instructions
- Basic environment details (working directory, platform)
- Preloaded skills (only if listed in
skills field)
What agents do NOT receive
- Parent conversation history
- Parent skills (must be explicit)
- Full Claude Code system prompt (agents get their own simpler prompt)
Context & storage
- Own context window — verbose tool output stays in agent's window, not yours
- Compaction — at ~95% capacity, older messages compressed. Put critical rules in CLAUDE.md (reloaded after compaction)
- Transcripts — saved as
~/.claude/projects/<hash>/agent-<agentId>.jsonl. Auto-removed after 30 days.
Invocation
| Method | How |
|---|
| Auto | Claude matches message against agent descriptions. Best match spawned. Most common. |
| Explicit | Name agent in prompt: "Use the security-auditor agent". |
/agents | Slash command to browse, create, edit agents mid-session. |
--agent name | CLI flag to start session as that agent. |
--agents '{...}' | Register extra agents inline as JSON. |
Foreground: 1 agent → conversation pauses until done.
Background: Multiple agents → run concurrently in parallel. Background agents cannot use MCP tools or ask questions.
Built-in Agents
| Agent | Model | Tools | Purpose |
|---|
Explore | Haiku | Read, Grep, Glob | Search files, discover patterns |
Plan | Inherit | Read-only | Gather context for planning |
General | Inherit | All | Complex multi-step tasks |
Bash | Inherit | Bash only | Terminal commands |
Guide | Haiku | Read-only | Answer Claude Code questions |
Disable a built-in: { "permissions": { "deny": ["Task(Explore)"] } } in settings.
Best Practices
- Minimal tools — [Read, Grep, Glob] can't delete files or run commands. If prompt injection tricks it, damage is contained to reading.
- Specific descriptions — "Handle code tasks" matches everything poorly. "Review TypeScript for security vulnerabilities in the auth module" activates reliably.
- Set maxTurns — Without limit, confused agent loops indefinitely burning tokens. Code reviewer rarely needs >15 turns.
- dontAsk for read-only — Blocks actions needing approval. Second safety layer beyond tool restrictions.
- Sonnet/Opus for sensitive code — Larger models are harder to prompt-inject. Haiku follows instructions more literally.
- Check into version control — Agents are code. PR reviews catch permission escalation and prompt regressions.
- Haiku for parallel research — 10 Haiku agents cost ≈ 1 Sonnet. Finish much faster for simple file lookups.
- Start restrictive, expand later — Easier to grant access than recover from a security incident.
- Store critical rules in CLAUDE.md — Survives compaction. Agent system prompt can be lost at ~95% capacity.
- Provide verification criteria — "Write validateEmail, test: user@example.com → true, invalid → false. Run tests after." gives the agent a feedback loop.
Common Mistakes
- Swiss Army knife agent — One agent with all tools and vague description. Never activates reliably, can't be specialized, security liability.
- Subagents for implementation — They do NOT inherit conversation history. Use for research/information gathering; implement in main session.
- Parallel agents editing same files — Last-write-wins race condition. Define explicit file ownership.
- Expecting conversation inheritance — Agents start fresh. Include ALL necessary context in the task prompt.
- Not reviewing test modifications — Agents sometimes change assertions to match incorrect code. Always review test file changes.
- Correction loops — After 2 failed attempts, start fresh. /clear + better prompt is cheaper than noise accumulation.
- Over-parallelizing — 5 agents for a single-domain feature wastes tokens. Match parallelism to task complexity.
Creation Workflow
- Identify the task — What specific job does this agent do? Be narrow.
- Choose tools — Start with minimum needed. Read-only if possible.
- Choose model — Haiku for simple lookups, Sonnet for standard work, Opus for critical reasoning.
- Set permissions —
dontAsk for read-only agents, default for writers.
- Set maxTurns — Estimate reasonable turns for the task. Add small buffer.
- Write description — Specific enough that Claude activates it for the right tasks and ignores it for others.
- Write system prompt — Tell the agent how to do its job: what to focus on, output format, what to avoid, domain rules.
- Add memory (optional) — Only if agent runs repeatedly across sessions. See references/memory-and-hooks.md.
- Add hooks (optional) — Auto-format, auto-lint, cleanup. See references/memory-and-hooks.md.
- Add skills (optional) — Explicitly list any skills the agent needs.
- Save to
.claude/agents/ and commit to Git.
Advanced Topics