pub struct ClientConfig {Show 32 fields
pub prompt: String,
pub cli_path: Option<PathBuf>,
pub cwd: Option<PathBuf>,
pub model: Option<String>,
pub fallback_model: Option<String>,
pub system_prompt: Option<SystemPrompt>,
pub max_turns: Option<u32>,
pub max_budget_usd: Option<f64>,
pub max_thinking_tokens: Option<u32>,
pub allowed_tools: Vec<String>,
pub disallowed_tools: Vec<String>,
pub permission_mode: PermissionMode,
pub can_use_tool: Option<CanUseToolCallback>,
pub resume: Option<String>,
pub hooks: Vec<HookMatcher>,
pub mcp_servers: McpServers,
pub message_callback: Option<MessageCallback>,
pub env: HashMap<String, String>,
pub verbose: bool,
pub output_format: String,
pub extra_args: BTreeMap<String, Option<String>>,
pub connect_timeout: Option<Duration>,
pub close_timeout: Option<Duration>,
pub end_input_on_connect: bool,
pub read_timeout: Option<Duration>,
pub default_hook_timeout: Duration,
pub version_check_timeout: Option<Duration>,
pub control_request_timeout: Duration,
pub cancellation_token: Option<CancellationToken>,
pub stderr_callback: Option<Arc<dyn Fn(String) + Send + Sync>>,
pub input_format: Option<String>,
pub init_stdin_message: Option<String>,
}Expand description
Configuration for a Claude Code SDK client session.
Use ClientConfig::builder() to construct.
Fields§
§prompt: StringThe prompt text to send to Claude.
cli_path: Option<PathBuf>Path to the Claude CLI binary. If None, auto-discovered via
find_cli().
cwd: Option<PathBuf>Working directory for the Claude process.
model: Option<String>Model to use (e.g., "claude-sonnet-4-5").
fallback_model: Option<String>Fallback model if the primary is unavailable.
system_prompt: Option<SystemPrompt>System prompt configuration.
max_turns: Option<u32>Maximum number of agentic turns before stopping.
max_budget_usd: Option<f64>Maximum USD budget for the session.
max_thinking_tokens: Option<u32>Maximum thinking tokens per turn.
allowed_tools: Vec<String>Explicitly allowed tool names.
disallowed_tools: Vec<String>Explicitly disallowed tool names.
permission_mode: PermissionModePermission mode for the session.
can_use_tool: Option<CanUseToolCallback>Callback invoked when the CLI requests tool use permission.
resume: Option<String>Resume an existing session by ID.
hooks: Vec<HookMatcher>Lifecycle hooks to register for the session.
mcp_servers: McpServersExternal MCP server configurations.
message_callback: Option<MessageCallback>Optional message callback for observe/filter.
env: HashMap<String, String>Extra environment variables to pass to the CLI process.
verbose: boolEnable verbose (debug) output from the CLI.
output_format: StringOutput format. Defaults to "stream-json" for SDK use.
"stream-json" enables realtime streaming — the CLI outputs NDJSON
lines as events happen. This is required for the SDK’s init handshake
and multi-turn conversations.
Other options: "json" (single result blob), "text" (human-readable).
extra_args: BTreeMap<String, Option<String>>Arbitrary extra CLI flags to pass through to the Claude process.
Keys are flag names (without the -- prefix). Values are optional:
Some("value")produces--key valueNoneproduces--key(boolean-style flag)
Uses BTreeMap to guarantee deterministic CLI arg ordering across
invocations (important for reproducible test snapshots).
§Example
use std::collections::BTreeMap;
use claude_cli_sdk::ClientConfig;
let config = ClientConfig::builder()
.prompt("hello")
.extra_args(BTreeMap::from([
("replay-user-messages".into(), None),
("context-window".into(), Some("200000".into())),
]))
.build();connect_timeout: Option<Duration>Deadline for process spawn + init message. None = wait forever.
Default: Some(30s).
close_timeout: Option<Duration>Deadline for child.wait() during close. On expiry the process is
killed. None = wait forever.
Default: Some(10s).
end_input_on_connect: boolIf true, close stdin immediately after spawning the CLI process.
This is required when using --print mode (the default) because
the CLI expects stdin EOF before processing the prompt with
--output-format stream-json.
Default: true.
read_timeout: Option<Duration>Per-message recv deadline. None = wait forever (default).
This is for detecting hung/zombie processes, not for limiting turn time — Claude turns can legitimately take minutes.
default_hook_timeout: DurationFallback timeout for hook callbacks when HookMatcher::timeout is
None.
Default: 30 seconds.
version_check_timeout: Option<Duration>Deadline for the --version check in
check_cli_version().
None = wait forever.
Default: Some(5s).
control_request_timeout: DurationDeadline for control requests (e.g., set_model, set_permission_mode).
If the CLI does not respond within this duration, the request fails with
Error::Timeout.
Default: 30s.
cancellation_token: Option<CancellationToken>Optional cancellation token for cooperative cancellation.
When cancelled, in-flight streams yield Error::Cancelled
and the background reader task shuts down cleanly.
stderr_callback: Option<Arc<dyn Fn(String) + Send + Sync>>Optional callback for CLI stderr output.
input_format: Option<String>Input format for the CLI session.
Set to INPUT_FORMAT_STREAM_JSON for bidirectional multi-turn
streaming (the CLI reads all messages from stdin as NDJSON). When set,
--print is omitted and init_stdin_message
must provide the first message to unblock the init handshake.
Default: None (standard --print mode).
init_stdin_message: Option<String>Optional message to write to stdin immediately after spawning the CLI, before waiting for the init message.
This is needed when using --input-format stream-json (without --print),
because the CLI waits for stdin input before emitting the system/init
message. Writing a trigger message unblocks the init handshake.
The message should be a valid JSON user message for stream-json mode.
Note: the CLI will process this message and produce a response that
flows through the normal message channel.
Default: None (no trigger — suitable for --print mode).
Implementations§
Source§impl ClientConfig
impl ClientConfig
Sourcepub fn builder() -> ClientConfigBuilder<((), (), (), (), (), (), (), (), (), (), (), (), (), (), (), (), (), (), (), (), (), (), (), (), (), (), (), (), (), (), (), ())>
pub fn builder() -> ClientConfigBuilder<((), (), (), (), (), (), (), (), (), (), (), (), (), (), (), (), (), (), (), (), (), (), (), (), (), (), (), (), (), (), (), ())>
Create a builder for building ClientConfig.
On the builder, call .prompt(...), .cli_path(...)(optional), .cwd(...)(optional), .model(...)(optional), .fallback_model(...)(optional), .system_prompt(...)(optional), .max_turns(...)(optional), .max_budget_usd(...)(optional), .max_thinking_tokens(...)(optional), .allowed_tools(...)(optional), .disallowed_tools(...)(optional), .permission_mode(...)(optional), .can_use_tool(...)(optional), .resume(...)(optional), .hooks(...)(optional), .mcp_servers(...)(optional), .message_callback(...)(optional), .env(...)(optional), .verbose(...)(optional), .output_format(...)(optional), .extra_args(...)(optional), .connect_timeout(...)(optional), .close_timeout(...)(optional), .end_input_on_connect(...)(optional), .read_timeout(...)(optional), .default_hook_timeout(...)(optional), .version_check_timeout(...)(optional), .control_request_timeout(...)(optional), .cancellation_token(...)(optional), .stderr_callback(...)(optional), .input_format(...)(optional), .init_stdin_message(...)(optional) to set the values of the fields.
Finally, call .build() to create the instance of ClientConfig.
Source§impl ClientConfig
impl ClientConfig
Sourcepub fn validate(&self) -> Result<()>
pub fn validate(&self) -> Result<()>
Validate the configuration, returning an error for invalid settings.
Checks:
- If
cwdis set, it must exist and be a directory.
This is called automatically by Client::new().
Sourcepub fn to_cli_args(&self) -> Vec<String>
pub fn to_cli_args(&self) -> Vec<String>
Build the CLI argument list for spawning the Claude process.
This does NOT include the binary path itself — just the arguments.
Sourcepub fn to_env(&self) -> HashMap<String, String>
pub fn to_env(&self) -> HashMap<String, String>
Build the environment variable map for the CLI process.
Merges SDK defaults with self.env (user-supplied env takes precedence)
and any SDK-internal env vars.
SDK defaults:
CLAUDE_CODE_SDK_ORIGINATOR=claude_cli_sdk_rs— telemetry originatorTERM=dumb— prevent ANSI escape sequences in output
NOTE: CI=true is intentionally NOT set. Claude Code CLI v2.1+ treats
CI=true as a signal to suppress ALL output (stdout + stderr), which
breaks the NDJSON streaming protocol this SDK relies on.
Trait Implementations§
Auto Trait Implementations§
impl Freeze for ClientConfig
impl !RefUnwindSafe for ClientConfig
impl Send for ClientConfig
impl Sync for ClientConfig
impl Unpin for ClientConfig
impl UnsafeUnpin for ClientConfig
impl !UnwindSafe for ClientConfig
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more