use futures::StreamExt;
use open_agent::{AgentOptions, ApiProtocol, ContentBlock, StreamEvent, query};
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
let api_key = std::env::var("ANTHROPIC_API_KEY")
.map_err(|_| "set ANTHROPIC_API_KEY to run this example")?;
let options = AgentOptions::builder()
.system_prompt("You are a helpful assistant")
.model("claude-sonnet-5")
.base_url("https://api.anthropic.com/v1")
.protocol(ApiProtocol::Anthropic)
.api_key(api_key)
.max_tokens(500)
.include_reasoning(true)
.build()?;
println!("Sending query to {}...\n", options.model());
let mut stream = query("What's the capital of France? Please be brief.", &options).await?;
print!("Response: ");
while let Some(event) = stream.next().await {
match event? {
StreamEvent::Block(ContentBlock::Text(text)) => {
print!("{}", text.text);
std::io::Write::flush(&mut std::io::stdout())?;
}
StreamEvent::Reasoning(thinking) => {
print!("\n[thinking] {thinking}");
std::io::Write::flush(&mut std::io::stdout())?;
}
StreamEvent::Finish(reason) => {
println!("\n[stopped: {reason}]");
}
_ => {}
}
}
println!("\nQuery complete!");
Ok(())
}