cli.js that ships inside the npm package. No build step required.
Navigate to the repository root
The pre-built
cli.js lives at the root of the package, alongside package.json.Authenticate
Claude Code needs access to the Anthropic API. Use either method:Option 1 — Environment variable (recommended for scripts):Option 2 — Interactive login:The login command walks you through the OAuth flow and stores credentials locally.
Run a non-interactive query
Use The response is printed to stdout and the process exits.
-p (or --print) to send a single prompt and exit immediately. No interactive session is opened.Key flags
| Flag | Description |
|---|---|
--version, -v | Print the version string and exit. Zero module loading — the fastest path. |
-p, --print | Non-interactive mode. Send one prompt, print the response, and exit. |
-c, --continue | Resume the most recent session in the current working directory. |
-r, --resume <session-id> | Resume a specific session by ID. Optionally pass a search term to filter the interactive picker. |
--model <model-id> | Override the model used for the main agent loop. Accepts aliases (sonnet, opus, haiku) or full IDs. |
--output-format <format> | Output format for --print mode: text (default), json, or stream-json. |
--max-turns <n> | Maximum number of agentic turns in non-interactive mode. |
--dangerously-skip-permissions | Bypass all permission checks. For sandboxed CI/CD pipelines only. |
--add-dir <path> | Grant tool access to an additional directory beyond the current working directory. |
--allowed-tools <tools...> | Comma/space-separated allowlist of tool names (e.g. "Bash(git:*) Edit"). |
--disallowed-tools <tools...> | Comma/space-separated denylist of tool names. |
--system-prompt <prompt> | Replace the default system prompt with a custom one. |
--append-system-prompt <prompt> | Append text to the default system prompt. |
--mcp-config <file> | Load MCP server configurations from a JSON file. |
--settings <file-or-json> | Load additional settings from a JSON file or inline JSON string. |
--bare | Minimal mode: skip hooks, LSP, plugins, attribution, and auto-memory. Auth via ANTHROPIC_API_KEY only. |
--no-session-persistence | Disable writing session transcripts to disk (only works with --print). |
Example: non-interactive query
Example: resume a previous session
Install globally (optional)
If you want to callclaude instead of node cli.js:
The
-p flag puts Claude Code into non-interactive (headless) mode. This is equivalent to the SDK’s QueryEngine path and is well-suited for scripting and automation.