通俗版 Claude Code 文档

结构照原 docs,内容改成真能照着做的人话版。

查看原始文档 目录顺序与官方保持一致

Customize behavior

Slash Commands in the SDK

Slash Commands in the SDK 这一页讲的,就是 Slash Commands in the SDK 这件事在 Claude Code 里到底怎么用。

页面信息

对应原页

Slash Commands in the SDK

页面性质

第三方中文解释页

使用建议

先看人话解释,再对照原页命令和代码

这页不是官方原文,而是顺着官方文档结构做的中文解释版。命令、参数、配置名这些硬东西尽量保留,解释部分则尽量讲成人能照着做的话。

如果你碰到特别敏感的配置、权限或企业环境差异,最好顺手点上面的“查看原始文档”再核一遍。

这一页先讲明白

这页主要讲 Slash Commands in the SDK:Learn how to use slash commands to control Claude Code sessions through the SDK

你可以把它当成"Customize behavior"这块里专门管这一摊事的说明书。

你可以把"Slash Commands in the SDK"理解成 Customize behavior 这一栏里的一把专门工具。这页不是让你背书,而是教你什么时候该把这把工具拿出来。

原文这页大多会按 Discovering Available Slash Commands、Sending Slash Commands、Common Slash Commands、/compact - Compact Conversation History 这些环节往下讲。

翻成人话,大概就是:Discovering Available Slash Commands

第一,先别一上来全开全配。先按最小一步试通,确认没跑偏,再继续往下加。

第二,命令、配置名、参数名这些硬东西尽量保留原样。人话解释是帮你听懂,不是帮你改关键字。

第三,照着原文这几个环节挨个过:Discovering Available Slash Commands -> Sending Slash Commands -> Common Slash Commands -> /compact - Compact Conversation History。像下地先看水路、再试机器、再正式开干,一步一步最稳。

终端里敲

原页关键片段:Discovering Available Slash Commands

这一段不是只让你理解意思,下面这条命令就是现在要跑的。

import { query } from "@anthropic-ai/claude-agent-sdk";

for await (const message of query({
  prompt: "Hello Claude",
  options: { maxTurns: 1 }
})) {
  if (message.type === "system" && message.subtype === "init") {
    console.log("Available slash commands:", message.slash_commands);
    // Example output: ["/compact", "/context", "/usage"]
  }
}
终端里敲

原页关键片段:Sending Slash Commands

看到这里,别光点头,下面这条命令先跑起来再说。

import { query } from "@anthropic-ai/claude-agent-sdk";

// Send a slash command
for await (const message of query({
  prompt: "/compact",
  options: { maxTurns: 1 }
})) {
  if (message.type === "result") {
    console.log("Command executed:", message.result);
  }
}
终端里敲

原页关键片段:/compact - Compact Conversation History

这一段不是只让你理解意思,下面这条命令就是现在要跑的。

import { query } from "@anthropic-ai/claude-agent-sdk";

for await (const message of query({
  prompt: "/compact",
  options: { maxTurns: 1 }
})) {
  if (message.type === "system" && message.subtype === "compact_boundary") {
    console.log("Compaction completed");
    console.log("Pre-compaction tokens:", message.compact_metadata.pre_tokens);
    console.log("Trigger:", message.compact_metadata.trigger);
  }
}
关键片段

原页关键片段:File Format 1

"File Format"这一段里最要紧的原始写法在下面,先看它怎么落地。

Refactor the selected code to improve readability and maintainability.
Focus on clean code principles and best practices.
改配置

原页关键片段:File Format 2

想把这条规矩固定住,就把下面这块老老实实写进去。

---
allowed-tools: Read, Grep, Glob
description: Run security vulnerability scan
model: claude-opus-4-7
---

Analyze the codebase for security vulnerabilities including:
- SQL injection risks
- XSS vulnerabilities
- Exposed credentials
- Insecure configurations
终端里敲

原页关键片段:Using Custom Commands in the SDK

看到这里,别光点头,下面这条命令先跑起来再说。

import { query } from "@anthropic-ai/claude-agent-sdk";

// Use a custom command
for await (const message of query({
  prompt: "/refactor src/auth/login.ts",
  options: { maxTurns: 3 }
})) {
  if (message.type === "assistant") {
    console.log("Refactoring suggestions:", message.message);
  }
}

// Custom commands appear in the slash_commands list
for await (const message of query({
  prompt: "Hello",
  options: { maxTurns: 1 }
})) {
  if (message.type === "system" && message.subtype === "init") {
    // Will include both built-in and custom commands
    console.log("Available commands:", message.slash_commands);
    // Example: ["/compact", "/context", "/usage", "/refactor", "/security-check"]
  }
}
改配置

