> ## Documentation Index
> Fetch the complete documentation index at: https://docs.praison.ai/llms.txt
> Use this file to discover all available pages before exploring further.

# Claude Code Integration

> Integrate Anthropic's Claude Code CLI for AI-powered coding tasks

# Claude Code Integration

PraisonAI provides seamless integration with Anthropic's Claude Code CLI, supporting both subprocess-based execution and the official Python SDK.

## Installation

### CLI Installation

```bash theme={"theme":{"light":"vitesse-light","dark":"vitesse-dark"}}
# Install Claude Code CLI
curl -fsSL https://claude.ai/install.sh | bash

# Verify installation
claude --version
```

### SDK Installation (Optional)

```bash theme={"theme":{"light":"vitesse-light","dark":"vitesse-dark"}}
# Install the Python SDK for enhanced features
pip install claude-agent-sdk
```

## Quick Start

```python theme={"theme":{"light":"vitesse-light","dark":"vitesse-dark"}}
from praisonai.integrations import ClaudeCodeIntegration

# Create integration
claude = ClaudeCodeIntegration(
    workspace="/path/to/project",
    output_format="json",
    skip_permissions=True
)

# Execute a coding task
result = await claude.execute("Refactor the auth module")
print(result)
```

## Configuration Options

| Option             | Type | Default | Description                                  |
| ------------------ | ---- | ------- | -------------------------------------------- |
| `workspace`        | str  | "."     | Working directory for CLI execution          |
| `timeout`          | int  | 300     | Timeout in seconds                           |
| `output_format`    | str  | "json"  | Output format: "json", "text", "stream-json" |
| `skip_permissions` | bool | True    | Skip permission prompts                      |
| `system_prompt`    | str  | None    | Custom system prompt to append               |
| `allowed_tools`    | list | None    | List of allowed tools                        |
| `disallowed_tools` | list | None    | List of disallowed tools                     |
| `use_sdk`          | bool | False   | Use SDK instead of subprocess                |
| `model`            | str  | None    | Model to use (e.g., "sonnet", "opus")        |

## Examples

### Basic Execution

```python theme={"theme":{"light":"vitesse-light","dark":"vitesse-dark"}}
from praisonai.integrations import ClaudeCodeIntegration

claude = ClaudeCodeIntegration(workspace="/project")

# Simple task
result = await claude.execute("Add error handling to main.py")
print(result)
```

### With System Prompt

```python theme={"theme":{"light":"vitesse-light","dark":"vitesse-dark"}}
claude = ClaudeCodeIntegration(
    workspace="/project",
    system_prompt="You are a Python expert. Follow PEP 8 guidelines."
)

result = await claude.execute("Refactor the utils module")
```

### Tool Restrictions

```python theme={"theme":{"light":"vitesse-light","dark":"vitesse-dark"}}
claude = ClaudeCodeIntegration(
    workspace="/project",
    allowed_tools=["Read", "Write"],  # Only allow file operations
    disallowed_tools=["Bash"]  # Disable shell commands
)

result = await claude.execute("Update the config file")
```

### Session Continuation

```python theme={"theme":{"light":"vitesse-light","dark":"vitesse-dark"}}
claude = ClaudeCodeIntegration(workspace="/project")

# First request
result1 = await claude.execute("Read main.py and understand the structure")

# Continue the session
result2 = await claude.execute(
    "Now refactor the function we discussed",
    continue_session=True
)

# Reset session
claude.reset_session()
```

### Using the SDK

```python theme={"theme":{"light":"vitesse-light","dark":"vitesse-dark"}}
claude = ClaudeCodeIntegration(
    workspace="/project",
    use_sdk=True  # Use claude-agent-sdk if available
)

# Check if SDK is available
print(f"SDK available: {claude.sdk_available}")

result = await claude.execute("Complex refactoring task")
```

### Streaming Output

```python theme={"theme":{"light":"vitesse-light","dark":"vitesse-dark"}}
claude = ClaudeCodeIntegration(workspace="/project")

async for event in claude.stream("Add comprehensive tests"):
    event_type = event.get("type")
    
    if event_type == "assistant":
        print(f"Assistant: {event.get('content')}")
    elif event_type == "tool_use":
        print(f"Using tool: {event.get('name')}")
    elif event_type == "result":
        print(f"Result: {event.get('content')}")
```

### As Agent Tool

```python theme={"theme":{"light":"vitesse-light","dark":"vitesse-dark"}}
from praisonai import Agent
from praisonai.integrations import ClaudeCodeIntegration

claude = ClaudeCodeIntegration(
    workspace="/project",
    skip_permissions=True
)

# Create tool
tool = claude.as_tool()

# Use with agent
agent = Agent(
    name="Code Assistant",
    role="Software Developer",
    goal="Help with coding tasks",
    tools=[tool]
)

result = agent.start("Refactor the authentication module")
```

## Environment Variables

```bash theme={"theme":{"light":"vitesse-light","dark":"vitesse-dark"}}
# API Key (required)
export ANTHROPIC_API_KEY=your-key
# or
export CLAUDE_API_KEY=your-key

# Optional: Set default workspace
export PRAISONAI_CODE_REPO_PATH=/path/to/project
```

## CLI Flags Used

The integration uses the following Claude Code CLI flags:

| Flag                             | Description                 |
| -------------------------------- | --------------------------- |
| `-p`                             | Print mode (headless)       |
| `--output-format json`           | JSON output for parsing     |
| `--continue`                     | Continue previous session   |
| `--dangerously-skip-permissions` | Skip permission prompts     |
| `--append-system-prompt`         | Add custom system prompt    |
| `--allowedTools`                 | Restrict available tools    |
| `--disallowedTools`              | Disable specific tools      |
| `--model`                        | Select model (sonnet, opus) |

## Error Handling

```python theme={"theme":{"light":"vitesse-light","dark":"vitesse-dark"}}
from praisonai.integrations import ClaudeCodeIntegration

claude = ClaudeCodeIntegration(timeout=60)

try:
    result = await claude.execute("Complex task")
except TimeoutError:
    print("Task timed out after 60 seconds")
except Exception as e:
    print(f"Error: {e}")
```

## Best Practices

1. **Use JSON output** for programmatic processing
2. **Set appropriate timeouts** for complex tasks
3. **Use tool restrictions** for security
4. **Enable SDK** for enhanced features when available
5. **Use session continuation** for multi-step tasks
