Skip to main content

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

# Install Claude Code CLI
curl -fsSL https://claude.ai/install.sh | bash

# Verify installation
claude --version

SDK Installation (Optional)

# Install the Python SDK for enhanced features
pip install claude-agent-sdk

Quick Start

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

OptionTypeDefaultDescription
workspacestr”.”Working directory for CLI execution
timeoutint300Timeout in seconds
output_formatstr”json”Output format: “json”, “text”, “stream-json”
skip_permissionsboolTrueSkip permission prompts
system_promptstrNoneCustom system prompt to append
allowed_toolslistNoneList of allowed tools
disallowed_toolslistNoneList of disallowed tools
use_sdkboolFalseUse SDK instead of subprocess
modelstrNoneModel to use (e.g., “sonnet”, “opus”)

Examples

Basic Execution

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

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

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

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

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

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

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

# 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:
FlagDescription
-pPrint mode (headless)
--output-format jsonJSON output for parsing
--continueContinue previous session
--dangerously-skip-permissionsSkip permission prompts
--append-system-promptAdd custom system prompt
--allowedToolsRestrict available tools
--disallowedToolsDisable specific tools
--modelSelect model (sonnet, opus)

Error Handling

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