原页关键片段:Advanced Features 1

想把这条规矩固定住,就把下面这块老老实实写进去。

---
argument-hint: [issue-number] [priority]
description: Fix a GitHub issue
---

Fix issue #$1 with priority $2.
Check the issue description and implement the necessary changes.
终端里敲

原页关键片段:Advanced Features 2

看到这里,别光点头,下面这条命令先跑起来再说。

import { query } from "@anthropic-ai/claude-agent-sdk";

// Pass arguments to custom command
for await (const message of query({
  prompt: "/fix-issue 123 high",
  options: { maxTurns: 5 }
})) {
  // Command will process with $1="123" and $2="high"
  if (message.type === "result") {
    console.log("Issue fixed:", message.result);
  }
}
改配置

原页关键片段:Advanced Features 3

想把这条规矩固定住,就把下面这块老老实实写进去。

---
allowed-tools: Bash(git add *), Bash(git status *), Bash(git commit *)
description: Create a git commit
---

## Context

- Current status: !`git status`
- Current diff: !`git diff HEAD`

## Task

Create a git commit with appropriate message based on the changes.
目录怎么摆

原页关键片段:Organization with Namespacing

这一段主要是认目录和文件摆放位置。先把地方放对,后面才不容易串。

.claude/commands/
├── frontend/
│   ├── component.md      # Creates /component (project:frontend)
│   └── style-check.md     # Creates /style-check (project:frontend)
├── backend/
│   ├── api-test.md        # Creates /api-test (project:backend)
│   └── db-migrate.md      # Creates /db-migrate (project:backend)
└── review.md              # Creates /review (project)
改配置

原页关键片段:Practical Examples 1

光知道意思还不够,这里得把规矩落进配置里,下面这块照着填。

---
allowed-tools: Read, Grep, Glob, Bash(git diff *)
description: Comprehensive code review
---

## Changed Files
!`git diff --name-only HEAD~1`

## Detailed Changes
!`git diff HEAD~1`

## Review Checklist

Review the above changes for:
1. Code quality and readability
2. Security vulnerabilities
3. Performance implications
4. Test coverage
5. Documentation completeness

Provide specific, actionable feedback organized by priority.
改配置

原页关键片段:Practical Examples 2

光知道意思还不够,这里得把规矩落进配置里,下面这块照着填。

---
allowed-tools: Bash, Read, Edit
argument-hint: [test-pattern]
description: Run tests with optional pattern
---

Run tests matching pattern: $ARGUMENTS

1. Detect the test framework (Jest, pytest, etc.)
2. Run tests with the provided pattern
3. If tests fail, analyze and fix them
4. Re-run to verify fixes

预留广告位

正文中段响应式广告 等你后面真接 AdSense,这里再放正式广告。

Documentation Index

这里不是让你背"Documentation Index"这个词,而是让你看它真干活时怎么使。

这里还牵扯作用域,意思就是这条规则到底管当前项目、你个人,还是只管这一趟会话。

Discovering Available Slash Commands

别把这段只当成标题看,它其实是在给"Discovering Available Slash Commands"划边界。

这里还牵扯作用域,意思就是这条规则到底管当前项目、你个人,还是只管这一趟会话。

终端里敲

Discovering Available Slash Commands

这一段不是只让你理解意思,下面这条命令就是现在要跑的。

import { query } from "@anthropic-ai/claude-agent-sdk";

for await (const message of query({
  prompt: "Hello Claude",
  options: { maxTurns: 1 }
})) {
  if (message.type === "system" && message.subtype === "init") {
    console.log("Available slash commands:", message.slash_commands);
    // Example output: ["/compact", "/context", "/usage"]
  }
}

Sending Slash Commands

这一块主要是在说"Sending Slash Commands"真到手上该怎么用,哪里最容易踩坑。

终端里敲

Sending Slash Commands

看到这里,别光点头,下面这条命令先跑起来再说。

import { query } from "@anthropic-ai/claude-agent-sdk";

// Send a slash command
for await (const message of query({
  prompt: "/compact",
  options: { maxTurns: 1 }
})) {
  if (message.type === "result") {
    console.log("Command executed:", message.result);
  }
}

Common Slash Commands

这里不是让你背"Common Slash Commands"这个词,而是让你看它真干活时怎么使。

/compact - Compact Conversation History

