Skip to content

Creating Custom Commands

Every team has that one senior developer who always asks the right questions during code review. They check for error handling edge cases, verify that database queries use indexes, and catch the N+1 query before it hits production. Custom commands let you encode that developer’s judgment into a reusable /review command that any team member can run.

  • How to create custom slash commands from simple prompt templates to full agent workflows
  • Subagent configuration patterns for code review, security auditing, and testing
  • Techniques for sharing commands across your team via version control
  • Strategies for building a library of domain-specific commands

Custom commands in Claude Code are markdown files that live in specific directories. When you type / in the REPL, these appear alongside built-in commands.

Commands are .md files in these locations:

LocationScopeAvailable As
.claude/commands/review.mdProject, shared with team/project:review
~/.claude/commands/standup.mdPersonal, all projects/user:standup

The file name becomes the command name. Subdirectories create namespaced commands: .claude/commands/test/integration.md becomes /project:test:integration.

Create .claude/commands/review.md:

Review the changes in the current git diff. For each file:
1. Check error handling: Are all async operations wrapped in try/catch? Do error responses include meaningful messages?
2. Check performance: Are there N+1 queries? Unnecessary re-renders? Missing indexes for new queries?
3. Check security: Is user input validated? Are SQL queries parameterized? Are secrets handled correctly?
4. Check tests: Do the changes have corresponding test coverage? Are edge cases tested?
Output format:
- Start with a one-line summary: PASS, NEEDS CHANGES, or BLOCKING
- List findings grouped by file
- For each finding, explain WHY it matters and suggest a specific fix

Now any team member can run /project:review and get a consistent, thorough code review.

Commands can include $ARGUMENTS to accept input when invoked:

.claude/commands/explain.md
Explain the following code or concept in the context of this project: $ARGUMENTS
Focus on:
- What it does and why it exists
- How it connects to surrounding code
- Any non-obvious design decisions
- Potential gotchas for future maintainers

Usage: /project:explain the WebSocket reconnection logic in src/realtime/

Subagents are specialized Claude Code instances with their own context window, tools, and instructions. They are ideal for tasks that require focused analysis without polluting your main session’s context.

Create .claude/agents/reviewer.md:

---
description: "Expert code reviewer. Triggers automatically after code changes or when review is requested."
tools:
- Read
- Grep
- Glob
- Bash
model: sonnet
---
You are a senior code reviewer with expertise in TypeScript and Node.js.
When reviewing code:
1. Read the git diff first to understand the scope of changes
2. Check each modified file for:
- Type safety issues (any types, missing null checks)
- Error handling gaps
- Performance concerns (unnecessary iterations, missing memoization)
- Test coverage
3. Be specific: reference exact file paths and line ranges
4. Suggest concrete fixes, not vague improvements
5. Acknowledge good patterns when you see them
Output a structured review with APPROVE, REQUEST_CHANGES, or COMMENT verdict.
FieldRequiredDescription
descriptionYesWhen the agent should be triggered (Claude reads this to decide)
promptNoSystem prompt (alternatively, write it in the body of the file)
toolsNoTool restrictions. Defaults to inheriting all tools
modelNosonnet, opus, haiku, or inherit
maxTurnsNoMaximum agentic turns before the agent stops
disallowedToolsNoTools to explicitly block

Subagents trigger in two ways:

  1. Automatically: Claude reads the description field and decides when a subagent would be helpful. Good descriptions improve automatic triggering.
  2. Explicitly: Ask Claude to “run the reviewer agent” or “use the test-writer to add tests for auth.ts.”

For temporary or experimental agents, define them on the command line:

Terminal window
claude --agents '{
"performance": {
"description": "Analyzes code for performance bottlenecks",
"prompt": "You are a performance engineer. Profile and analyze code for bottlenecks. Focus on algorithmic complexity, memory allocation, and I/O patterns.",
"tools": ["Read", "Grep", "Glob", "Bash"],
"model": "sonnet"
}
}'
.claude/
commands/
review.md # General code review
security-audit.md # Security-focused review
explain.md # Code explanation with context
refactor.md # Guided refactoring workflow
test/
unit.md # Generate unit tests
integration.md # Generate integration tests
e2e.md # Generate end-to-end tests
deploy/
checklist.md # Pre-deployment verification
rollback.md # Rollback assistance
agents/
reviewer.md # Code review subagent
test-writer.md # Test generation subagent
debugger.md # Debugging specialist

Since .claude/commands/ and .claude/agents/ are regular files in your repository, they are automatically shared when committed:

Terminal window
git add .claude/commands/ .claude/agents/
git commit -m "Add team review and test-writing commands"

New team members get your entire command library when they clone the repo.

Subagent does not trigger automatically: Improve the description field. Claude needs clear guidance about when to use each agent. Add phrases like “Use proactively after code changes” or “Triggers when testing is discussed.”

Command is too long and gets truncated: Claude Code loads the full command file into context. Very long commands (more than 500 lines) may crowd out other important context. Keep commands focused and delegate complex logic to subagents.

Agent inherits too many tools: By default, subagents inherit all tools from the parent session. If your agent should not modify files, explicitly restrict its tools to ["Read", "Grep", "Glob", "Bash"].

Team commands conflict with personal commands: Project commands use /project: prefix, personal commands use /user:. If you have both, they are clearly separated. However, if an agent definition exists in both .claude/agents/ and ~/.claude/agents/ with the same name, the project version takes precedence.