22 })) {22 })) {
23 if (message.type === "system" && message.subtype === "init") {23 if (message.type === "system" && message.subtype === "init") {
24 console.log("Available slash commands:", message.slash_commands);24 console.log("Available slash commands:", message.slash_commands);
25 // Example output: ["/compact", "/context", "/usage"]25 // Example output: ["clear", "compact", "context", "usage"]
26 }26 }
27 }27 }
28 ```28 ```
36 async for message in query(prompt="Hello Claude", options=ClaudeAgentOptions(max_turns=1)):36 async for message in query(prompt="Hello Claude", options=ClaudeAgentOptions(max_turns=1)):
37 if isinstance(message, SystemMessage) and message.subtype == "init":37 if isinstance(message, SystemMessage) and message.subtype == "init":
38 print("Available slash commands:", message.data["slash_commands"])38 print("Available slash commands:", message.data["slash_commands"])
39 # Example output: ["/compact", "/context", "/usage"]39 # Example output: ["clear", "compact", "context", "usage"]
40 40
41 41
42 asyncio.run(main())42 asyncio.run(main())
80 80
81## Common Slash Commands81## Common Slash Commands
82 82
83### `/compact` - Compact Conversation History83### `/compact` - Compact conversation history
84 84
85The `/compact` command reduces the size of your conversation history by summarizing older messages while preserving important context:85The `/compact` command reduces the size of your conversation history by summarizing older messages while preserving important context:
86 86
117 ```117 ```
118</CodeGroup>118</CodeGroup>
119 119
120### Clearing the conversation120### `/clear` - Reset conversation context
121 121
122The interactive `/clear` command is not available in the SDK. Each `query()` call already starts a fresh conversation, so to clear context, end the current `query()` and start a new one. The previous conversation stays on disk and can be returned to by passing its session ID to the [`resume` option](/en/agent-sdk/sessions#resume-by-id).122The `/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](/en/agent-sdk/sessions#resume-by-id).
123
124This is useful in [streaming input mode](/en/agent-sdk/streaming-vs-single-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.
125
126<Note>
127 `/clear` in the SDK requires Claude Code v2.1.117 or later. In earlier versions it is omitted from `slash_commands`.
128</Note>
123 129
124## Creating Custom Slash Commands130## Creating Custom Slash Commands
125 131
199 if (message.type === "system" && message.subtype === "init") {205 if (message.type === "system" && message.subtype === "init") {
200 // Will include both built-in and custom commands206 // Will include both built-in and custom commands
201 console.log("Available commands:", message.slash_commands);207 console.log("Available commands:", message.slash_commands);
202 // Example: ["/compact", "/context", "/usage", "/refactor", "/security-check"]208 // Example: ["clear", "compact", "context", "usage", "refactor", "security-check"]
203 }209 }
204 }210 }
205 ```211 ```
224 if isinstance(message, SystemMessage) and message.subtype == "init":230 if isinstance(message, SystemMessage) and message.subtype == "init":
225 # Will include both built-in and custom commands231 # Will include both built-in and custom commands
226 print("Available commands:", message.data["slash_commands"])232 print("Available commands:", message.data["slash_commands"])
227 # Example: ["/compact", "/context", "/usage", "/refactor", "/security-check"]233 # Example: ["clear", "compact", "context", "usage", "refactor", "security-check"]
228 234
229 235
230 asyncio.run(main())236 asyncio.run(main())