别把这段只当成标题看,它其实是在给"/compact - Compact Conversation History"划边界。

终端里敲

/compact - Compact Conversation History

这一段不是只让你理解意思,下面这条命令就是现在要跑的。

import { query } from "@anthropic-ai/claude-agent-sdk";

for await (const message of query({
  prompt: "/compact",
  options: { maxTurns: 1 }
})) {
  if (message.type === "system" && message.subtype === "compact_boundary") {
    console.log("Compaction completed");
    console.log("Pre-compaction tokens:", message.compact_metadata.pre_tokens);
    console.log("Trigger:", message.compact_metadata.trigger);
  }
}

Clearing the conversation

这一段不只是挂个标题,它是在说明"Clearing the conversation"这一块到底负责什么。

这里还牵扯作用域,意思就是这条规则到底管当前项目、你个人,还是只管这一趟会话。

Creating Custom Slash Commands

这一块主要是在说"Creating Custom Slash Commands"真到手上该怎么用,哪里最容易踩坑。

如果你打算把外接能力往里挂,这里提到的 hooks、MCP、skills、memory 都要分清各自负责哪一摊。

File Locations

这一段主要是在把"File Locations"讲实,不是只摆个标题给你看。

这里还牵扯作用域,意思就是这条规则到底管当前项目、你个人,还是只管这一趟会话。

File Format

这一块主要是在说"File Format"真到手上该怎么用,哪里最容易踩坑。

关键片段

File Format 1

"File Format"这一段里最要紧的原始写法在下面,先看它怎么落地。

Refactor the selected code to improve readability and maintainability.
Focus on clean code principles and best practices.
改配置

File Format 2

想把这条规矩固定住,就把下面这块老老实实写进去。

---
allowed-tools: Read, Grep, Glob
description: Run security vulnerability scan
model: claude-opus-4-7
---

Analyze the codebase for security vulnerabilities including:
- SQL injection risks
- XSS vulnerabilities
- Exposed credentials
- Insecure configurations

Using Custom Commands in the SDK

这一块主要是在说"Using Custom Commands in the SDK"真到手上该怎么用,哪里最容易踩坑。

终端里敲

Using Custom Commands in the SDK

看到这里,别光点头,下面这条命令先跑起来再说。

import { query } from "@anthropic-ai/claude-agent-sdk";

// Use a custom command
for await (const message of query({
  prompt: "/refactor src/auth/login.ts",
  options: { maxTurns: 3 }
})) {
  if (message.type === "assistant") {
    console.log("Refactoring suggestions:", message.message);
  }
}

// Custom commands appear in the slash_commands list
for await (const message of query({
  prompt: "Hello",
  options: { maxTurns: 1 }
})) {
  if (message.type === "system" && message.subtype === "init") {
    // Will include both built-in and custom commands
    console.log("Available commands:", message.slash_commands);
    // Example: ["/compact", "/context", "/usage", "/refactor", "/security-check"]
  }
}

Advanced Features

这一块主要是在说"Advanced Features"真到手上该怎么用,哪里最容易踩坑。

改配置

Advanced Features 1

想把这条规矩固定住,就把下面这块老老实实写进去。

---
argument-hint: [issue-number] [priority]
description: Fix a GitHub issue
---

Fix issue #$1 with priority $2.
Check the issue description and implement the necessary changes.
终端里敲

Advanced Features 2

看到这里,别光点头,下面这条命令先跑起来再说。

import { query } from "@anthropic-ai/claude-agent-sdk";

// Pass arguments to custom command
for await (const message of query({
  prompt: "/fix-issue 123 high",
  options: { maxTurns: 5 }
})) {
  // Command will process with $1="123" and $2="high"
  if (message.type === "result") {
    console.log("Issue fixed:", message.result);
  }
}
改配置

Advanced Features 3

想把这条规矩固定住,就把下面这块老老实实写进去。

---
allowed-tools: Bash(git add *), Bash(git status *), Bash(git commit *)
description: Create a git commit
---

## Context

- Current status: !`git status`
- Current diff: !`git diff HEAD`

## Task

Create a git commit with appropriate message based on the changes.
改配置

Advanced Features 4

想把这条规矩固定住,就把下面这块老老实实写进去。

---
description: Review configuration files
---

Review the following configuration files for issues:
- Package config: @package.json
- TypeScript config: @tsconfig.json
- Environment config: @.env

Check for security issues, outdated dependencies, and misconfigurations.

Organization with Namespacing

看到这里,就把"Organization with Namespacing"当成一件真要上手的活来看。

