# Claude Code CLI - Undocumented Features Analysis

## Executive Summary

This report compares features found in the Claude Code source code (at `D:\Development\third-party\claude-code`) against the official documentation at https://code.claude.com/docs/en/cli-reference.

**Key Findings:**
- 47 undocumented or partially documented CLI flags discovered
- 35+ undocumented environment variables
- Complete stdin/stdout streaming protocol for subprocess integration
- Advanced hook features not fully documented

---

## 1. UNDOCUMENTED CLI FLAGS

### Session Management (Not Documented)

| Flag | Purpose | Example |
|------|---------|---------|
| `--replay-user-messages` | Replay user messages back to stdout (SDK mode) | `claude -p --replay-user-messages` |
| `--no-session-persistence` | Don't save session to disk (print mode only) | `claude -p --no-session-persistence "query"` |

### Input/Output Control (Partially Documented)

| Flag | Purpose | Notes |
|------|---------|-------|
| `--input-format stream-json` | JSON input via stdin | Documented for output but not input |
| `--include-partial-messages` | Stream partial content blocks | Only briefly mentioned |

### Debug & Development

| Flag | Purpose | Example |
|------|---------|---------|
| `--debug <categories>` | Category-filtered debug output | `--debug "api,hooks"` or `--debug "!statsig"` |
| `--mcp-debug` | Detailed MCP server error info | `claude --mcp-debug` |

### Plugin System

| Flag | Purpose | Example |
|------|---------|---------|
| `--comment` | Post review comments to PR (code-review plugin) | Plugin-specific |
| `--max-iterations <n>` | Max iterations for ralph-loop | Plugin-specific |
| `--completion-promise <text>` | Completion signal for ralph-loop | Plugin-specific |

---

## 2. UNDOCUMENTED ENVIRONMENT VARIABLES

### Shell & Command Execution

| Variable | Purpose | Default |
|----------|---------|---------|
| `CLAUDE_CODE_SHELL` | Override automatic shell detection | Auto-detected |
| `CLAUDE_CODE_SHELL_PREFIX` | Wrap all bash commands with prefix script | None |
| `CLAUDE_BASH_MAINTAIN_PROJECT_WORKING_DIR` | Freeze working directory for bash | `false` |
| `CLAUDE_BASH_NO_LOGIN` | Skip login shell initialization | `false` |

### Token & Context Limits

| Variable | Purpose | Default |
|----------|---------|---------|
| `CLAUDE_CODE_FILE_READ_MAX_OUTPUT_TOKENS` | Max tokens for Read tool | ~8000 |
| `MAX_MCP_OUTPUT_TOKENS` | MCP tool response limit | 25000 |
| `CLAUDE_AUTOCOMPACT_PCT_OVERRIDE` | Auto-compact trigger % | System default |

### Session Management

| Variable | Purpose | Example |
|----------|---------|---------|
| `CLAUDE_CODE_EXIT_AFTER_STOP_DELAY` | Auto-exit SDK mode after idle | `5000` (ms) |
| `CLAUDE_SESSION_ID` | String substitution in skills | UUID string |
| `CLAUDE_CODE_TASK_LIST_ID` | Share task list across sessions | Task ID |

### Network & Authentication

| Variable | Purpose |
|----------|---------|
| `CLAUDE_CODE_PROXY_RESOLVES_HOSTS` | Let proxy handle DNS resolution |
| `ANTHROPIC_BEDROCK_BASE_URL` | Custom Bedrock API endpoint |
| `AWS_BEARER_TOKEN_BEDROCK` | Bedrock authentication token |
| `ANTHROPIC_FOUNDRY_API_KEY` | Microsoft Foundry API key |
| `ANTHROPIC_FOUNDRY_BASE_URL` | Foundry resource URL |

### Development & Debugging

