pub struct Client { /* private fields */ }
Expand description
High-level client for interacting with Claude Code CLI
The Client
provides a type-safe, async interface to Claude Code with support
for different output formats, configuration options, and both simple and advanced
response handling.
§Examples
Basic usage:
let client = Client::new(Config::default());
let response = client.query("Hello").send().await?;
println!("{}", response);
With configuration:
let client = Client::builder()
.model("claude-3-opus-20240229")
.stream_format(StreamFormat::Json)
.timeout_secs(60)
.build();
Implementations§
Source§impl Client
impl Client
Sourcepub fn builder() -> ClientBuilder
pub fn builder() -> ClientBuilder
Create a new client builder for fluent configuration
Sourcepub fn query(&self, query: impl Into<String>) -> QueryBuilder
pub fn query(&self, query: impl Into<String>) -> QueryBuilder
Create a query builder for the given query string
§Examples
let client = Client::new(Config::default());
let response = client
.query("Explain Rust ownership")
.send()
.await?;
Sourcepub async fn send(&self, query: &str) -> Result<String>
pub async fn send(&self, query: &str) -> Result<String>
Send a query and return just the text content (backwards compatible)
This is the simplest way to get a response from Claude. For access to
metadata, costs, and raw JSON, use send_full
.
§Examples
let client = Client::new(Config::default());
let answer = client.send("What is 2 + 2?").await?;
assert_eq!(answer.trim(), "4");
Sourcepub async fn send_full(&self, query: &str) -> Result<ClaudeResponse>
pub async fn send_full(&self, query: &str) -> Result<ClaudeResponse>
Send a query and return the full response with metadata and raw JSON
This method provides access to the complete response from Claude Code, including metadata like costs, session IDs, and the raw JSON for advanced parsing or storage.
§Examples
let client = Client::builder()
.stream_format(StreamFormat::Json)
.build();
let response = client.send_full("Hello").await?;
println!("Content: {}", response.content);
if let Some(metadata) = &response.metadata {
println!("Cost: ${:.6}", metadata.cost_usd.unwrap_or(0.0));
println!("Session: {}", metadata.session_id);
}
// Access raw JSON for custom parsing
if let Some(raw) = &response.raw_json {
// Custom field extraction
let custom_field = raw.get("custom_field");
}
Trait Implementations§
Auto Trait Implementations§
impl Freeze for Client
impl RefUnwindSafe for Client
impl Send for Client
impl Sync for Client
impl Unpin for Client
impl UnwindSafe for Client
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> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
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