目录怎么摆

Organization with Namespacing

这一段主要是认目录和文件摆放位置。先把地方放对,后面才不容易串。

.claude/commands/
├── frontend/
│   ├── component.md      # Creates /component (project:frontend)
│   └── style-check.md     # Creates /style-check (project:frontend)
├── backend/
│   ├── api-test.md        # Creates /api-test (project:backend)
│   └── db-migrate.md      # Creates /db-migrate (project:backend)
└── review.md              # Creates /review (project)

Practical Examples

这一段是在教你把 .claude/commands/code-review.md: 真正建出来。文件放哪儿、字段怎么写、建完怎么验,都得跟着看。

改配置

Practical Examples 1

光知道意思还不够,这里得把规矩落进配置里,下面这块照着填。

---
allowed-tools: Read, Grep, Glob, Bash(git diff *)
description: Comprehensive code review
---

## Changed Files
!`git diff --name-only HEAD~1`

## Detailed Changes
!`git diff HEAD~1`

## Review Checklist

Review the above changes for:
1. Code quality and readability
2. Security vulnerabilities
3. Performance implications
4. Test coverage
5. Documentation completeness

Provide specific, actionable feedback organized by priority.
改配置

Practical Examples 2

光知道意思还不够,这里得把规矩落进配置里,下面这块照着填。

---
allowed-tools: Bash, Read, Edit
argument-hint: [test-pattern]
description: Run tests with optional pattern
---

Run tests matching pattern: $ARGUMENTS

1. Detect the test framework (Jest, pytest, etc.)
2. Run tests with the provided pattern
3. If tests fail, analyze and fix them
4. Re-run to verify fixes
终端里敲

Practical Examples 3

真到动手的时候了,下面这条直接敲一遍,看它回什么。

import { query } from "@anthropic-ai/claude-agent-sdk";

// Run code review
for await (const message of query({
  prompt: "/code-review",
  options: { maxTurns: 3 }
})) {
  // Process review feedback
}

// Run specific tests
for await (const message of query({
  prompt: "/test auth",
  options: { maxTurns: 5 }
})) {
  // Handle test results
}

See Also

这一段主要是在把"See Also"讲实,不是只摆个标题给你看。

照着做一遍

如果你不想来回翻,就先照这几步顺着做。

每做完一步就看一下结果,再决定要不要继续往下。

终端里敲

第 1 步:Discovering Available Slash Commands

这一段不是只让你理解意思,下面这条命令就是现在要跑的。

import { query } from "@anthropic-ai/claude-agent-sdk";

for await (const message of query({
  prompt: "Hello Claude",
  options: { maxTurns: 1 }
})) {
  if (message.type === "system" && message.subtype === "init") {
    console.log("Available slash commands:", message.slash_commands);
    // Example output: ["/compact", "/context", "/usage"]
  }
}
终端里敲

第 2 步:Sending Slash Commands

看到这里,别光点头,下面这条命令先跑起来再说。

import { query } from "@anthropic-ai/claude-agent-sdk";

// Send a slash command
for await (const message of query({
  prompt: "/compact",
  options: { maxTurns: 1 }
})) {
  if (message.type === "result") {
    console.log("Command executed:", message.result);
  }
}
终端里敲

第 3 步:/compact - Compact Conversation History

这一段不是只让你理解意思,下面这条命令就是现在要跑的。

import { query } from "@anthropic-ai/claude-agent-sdk";

for await (const message of query({
  prompt: "/compact",
  options: { maxTurns: 1 }
})) {
  if (message.type === "system" && message.subtype === "compact_boundary") {
    console.log("Compaction completed");
    console.log("Pre-compaction tokens:", message.compact_metadata.pre_tokens);
    console.log("Trigger:", message.compact_metadata.trigger);
  }
}
关键片段

第 4 步:File Format 1

"File Format"这一段里最要紧的原始写法在下面,先看它怎么落地。

Refactor the selected code to improve readability and maintainability.
Focus on clean code principles and best practices.

一眼看懂这一页

先把这页到底在讲什么看明白,再去碰具体命令和配置,最不容易绕晕。

Slash Commands in the SDK
   |
   v
这是 Customize behavior 里的一摊要紧活
   |
   v
先弄懂,再下手

文末提醒

这站会按官方 docs 的导航和内容变化继续重生成,原站加页、删页、改页时,这里会跟着更新。

人话解释会尽量顺着原页往下讲,但命令、参数名、配置名这些硬东西还是保留原样,免得你抄过去跑不起来。