| Variable | Purpose | Value |
|----------|---------|-------|
| `IS_DEMO` | Hide email/org in UI (for recordings) | `true` |
| `DISABLE_AUTOUPDATER` | Disable auto-updates | `1` |
| `FORCE_AUTOUPDATE_PLUGINS` | Force plugin updates | `true` |
| `USE_BUILTIN_RIPGREP` | Use system ripgrep instead | `0` |
| `DISABLE_INTERLEAVED_THINKING` | Disable thinking mode | `1` |
| `CLAUDE_CODE_AUTO_CONNECT_IDE` | Disable IDE auto-connection | `false` |
| `ANTHROPIC_LOG` | Debug logging level | `debug` |

### Feature Flags

| Variable | Purpose |
|----------|---------|
| `CLAUDE_CODE_ENABLE_TASKS` | Enable/disable task system |
| `CLAUDE_CODE_DISABLE_BACKGROUND_TASKS` | Disable background tasks + Ctrl+B |
| `CLAUDE_CODE_DISABLE_NONESSENTIAL_TRAFFIC` | Disable telemetry/updates |
| `CLAUDE_CODE_ADDITIONAL_DIRECTORIES_CLAUDE_MD` | Load CLAUDE.md from additional dirs |

---

## 3. STREAMING PROTOCOL (stdin/stdout)

This is the **most critical undocumented feature** for Claudito's subprocess integration.

### Required Flags for Streaming Mode

```bash
claude --print \
       --input-format stream-json \
       --output-format stream-json \
       --verbose
```

### Output Event Types

Each line is a newline-delimited JSON object:

| Event Type | Purpose | Key Fields |
|------------|---------|------------|
| `system` | Session initialization | `subtype: 'init'`, `session_id` |
| `assistant` | Complete assistant response | `message.content[]` (ContentBlocks) |
| `content_block_start` | New content block beginning | `content_block: {type, name, id, input}` |
| `content_block_delta` | Streaming text/JSON | `delta.text` or `delta.partial_json` |
| `content_block_stop` | Content block complete | (no special fields) |
| `result` | Final result | `subtype: 'success'|'error'`, `is_error`, `errors[]` |
| `user` | User input confirmation | `message.content[]` (tool results) |
| `compact` / `summary` | Context compaction | `content` (summary text) |

### Content Block Structure

```typescript
interface ContentBlock {
  type: 'text' | 'tool_use' | 'tool_result';
  text?: string;                    // For text blocks
  name?: string;                    // Tool name (tool_use)
  id?: string;                      // Tool use ID
  input?: Record<string, unknown>;  // Tool input parameters
  tool_use_id?: string;             // Reference to tool (tool_result)
  is_error?: boolean;               // Error flag (tool_result)
}
```

### Token Usage Tracking

```typescript
interface StreamEventUsage {
  input_tokens?: number;
  output_tokens?: number;
  cache_creation_input_tokens?: number;
  cache_read_input_tokens?: number;
}
```

Appears in `event.usage` or `event.message.usage`.

### stdin Input Protocol

Send JSON messages via stdin:

```typescript
const message = JSON.stringify({
  type: 'user',
  message: {
    role: 'user',
    content: content  // string or multimodal array
  }
});
process.stdin.write(message + '\n');
```

**Multimodal content** (images):
```json
{
  "type": "user",
  "message": {
    "role": "user",
    "content": [
      { "type": "text", "text": "What's in this image?" },
      { "type": "image", "source": { "type": "base64", "data": "..." } }
    ]
  }
}
```

### Example Event Flow

```json
{"type":"system","subtype":"init","session_id":"550e8400-e29b-41d4-a716-446655440000"}
{"type":"content_block_start","content_block":{"type":"text"}}
{"type":"content_block_delta","delta":{"text":"Let me help you with that..."}}
{"type":"content_block_stop"}
{"type":"content_block_start","content_block":{"type":"tool_use","name":"Read","id":"toolu_01ABC","input":{"file_path":"/src/app.js"}}}
{"type":"content_block_stop"}
{"type":"user","message":{"content":[{"type":"tool_result","tool_use_id":"toolu_01ABC","content":"file contents..."}]}}
{"type":"result","subtype":"success","message":{"usage":{"input_tokens":5000,"output_tokens":1000}}}
```

---

## 4. SESSION MANAGEMENT

