//! Minimal `query()` example. //! //! Run: //! //! ```sh //! cargo run --example basic //! ``` //! //! Requires the `claude` CLI on `PATH` and authenticated. Output prints each //! assistant text block, then the total cost from the terminal `result` //! message. use claude_agent_sdk::{ClaudeAgentOptions, ContentBlock, Message, query}; use tokio_stream::StreamExt; #[tokio::main] async fn main() -> anyhow::Result<()> { tracing_subscriber::fmt() .with_env_filter( tracing_subscriber::EnvFilter::try_from_default_env() .unwrap_or_else(|_| "claude_agent_sdk=info".into()), ) .init(); let opts = ClaudeAgentOptions::new().with_max_turns(1); let mut stream = query("What is 2 + 2? Reply with just the digit.", opts).await?; while let Some(item) = stream.next().await { match item? { Message::Assistant(a) => { for block in &a.message.content { if let ContentBlock::Text(t) = block { println!("Claude: {}", t.text); } } } Message::Result(r) => { println!( "\n[result] subtype={} turns={} cost_usd={:?}", r.subtype, r.num_turns, r.total_cost_usd ); break; } _ => {} } } Ok(()) }