/. These commands can be sent through the SDK to perform actions like compacting context, listing context usage, or invoking custom 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.
Discovering Available Slash Commands
The Claude Agent SDK provides information about available slash commands in the system initialization message. Access this information when your session starts:Sending Slash Commands
Send slash commands by including them in your prompt string, just like regular text. Commands that act on conversation history, such as/compact, need prior messages to work with, so the examples below ask a question first and then send the command as a follow-up to the same conversation:
A query can end with an error result, for example when the
maxTurns / max_turns limit is reached before the work completes. The final result message then has is_error: true and an error subtype such as error_max_turns instead of success.After yielding that final result message, the SDK raises an error, because the CLI process exits with a non-zero code.Wrap the loop in a try/catch in TypeScript or try/except in Python if your command might hit the limit, as shown in Single Message Input, or set maxTurns high enough for the work to complete. In Python, catch Exception: the SDK surfaces error results as a plain Exception.Common Slash Commands
/compact - Compact conversation history
The /compact command reduces the size of your conversation history by summarizing older messages while preserving important context. Compaction needs an existing conversation with at least two prior exchanges to summarize. This example has a conversation first, then compacts it and reads the compact_boundary system message that reports the result:
A
compact_boundary message only arrives when compaction ran. With nothing to summarize, /compact reports the reason instead of raising: the run still ends with a success result, no compact_boundary message is emitted, and the result text carries the message, for example Not enough messages to compact. after a single short exchange. A fresh one-shot query() call starts with empty context, so use this pattern in a session with prior turns, for example in streaming input mode or when resuming a session./clear - Reset conversation context
The /clear command resets the conversation to an empty context, so subsequent prompts start with no prior conversation history. The previous conversation remains on disk and can be returned to by passing its session ID to the resume option.
This is useful in streaming input mode, where you send multiple prompts over a single connection. For one-shot query() calls, each call already starts with empty context, so sending /clear has no practical effect; start a new query() instead.
/clear in the SDK requires Claude Code v2.1.117 or later. In earlier versions it is omitted from slash_commands.Creating Custom Slash Commands
In addition to using built-in slash commands, you can create your own custom commands that are available through the SDK. Custom commands are defined as markdown files in specific directories, similar to how subagents are configured.The
.claude/commands/ directory is the legacy format. The recommended format is .claude/skills/<name>/SKILL.md, which supports the same slash-command invocation (/name) plus autonomous invocation by Claude. See Skills for the current format. The CLI continues to support both formats, and the examples below remain accurate for .claude/commands/.File Locations
Custom slash commands are stored in designated directories based on their scope:- Project commands:
.claude/commands/- Available only in the current project (legacy; prefer.claude/skills/) - Personal commands:
~/.claude/commands/- Available across all your projects (legacy; prefer~/.claude/skills/)
File Format
Each custom command is a markdown file where:- The filename (without
.mdextension) becomes the command name - The file content defines what the command does
- Optional YAML frontmatter provides configuration
Basic Example
Create the.claude/commands directory in your project if it doesn’t exist, then create .claude/commands/refactor.md:
/refactor command that you can use through the SDK.
With Frontmatter
Create.claude/commands/security-check.md:
Using Custom Commands in the SDK
Once defined in the filesystem, custom commands are automatically available through the SDK:Advanced Features
Arguments and Placeholders
Custom commands support dynamic arguments using placeholders: Create.claude/commands/fix-issue.md:
Bash Command Execution
Custom commands can execute bash commands and include their output: Create.claude/commands/git-commit.md:
File References
Include file contents using the@ prefix:
Create .claude/commands/review-config.md:
Organization with Namespacing
Organize commands in subdirectories for better structure:Practical Examples
Pull Request Review Command
Create.claude/commands/review-pr.md:
Claude Code includes bundled
code-review and verify skills. If you name a custom command after one of them, for example .claude/commands/code-review.md, your command shadows the bundled skill and slash_commands lists the name once.Test Runner Command
Create.claude/commands/test.md:
See Also
- Slash Commands - Complete slash command documentation
- Subagents in the SDK - Similar filesystem-based configuration for subagents
- TypeScript SDK reference - Complete API documentation
- SDK overview - General SDK concepts
- CLI reference - Command-line interface