### Session ID Handling

| Scenario | Flag | Behavior |
|----------|------|----------|
| New session | `--session-id {uuid}` | Creates new session with specific ID |
| Resume session | `--resume {uuid}` | Resumes existing session |
| Continue last | `--continue` | Loads most recent session |
| Fork session | `--fork-session` | Creates new ID from resumed session |

### Session Recovery Protocol

When Claude doesn't recognize a session ID (error: "No conversation found with session ID"):
1. System detects the error via stderr regex
2. Old conversation is deleted
3. New conversation created with fresh UUID
4. UI output cleared
5. Restart with new session using `--session-id`

Pattern for detection:
```typescript
const sessionNotFoundPattern = /No conversation found with session ID: ([a-f0-9-]+)/i;
```

---

## 5. PERMISSION MODES

### Runtime Permission Control

| Mode | Flag Value | Behavior |
|------|------------|----------|
| Accept Edits | `--permission-mode acceptEdits` | Auto-approve file edits |
| Plan Mode | `--permission-mode plan` | Review plan before execution |
| Default | (no flag) | Ask for each permission |

### Tool Restriction Flags

```bash
# Allow specific tools to run without prompting
--allowedTools "Bash(npm run:*)" "Read" "Glob"

# Block specific tools entirely
--disallowedTools "Bash(rm:*)" "Edit(.env)"

# Restrict available tools (whitelist)
--tools "Bash,Read,Edit"
```

---

## 6. HOOK SYSTEM DETAILS (Undocumented Features)

### Hook Input Fields (Not Fully Documented)

All hooks receive these common fields via stdin JSON:

```json
{
  "session_id": "abc123",
  "transcript_path": "/path/to/transcript.jsonl",
  "cwd": "/current/working/dir",
  "permission_mode": "default|plan|acceptEdits|dontAsk|bypassPermissions",
  "hook_event_name": "PreToolUse|PostToolUse|Stop|etc"
}
```

### Undocumented Hook Features

**Tool Input Modification (PreToolUse):**
```json
{
  "hookSpecificOutput": {
    "permissionDecision": "allow",
    "updatedInput": {
      "command": "safer_version_of_command"
    }
  }
}
```

**Additional Context Injection:**
```json
{
  "hookSpecificOutput": {
    "additionalContext": "Environment: production. User: admin."
  }
}
```

**Session Environment Persistence (SessionStart only):**
```bash
echo 'export NODE_ENV=production' >> "$CLAUDE_ENV_FILE"
```

### Hook Matchers (Regex Support)

```json
{
  "matcher": "mcp__.*__delete.*",  // Regex pattern
  "matcher": "Write|Edit|Bash",    // Multiple tools (OR)
  "matcher": "*"                    // All tools
}
```

---

## 7. MCP (MODEL CONTEXT PROTOCOL) INTEGRATION

### MCP Tool Naming Convention

Tools from MCP servers follow pattern: `mcp__<server>__<tool>`

Examples:
- `mcp__memory__create_entities`
- `mcp__filesystem__read_file`
- `mcp__github__search_repositories`

### Undocumented MCP Environment Variables

| Variable | Purpose | Default |
|----------|---------|---------|
| `MCP_TIMEOUT` | Server startup timeout (ms) | 5000 |
| `MCP_TOOL_TIMEOUT` | Tool execution timeout (ms) | 30000 |
| `ENABLE_TOOL_SEARCH` | Tool search control | `auto` |

### MCP Config Flags

```bash
# Load from file
--mcp-config ./mcp.json

# Multiple configs
--mcp-config ./mcp1.json ./mcp2.json

# Strict mode (ignore other configs)
--strict-mcp-config --mcp-config ./mcp.json
```

---

## 8. SPECIAL TOOLS DETECTION

Claudito can detect special tool outcomes for enhanced UI:

| Tool | Detection | UI Action |
|------|-----------|-----------|
| `AskUserQuestion` | `content_block_start` with name | Show question UI |
| `EnterPlanMode` | Tool use detected | Show plan review mode |
| `ExitPlanMode` | Tool use detected | Show plan approval buttons |
| `TodoWrite` | Tool input parsed | Update task list display |

