Claude Code SDK for Rust
Rust SDK for Claude Code. See the Claude Code SDK documentation for more information.
Installation
Add this to your Cargo.toml:
[]
= "0.0.10"
= { = "1.0", = ["full"] }
Prerequisites:
- Rust 1.70+
- Node.js
- Claude Code:
npm install -g @anthropic-ai/claude-code
Quick Start
use query;
use StreamExt;
async
Usage
Basic Query
use ;
use StreamExt;
// Simple query
let mut stream = query.await?;
while let Some = stream.next.await
// With options
let options = ClaudeCodeOptions ;
let mut stream = query.await?;
while let Some = stream.next.await
Using Tools
let options = ClaudeCodeOptions ;
let mut stream = query.await?;
while let Some = stream.next.await
Working Directory
use PathBuf;
let options = ClaudeCodeOptions ;
Logging
The Claude Code SDK provides comprehensive structured logging using the tracing ecosystem. This helps with debugging, monitoring, and understanding SDK operations.
Quick Setup
use claude_code_sdk;
async
Environment Variables
Control logging levels with the RUST_LOG environment variable:
# Show info and above (default)
RUST_LOG=claude_code_sdk=info
# Show debug messages (recommended for development)
RUST_LOG=claude_code_sdk=debug
# Show all messages including trace
RUST_LOG=claude_code_sdk=trace
# Show only errors
RUST_LOG=claude_code_sdk=error
# Multiple modules
RUST_LOG=claude_code_sdk=debug,tokio=info
Custom Logging Setup
For more control over logging, set up tracing-subscriber directly:
use ;
// JSON structured logging
registry
.with
.with
.init;
// Custom formatting
registry
.with
.with
.init;
// File logging
let file = new
.create
.write
.truncate
.open?;
registry
.with
.with
.init;
What Gets Logged
The SDK logs various operations at different levels:
- ERROR: CLI not found, connection failures, JSON decode errors
- WARN: Process termination issues, unexpected states
- INFO: Query start/completion, connection events, major operations
- DEBUG: Message processing, command building, subprocess management
- TRACE: Individual message parsing, detailed state changes
Log Fields
Structured logs include useful fields:
Performance Monitoring
Enable performance monitoring with tracing spans:
use ;
async
API Reference
query(prompt: &str, options: Option<ClaudeCodeOptions>)
Main async function for querying Claude.
Parameters:
prompt: The prompt to send to Claudeoptions: Optional configuration
Returns: Result<Pin<Box<dyn Stream<Item = Message>>>, ClaudeSDKError>
Types
See src/types.rs for complete type definitions:
ClaudeCodeOptions- Configuration optionsMessagevariants -Assistant,User,System,ResultContentBlockvariants -Text,ToolUse,ToolResult
Error Handling
use ;
match query.await
Available Tools
See the Claude Code documentation for a complete list of available tools.
Examples
- examples/quick_start.rs - Complete working example with logging
- examples/logging_demo.rs - Advanced logging configurations
Running Examples
# Basic example with default logging
# Advanced logging demo with JSON output
# File logging
# Performance monitoring
RUST_LOG=claude_code_sdk=debug
License
MIT