claude-code
Unofficial — This library is not affiliated with or endorsed by Anthropic. "Claude" is a trademark of Anthropic.
A Rust library for executing Claude Code CLI (claude --print) as a subprocess and handling results in a type-safe way.
Supports both single-shot JSON responses and real-time streaming via --output-format stream-json.
Installation
Add to your Cargo.toml:
[]
= "0.1"
= { = "1", = ["rt-multi-thread", "macros"] }
Prerequisites
The claude CLI must be installed and available in your PATH. See the Claude Code documentation for installation instructions.
Usage
Simple (single-shot)
async
Streaming
use StreamExt;
async
Multi-turn conversation
Use resume to continue a conversation across multiple turns by passing the session ID from a previous response:
async
Conversation API
The Conversation API manages session_id automatically across turns:
async
Per-turn config overrides are supported via ask_with:
conv.ask_with.await?;
Structured Output
Use generate_schema and ask_structured to get typed responses:
use JsonSchema;
use Deserialize;
async
Requires the structured feature. Add it to your dependencies:
[]
= { = "0.1", = ["structured"] }
= "1"
Feature Flags
| Feature | Default | Description |
|---|---|---|
stream |
Yes | Enables ask_stream, StreamEvent, and Conversation stream methods. Adds tokio-stream and async-stream dependencies. |
structured |
No | Enables generate_schema helper for JSON Schema generation. Adds schemars dependency. |
tracing |
Yes | Enables debug/error/info logging via tracing. Adds tracing dependency. |
To use a minimal configuration (only ask()):
[]
= { = "0.1", = false }
Context Minimization Defaults
By default, claude-code applies a minimal configuration to reduce unnecessary context sent to the CLI. This keeps costs down and avoids side effects from user-level settings:
| Default | CLI Flag | Effect |
|---|---|---|
| No session persistence | --no-session-persistence |
Sessions are not saved to disk |
| No settings loaded | --setting-sources "" |
Ignores all user/project settings files |
| Strict MCP config | --strict-mcp-config |
Only uses explicitly provided MCP servers |
| Empty MCP config | --mcp-config '{"mcpServers":{}}' |
No MCP servers enabled |
| No built-in tools | --tools "" |
Disables all built-in tools |
| No slash commands | --disable-slash-commands |
Disables all skills/slash commands |
| Empty system prompt | --system-prompt "" |
No default system prompt |
All of these can be overridden via ClaudeConfigBuilder. For example, to re-enable session persistence:
let config = builder
.no_session_persistence
.build;
Documentation
- Contributing — Commit conventions, branch policy
- Architecture — Module structure, feature flags, error variants
- Releasing — Release workflow, CI/CD
- Testing — Testing strategy
- Claude CLI — Observed CLI behaviors and constraints
Compatibility
Tested against Claude Code CLI v2.1.94. Older or newer versions may work but have not been verified.
Not all CLI options have dedicated ClaudeConfigBuilder methods. Options not yet supported can be passed via extra_args:
let config = builder
.extra_args
.build;
See docs/claude-cli.md for the full option support status.
Minimum Supported Rust Version
Rust 1.93+ (edition 2024).
License
MIT