Skip to main content

Crate claude_code

Crate claude_code 

Source
Expand description

§claude-code

Unofficial Rust client for the Claude Code CLI.

This crate runs claude --print as a subprocess and provides type-safe access to the results. It supports single-shot JSON responses, real-time streaming via stream-json, multi-turn conversations, and structured output with JSON Schema.

§Quick Start

let client = claude_code::ClaudeClient::new(claude_code::ClaudeConfig::default());
let response = client.ask("Say hello").await?;
println!("{}", response.result);

§Feature Flags

FeatureDefaultDescription
streamYesEnables StreamEvent, streaming methods, and re-exports StreamExt.
structuredYesEnables generate_schema for JSON Schema generation.
tracingYesEnables debug/error/info logging via tracing.

Modules§

effort
Known values for the --effort CLI option.
permission_mode
Known values for the --permission-mode CLI option.

Structs§

ClaudeClient
Claude Code CLI client.
ClaudeConfig
Configuration options for Claude CLI execution.
ClaudeConfigBuilder
Builder for ClaudeConfig.
ClaudeResponse
JSON response from the Claude CLI.
Conversation
Stateful multi-turn conversation wrapper around ClaudeClient.
DefaultRunner
Runs claude via tokio::process::Command.
Usage
Token usage.

Enums§

ClaudeError
Error types for claude-code.
CliVersionStatus
Result of comparing the installed CLI version against TESTED_CLI_VERSION.
Preset
Preset defines the base set of CLI flags injected before builder attributes and extra_args.
StreamEventstream
Event emitted from a stream-json response.

Constants§

TESTED_CLI_VERSION
The Claude Code CLI version that this library was tested against.

Traits§

CommandRunner
Trait abstracting CLI execution. Mockable in tests.
StreamExtstream
An extension trait for the Stream trait that provides a variety of convenient combinator functions.

Functions§

check_cli
Checks that the claude CLI is available and returns its version string.
check_cli_version
Checks the installed claude CLI version against TESTED_CLI_VERSION.
check_cli_version_with_path
Checks the CLI at the given path against TESTED_CLI_VERSION.
check_cli_with_path
Checks that the CLI at the given path is available and returns its version string.
generate_schemastructured
Generates a JSON Schema string from a type implementing JsonSchema.