Write your first Claude Code slash command (2026 tutorial)
Tutorial · Beginner

Write your first slash command

Build a working slash command from scratch in 15 minutes. Learn the anatomy of a command file, frontmatter fields, prompt structure, and how to iterate to a version that actually works well.

15 minBeginnerUpdated Aug 2026

Prerequisites

  • Claude Code installed and working (claude --version returns a version)
  • A code project you can experiment in
  • A text editor to create files

What you'll learn

  • The anatomy of a slash command file
  • What each frontmatter field does
  • How to write prompts that produce good output
  • How to iterate from a bad first draft to a working command
  • How to ship it to your team via git

Slash commands are the most-used Claude Code primitive. They're how you turn "the thing I keep asking Claude to do" into a repeatable, shareable action. This tutorial builds a real command from scratch — /db-explain, which explains a SQL query and suggests optimizations. By the end, you'll have a working command in your project and understand how to build more.

1
Create the commands directory

Slash commands live in .claude/commands/ at your project root. If your project doesn't have this folder yet, create it. Optionally organize into subfolders by category (like .claude/commands/testing/) — Claude Code treats subfolder paths as command namespaces.

From your project root
mkdir -p .claude/commands/database
cd .claude/commands/database

That's it. Claude Code will pick up any .md file in this directory as a slash command.

2
Write your first command file

A command is a Markdown file with YAML frontmatter. The frontmatter tells Claude Code how to invoke it; the body is the prompt sent to Claude. Create db-explain.md:

.claude/commands/database/db-explain.md
---
description: Explain a SQL query and suggest optimizations
argument-hint: <SQL query or file path>
---

# Explain SQL query

Explain what this SQL query does and suggest any optimizations:

$ARGUMENTS

Cover:
1. What the query returns
2. Any obvious inefficiencies (missing indexes, N+1, unnecessary scans)
3. How you'd rewrite it to be faster if applicable

The command is now available as /db-explain in any Claude Code session in this project. Note the naming: filename without .md extension becomes the command name; subfolder path becomes a namespace (this one is /db-explain — subfolder is metadata, not part of the command name in this case).

🧪 Try it now

Open Claude Code in your project. Type /db-explain SELECT * FROM users WHERE email = 'x@y.com'. You should see Claude explain the query and suggest adding an index on email.

If /db-explain doesn't appear as an autocomplete option, restart Claude Code — it picks up new command files on session start.

3
Understand the frontmatter

Every command has frontmatter with these possible fields:

---
description: Short description shown in the command menu
argument-hint: What kind of input the user should provide
allowed-tools: Read, Write, Bash(git:*)         # optional; limits what Claude can do
model: claude-sonnet-5                          # optional; picks a specific model
---

description: Required. Shown in the autocomplete menu. Keep it short — one line.

argument-hint: Optional. Placeholder text that appears when the user is typing the command. Guides them on what to pass.

allowed-tools: Optional but recommended. Scopes what Claude can do while running this command. See step 5.

model: Optional. If your command needs deep reasoning, specify claude-opus-4-6. If it's a fast, mechanical task, claude-haiku-4-5 is cheaper.

4
Improve the prompt with context gathering

Your first draft works for a raw SQL query pasted in the argument. But what if the user passes a file path (like /db-explain queries/user-lookup.sql)? Improve the prompt to handle both:

Updated .claude/commands/database/db-explain.md
---
description: Explain a SQL query and suggest optimizations
argument-hint: <SQL query or file path>
allowed-tools: Read, Grep, Bash(cat:*)
---

# Explain SQL query

Explain and optimize the SQL provided as: $ARGUMENTS

## Handle these input types
- Raw SQL: use as-is
- File path: read the file first, then explain its contents
- Multiple queries: explain each in order

## Analysis to include
1. What the query returns (in plain English)
2. Any obvious inefficiencies:
   - Missing indexes (mention specific columns)
   - N+1 patterns (subqueries per row)
   - Unnecessary full-table scans
   - SELECT * where specific columns would do
3. Rewritten query if you can meaningfully improve it
4. Any DB-specific concerns (if you can detect PostgreSQL/MySQL/SQLite from syntax)

