[BUG] SDK prompt parsing issue: prompts starting with "-" are interpreted as CLI options
Environment
- Platform (select one):
- [x] Anthropic API
- [ ] AWS Bedrock
- [ ] Google Vertex AI
- [ ] Other: <!-- specify -->
- Claude CLI version: 1.0.53 (Claude Code)
- Operating System: macOS
- Terminal: WezTerm
Bug Description
When using the Claude Code SDK's query() function with prompts that start with a dash (-), the prompt is incorrectly interpreted as a CLI option instead of being treated as the prompt content. This causes the underlying CLI execution to fail or produce unexpected results.
Steps to Reproduce
- Use the JavaScript SDK to call
query()with a prompt starting with-:
import { query } from "@anthropic-ai/claude-code";
// This fails
(async () => {
try {
for await (const message of query({ prompt: "-v" })) {
console.log(message);
}
} catch (error) {
console.error(error);
}
})();
// This also fails
(async () => {
try {
for await (const message of query({ prompt: "-1 + 2 = ?" })) {
console.log(message);
}
} catch (error) {
console.error(error);
}
})();
- Run with
DEBUG=1to see the underlying CLI execution
Expected Behavior
The prompt should be treated as content to be processed by Claude, regardless of whether it starts with a dash or contains option-like strings.
Actual Behavior
Example 1: Prompt "-v"
When running with DEBUG=1, the following log appears:
Spawning Claude Code process: node /path/to/claude --output-format stream-json --verbose --print -v
Error during query: SyntaxError: Unexpected non-whitespace character after JSON at position 3 (line 1 column 4)
at JSON.parse (<anonymous>)
Example 2: Prompt "-1 + 2 = ?"
Claude Code stderr: error: unknown option '-1 + 2 = ?'
Root Cause Analysis
Based on the debug output, it appears the SDK is spawning the CLI process with arguments like:
spawn('node', ['/path/to/claude', '--output-format', 'stream-json', '--verbose', '--print', prompt])
The issue is that the prompt parameter is being passed directly as a CLI argument without proper separation from the options. When the prompt starts with -, the CLI parser interprets it as an option flag rather than the prompt content.
Proposed Solution
The spawn arguments should include -- before the prompt to explicitly separate options from arguments:
spawn('node', ['/path/to/claude', '--output-format', 'stream-json', '--verbose', '--print', '--', prompt])
This follows standard Unix CLI conventions where -- indicates the end of options and the beginning of positional arguments.
Impact
This bug can cause:
- Unexpected CLI behavior when prompts contain option-like strings
- Parsing errors and failed queries
- Potential security concerns if prompts can inadvertently modify CLI behavior
Additional Context
This issue affects the SDK's reliability and violates the principle of least surprise, as users would expect any string to be valid as a prompt content.
This issue has 1 comment on GitHub. Read the full discussion on GitHub ↗