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

SDK Installation (Optional)

Quick Start

Use as Agent Backend

Delegate an Agent’s LLM turns to the claude CLI instead of the Anthropic API — the flagship CLI backend, works with Claude Pro / Max subscriptions.
cli_backend= is deprecated (removal in 2.0.0). Prefer runtime="claude-code". See Subscription Auth for login details and the CLI Backend Protocol for the full config surface.

Configuration Options

Runtime-only kwargs (passed to execute(), not to the constructor): The callback receives one event dict at a time:

Examples

Basic Execution

With System Prompt

Tool Restrictions

Session Continuation

Using the SDK

Live Progress with on_event

Pass an on_event callback to execute() to see live progress while the run is still in flight — execute() still returns the final text. An Agent using the Claude Code tool surfaces progress with the same on_event kwarg — nothing extra on the Agent side.
The raw one-liner most callers reach for first:
on_event may be sync or async — awaitable results are awaited:
on_event may be sync or async — awaitable results are awaited. A failing callback never breaks the underlying run, so you can plug in any UI without crashing the agent. Use either on_event or its alias on_progress.
use_sdk=True + on_event: When you pass a progress callback, the integration silently reroutes through the subprocess path — even if use_sdk=True — because the SDK path yields no partial events. If you rely on the SDK for a specific reason (e.g. claude-agent-sdk custom tools), don’t pass on_event. Any output_format you set is also ignored, since streaming forces stream-json.

Streaming Output

Iterate stream() directly for full control over each event.

As Agent Tool

As a native async agent tool

Preferred when the agent runs on agent.astart(...) (or any async entrypoint) — the tool is awaited directly on the running loop with no thread hop.
as_async_tool() was added in PraisonAI PR #4022; as_tool() still works and is now also safe to call from inside an async agent runtime.

Environment Variables

CLI Flags Used

The integration uses the following Claude Code CLI flags:

Error Handling

Robustness (PR #4111)

  • A subprocess TimeoutError is now returned as CliBackendResult(error=...) instead of escaping as an exception.
  • --model (-m) and cwd are threaded through, so scheduled runs can pin a model and run in a workspace.
  • CliSessionBinding.is_resume is now set on the second turn of a session, so the resume branch runs instead of re-sending the system prompt every turn.

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