claude-code-cli
Original:🇺🇸 English
Translated
Claude Code CLI usage for automated/headless execution in Daytona sandboxes. Use when running Claude Code commands, configuring authentication, or setting up non-interactive mode.
2installs
Sourceseanchiuai/multishot
Added on
NPX Install
npx skill4agent add seanchiuai/multishot claude-code-cliTags
Translated version includes tags in frontmatterSKILL.md Content
View Translation Comparison →Claude Code CLI
Source: Claude Code CLI Reference, Headless Usage
Installation
bash
# Native (recommended)
curl -fsSL https://claude.ai/install.sh | bash
# NPM (deprecated)
npm install -g @anthropic-ai/claude-codeVerify:
claude --versionAuthentication
Environment Variables
| Variable | Description | Priority |
|---|---|---|
| API key from console.anthropic.com | High (overrides OAuth) |
| OAuth token for subscription auth | Normal |
Warning: Setting bypasses subscription and uses pay-as-you-go API rates.
ANTHROPIC_API_KEYFor Headless/Sandbox Use
Since Claude Code requires browser-based OAuth, use one of:
-
API Key (recommended for CI/sandboxes):bash
export ANTHROPIC_API_KEY="sk-ant-..." claude -p "your prompt" -
OAuth Token Transfer (from authenticated machine):bash
# On local machine: authenticate normally, then copy token # Location: ~/.config/claude-code/auth.json # Transfer to sandbox via environment variable export CLAUDE_CODE_OAUTH_TOKEN="token-from-auth-json"
Check Current Auth
In interactive mode:
/statusNon-Interactive Mode (-p / --print)
For automated execution, use flag:
-pbash
claude -p "your prompt here"This runs the prompt and exits (no REPL).
Essential Flags
Core Flags
| Flag | Description | Example |
|---|---|---|
| Non-interactive mode, exit after response | |
| Continue most recent conversation | |
| Resume specific session | |
| Set model ( | |
Permission & Security
| Flag | Description |
|---|---|
| Skip ALL permission prompts (use with caution) |
| Auto-approve specific tools without prompting |
| Block specific tools entirely |
| Start in specific mode (e.g., |
Tool Control
bash
# Allow specific tools (comma-separated)
claude -p "fix the bug" --allowedTools "Read,Edit,Bash"
# Allow with prefix matching (e.g., any git command)
claude -p "commit changes" --allowedTools "Bash(git:*)"
# Disable all tools
claude -p "explain this" --tools ""
# Use only specific tools
claude -p "review code" --tools "Read,Grep,Glob"Output Formats
| Flag | Value | Description |
|---|---|---|
| | Plain text (default) |
| JSON with metadata | |
| Newline-delimited JSON streaming |
bash
# Get JSON output
claude -p "summarize" --output-format json
# Parse with jq
claude -p "summarize" --output-format json | jq -r '.result'
# Structured output with schema
claude -p "list functions" --output-format json \
--json-schema '{"type":"object","properties":{"functions":{"type":"array","items":{"type":"string"}}}}'System Prompt
| Flag | Description |
|---|---|
| Replace entire system prompt |
| Add to default prompt (recommended) |
| Load replacement from file |
| Append from file |
bash
# Append custom instructions (keeps defaults)
claude -p "review this" --append-system-prompt "Focus on security vulnerabilities"
# Replace entire prompt
claude -p "task" --system-prompt "You are a Python expert"Budget & Limits
| Flag | Description |
|---|---|
| Maximum spend (print mode only) |
| Limit agentic turns (print mode only) |
bash
claude -p "refactor this file" --max-budget-usd 5.00 --max-turns 10Session Management
bash
# Get session ID from output
session_id=$(claude -p "start review" --output-format json | jq -r '.session_id')
# Resume later
claude -p "continue" --resume "$session_id"
# Continue most recent
claude -p "what was I doing?" --continueRecommended Sandbox Configuration
For running in Daytona sandboxes:
bash
# Full automated execution with tool access
ANTHROPIC_API_KEY="sk-ant-..." claude -p "Create a hello world Python script" \
--dangerously-skip-permissions \
--output-format json \
--max-turns 20
# Or with specific tool allowlist
ANTHROPIC_API_KEY="sk-ant-..." claude -p "Fix the bug in auth.py" \
--allowedTools "Read,Edit,Bash,Grep,Glob" \
--output-format json
# With OAuth token instead
CLAUDE_CODE_OAUTH_TOKEN="token..." claude -p "Explain this codebase" \
--dangerously-skip-permissionsExit Codes
| Code | Meaning |
|---|---|
| 0 | Success |
| 1 | Error (check stderr) |
Piped Input
bash
# Pipe file content
cat src/main.ts | claude -p "explain this code"
# Pipe command output
git diff | claude -p "review these changes"
# Pipe multiple files
cat src/*.ts | claude -p "find security issues"Common Patterns
Code Review
bash
git diff HEAD~1 | claude -p "Review these changes for bugs and improvements" \
--append-system-prompt "Focus on security and performance" \
--output-format jsonBug Fix
bash
claude -p "Fix the failing test in tests/auth.test.ts" \
--allowedTools "Read,Edit,Bash(npm test:*)" \
--max-turns 15Code Generation
bash
claude -p "Create a REST API endpoint for user registration" \
--dangerously-skip-permissions \
--max-turns 30Explanation Only (No Tools)
bash
cat complex-file.ts | claude -p "Explain how this works" --tools ""Error Messages to Watch For
In sandbox output, watch for:
- - Invalid token/key
"Authentication failed" - - Need fresh token
"OAuth token has expired" - - Check ANTHROPIC_API_KEY
"Invalid API key" - - Back off and retry
"Rate limit exceeded" - - Reduce input size
"Context length exceeded"
Notes
- Skills/slash commands (like ) only work in interactive mode, not with
/commit-p - mode exits after completion; use
-pfor multi-step tasks--continue - gives full tool access - use in trusted environments only
--dangerously-skip-permissions - Output buffering: Claude streams output, buffer at ~100ms for IPC efficiency