---

## 9. CONTEXT WINDOW MANAGEMENT

### Default Context Size

Claude Code uses **200,000 tokens** as the default context window.

### Context Calculation

```typescript
const totalTokens = inputTokens + outputTokens;
const percentUsed = Math.round((totalTokens / 200000) * 100 * 10) / 10;
```

### Auto-Compaction

Triggered by `CLAUDE_AUTOCOMPACT_PCT_OVERRIDE` environment variable (1-100%).

---

## 10. UNDOCUMENTED SLASH COMMANDS

These slash commands are available in interactive mode but not fully documented:

| Command | Purpose |
|---------|---------|
| `/tasks` | Task management interface |
| `/agents` | Agent management |
| `/sandbox` | Sandbox configuration |
| `/keybindings` | Keybinding configuration |
| `/stats` | Session statistics |
| `/usage` | Usage information |
| `/compact` | Manual context compaction |
| `/vim` | Vim mode toggle |
| `/plan` | Plan mode toggle |
| `/add-dir` | Add working directory |
| `/export` | Export conversation |
| `/remote-env` | Remote environment info |

---

## 11. RECOMMENDATIONS FOR CLAUDITO

### Already Implemented (Verified)

Based on `claude-agent.ts`:
- `--input-format stream-json` / `--output-format stream-json`
- `--verbose`
- `--session-id` / `--resume`
- `--permission-mode`
- `--allowedTools` / `--disallowedTools`
- `--append-system-prompt`
- Session recovery on "No conversation found" error

### Features Now Implemented

After this analysis, the following were added to Claudito:

1. **`--max-turns`** - Limit runaway agents (via `agentLimits.maxTurns` setting)
2. **`--include-partial-messages`** - Smoother streaming (via `agentStreaming.includePartialMessages`)
3. **`--no-session-persistence`** - Disposable sessions (via `agentStreaming.noSessionPersistence`)

**Note:** `--max-budget-usd` was not implemented as it requires API key authentication (not applicable when using Claude Code CLI).

### Features to Consider Adding

1. **Hook integration**: Use PreToolUse/PostToolUse for custom validation
2. **`CLAUDE_CODE_EXIT_AFTER_STOP_DELAY`**: Auto-cleanup idle agents
3. **Custom subagents via `--agents` JSON**: Define specialized agents at runtime
4. **`--json-schema`**: Get validated JSON output after workflow
5. **Notification hooks**: Desktop notifications for permission prompts, idle states

---

## 12. CRITICAL IMPLEMENTATION NOTES

### Line Buffering for stdin/stdout

Since events are newline-delimited JSON, proper line buffering is essential:

```typescript
let lineBuffer = '';

process.stdout.on('data', (chunk) => {
  lineBuffer += chunk.toString();
  const lines = lineBuffer.split('\n');
  lineBuffer = lines.pop() || '';  // Keep incomplete line

  for (const line of lines) {
    if (!line.trim()) continue;
    const event = JSON.parse(line);
    processEvent(event);
  }
});
```

### Process Spawning

```typescript
const process = spawn('claude', args, {
  cwd: projectPath,
  shell: true,
  detached: !isWindows  // Process group on Unix for clean termination
});
```

### Graceful Shutdown

On Windows, use `taskkill`. On Unix, use process group signals:
```typescript
if (isWindows) {
  spawn('taskkill', ['/pid', pid.toString(), '/f', '/t']);
} else {
  process.kill(-pid, 'SIGTERM');  // Negative PID = process group
}
```

---

## Summary

The Claude Code CLI has extensive undocumented capabilities, particularly around:

1. **Streaming Protocol**: Full JSON streaming for subprocess integration
2. **Session Management**: UUID-based sessions with recovery
3. **Permission Control**: Runtime mode switching and tool restrictions
4. **Hook System**: Powerful validation and context injection
5. **MCP Integration**: Extensible tool ecosystem

Claudito now leverages many of these features including the new limit and streaming options added as part of this analysis.