## Output format
- Start with a one-sentence summary of what the query does
- Then bullet points for issues
- Then rewritten SQL in a code block if applicable

Two improvements: (1) the prompt now handles file paths (we added Read and Grep to allowed-tools so Claude can read the file), and (2) we structured the output format so responses stay consistent.

Structured output

Explicit output format instructions matter more than they seem. Without them, responses drift between runs — sometimes bullet points, sometimes prose. With them, responses stay consistent and are easier to scan.

5
Scope with allowed-tools

allowed-tools limits what Claude can do while running this command. This prevents the command from accidentally doing things you didn't intend — like modifying files when it's supposed to only analyze.

For /db-explain, Claude needs to read files (Read) and search them (Grep), but shouldn't need to write anything or run arbitrary bash. Our config allows Read, Grep, and Bash(cat:*) (only cat commands, no shell freedom).

# Common allowed-tools patterns:

# Read-only analysis
allowed-tools: Read, Grep, Glob

# Read + write, no shell
allowed-tools: Read, Write, Edit, Grep, Glob

# Read + git operations only
allowed-tools: Read, Grep, Bash(git:*)

# Read + specific commands
allowed-tools: Read, Bash(npm test:*), Bash(npm run lint:*)

# Everything (default if unspecified — use sparingly)
allowed-tools: *

Being explicit about tools is a best practice. Even if the command doesn't need protection today, it makes the command's intent clear to teammates reading it later. "Ah, this only reads — I can trust it."

6
Test and iterate

Try the command with different inputs. Note where it produces bad output. Common issues:

Iterate on the prompt to fix the failure modes. This is where good commands get built — the first draft is rarely great, but 3-5 rounds of "try it, notice what's wrong, fix the prompt" produces something reliable.

  • Too verbose: Add "keep response under N sentences" or "skip general advice"
  • Misses obvious issues: Add a checklist to the analysis section
  • Format inconsistent: Be more explicit about output format
  • Uses wrong DB dialect: Add "detect DB dialect from syntax; ask if unclear"
  • Skips things that were requested: Restructure with numbered requirements at the top
7
Ship it to your team

The .claude/ folder should be committed to git. When teammates pull, they get the command automatically. No install step, no per-machine setup.

git add .claude/commands/database/db-explain.md
git commit -m "feat(claude): add /db-explain command"
git push

That's it. Anyone who pulls the branch now has /db-explain available in their Claude Code sessions.

What about ~/.claude?

You can also put commands in ~/.claude/commands/ (your home directory) for personal commands that follow you across projects. Project commands (in .claude/) travel with the repo and are team-shared; user commands are private to you. Both can coexist — project commands override user commands with the same name.

Recap

You just built a slash command. The steps generalize: create a .md file in .claude/commands/, write the frontmatter, write the prompt, iterate. Every command in cctk's 85-command catalog follows this exact pattern — the only difference is that those commands have gone through 3-6 rounds of iteration to be really good.

The next tutorial builds a subagent — for when you need Claude to do focused, extended work rather than a one-shot invocation.

Common gotchas

  • Command not showing up in autocomplete: Restart Claude Code — new commands are picked up on session start. If still missing, check the frontmatter syntax (YAML is picky about indentation and colons).
  • Frontmatter parse errors: YAML requires exact formatting. Common mistakes: tabs instead of spaces, missing quotes around values with colons, curly braces where they don't belong. If in doubt, copy the exact frontmatter from an existing working command.
  • Command works but produces bad output: This is normal for a first draft. Iterate on the prompt — add examples, add explicit output format, add "do NOT" negatives for common failure modes. Each iteration takes 5 minutes and compounds.
  • Command runs but tools are denied: Your allowed-tools is too restrictive. Widen it. If you weren't sure what tools Claude needs, start with a permissive list, then narrow after seeing what actually gets used.
  • Command works in one project but not another: Check that the .claude/ folder was committed and pulled correctly. Also check that the project uses the same command name convention (subfolders create namespaces in some setups).

Get cctk to skip the boilerplate

Everything in this tutorial is already scaffolded in cctk — commands, subagents, hooks, MCP configs. Install and customize instead of starting from scratch.

npx cctk@latest init Get cctk v1.5.2 →