Overview
Slash commands control a Claude Agent SDK session with special / commands. Only
commands that work without an interactive terminal are dispatchable through the
SDK; the system init message lists the ones available in your session.
Discover available commands
for await (const message of query({ prompt: "Hello", options: { maxTurns: 1 } })) {
if (message.type === "system" && message.subtype === "init") {
console.log("Available slash commands:", message.slash_commands);
// e.g. ["clear", "compact", "context", "usage"]
}
}
Send a command
Send a slash command by putting it in the prompt string:
for await (const message of query({ prompt: "/compact", options: { maxTurns: 1 } })) {
if (message.type === "result" && message.subtype === "success") {
console.log("Command executed:", message.result);
}
}
Common built-ins include /compact (summarize history; emits a
compact_boundary system message with pre_tokens) and /clear (reset context;
useful in streaming-input mode, requires v2.1.117+). For one-shot query() calls
/clear has no effect since each call starts empty.
Define custom commands
Custom commands are markdown files; the filename becomes the command name. The
recommended format is .claude/skills/<name>/SKILL.md (which also supports
autonomous invocation); .claude/commands/ is the legacy format and still works.
---
allowed-tools: Read, Grep, Glob
description: Run security vulnerability scan
---
Analyze the codebase for security vulnerabilities including SQL injection,
XSS, exposed credentials, and insecure configurations.
Custom commands support arguments ($0, $1, $ARGUMENTS), embedded bash output
(!`git status`), and file references (@package.json). They appear in the
slash_commands list and are invoked like built-ins:
for await (const message of query({ prompt: "/security-check", options: { maxTurns: 3 } })) {
/* ... */
}
Organization
Group commands in subdirectories (for example .claude/commands/frontend/); the
subdirectory shows in the description but not the command name.
Source