Claude Code Custom Commands: Build Your Own AI Agents
Create custom Claude Code agents tailored to your workflow. Learn to build specialized commands that automate your unique development patterns.
Problem: Claude Code's default capabilities don't match your specific workflow. You need a code reviewer that follows your team's standards, or a deployment specialist that knows your infrastructure.
Quick Win: Create your first custom slash command in under 2 minutes:
Create .claude/commands/code-review.md:
Then invoke it with /project:code-review in Claude Code.
Understanding: Custom slash commands are reusable prompts that invoke Claude with specialized instructions. They act like expert consultants you can summon with a single command.
How Custom Commands Transform Your Workflow
Custom commands solve the "remembering the prompt" problem. Instead of typing out detailed instructions every time, you create reusable command files that encode your team's expertise.
Three Approaches to Custom Agents:
- Slash Commands (
.claude/commands/): Invoke on-demand with/project:command-name - Agent Definitions (
.claude/agents/): Persistent sub-agent identities with YAML frontmatter that Claude's orchestrator spawns automatically - CLAUDE.md Instructions: Always-active behaviors that shape every interaction
Commands vs. Agents: These serve different purposes. Slash commands are prompts you invoke manually for specific workflows. Agent definitions in .claude/agents/ configure persistent sub-agents that Claude's Task tool can spawn during orchestration. Think of commands as "tools you pick up" and agents as "team members always available."
Both support the same scoping model:
| Scope | Commands | Agents |
|---|---|---|
| Project | .claude/commands/ | .claude/agents/ |
| User | ~/.claude/commands/ | ~/.claude/agents/ |
Project-scoped files are shareable via git. User-scoped files are personal to your machine and available across all projects.
Separation of Concerns: Like good software architecture, specialized commands perform better than general prompts. A security-focused command with specific checklists catches more issues than asking "review this code."
Creating Effective Custom Commands
Start Simple: Begin with one specific problem you face repeatedly.
Create .claude/commands/security-audit.md:
Invoke with /project:security-audit whenever you need a security review.
Dynamic Arguments: Commands can accept arguments using $ARGUMENTS:
Create .claude/commands/review-file.md:
Invoke with /project:review-file src/auth/login.ts
CLAUDE.md: Always-Active Agent Behavior
For behaviors you want active in every session, add them to your project's CLAUDE.md:
These instructions shape Claude's behavior automatically without needing to invoke a command.
Project vs User Commands:
.claude/commands/- Shared with your team via git~/.claude/commands/- Personal commands on your machine only
Persistent Sub-Agent Definitions with .claude/agents/
For sub-agents that Claude's orchestrator should be able to spawn automatically, use the .claude/agents/ directory. These are Markdown files with YAML frontmatter that define the agent's identity, capabilities, and instructions.
Unlike slash commands (which you invoke manually), agent definitions are available to Claude's Task tool during orchestration. When Claude determines it needs a specialist for a particular task, it can spawn a defined agent with the right context and constraints already configured.
Agents defined here also inherit your project's CLAUDE.md instructions, so they automatically follow your coding standards and project conventions.
For a deeper look at how sub-agents work, including parallel execution, backgrounding with Ctrl+B, and invocation quality, see the agent fundamentals guide.
Common Command Examples
Database Optimizer (.claude/commands/db-optimize.md):
Documentation Writer (.claude/commands/write-docs.md):
Prompting for Specialized Roles
Sometimes you don't need a saved command. Just prompt Claude directly:
This works well for one-off tasks. Save it as a command when you find yourself repeating it.
Next Actions
Ready to build your specialist commands? Start with your biggest pain point:
- Code Quality: Create a reviewer command with your team's standards using our agent fundamentals guide
- Security Focus: Build a vulnerability scanner with our sub-agent design patterns
- Team Workflow: Design collaboration patterns using task distribution strategies
Your custom commands become more valuable as you refine them. Commit them to git, share with your team, and build a library that encodes your collective expertise.
Last updated on