Available on crate features
async and conversation only.Expand description
The agent loop runner.
Client::run drives the tool_use -> tool_result loop until the
model stops requesting tools (or the iteration cap is hit). Collapses
the manual loop in examples/tool_use.rs into a single call.
Gated on the conversation feature in addition to the parent
tool_dispatch module’s async gate.
§Example
use claude_api::{Client, conversation::Conversation,
tool_dispatch::{RunOptions, ToolError, ToolRegistry}, types::ModelId};
use serde_json::json;
let client = Client::new(std::env::var("ANTHROPIC_API_KEY").unwrap());
let mut registry = ToolRegistry::new();
registry.register_described(
"get_weather",
"Return the current weather for a city.",
json!({
"type": "object",
"properties": {"city": {"type": "string"}},
"required": ["city"]
}),
|input| async move {
let city = input["city"].as_str().ok_or_else(|| ToolError::invalid_input("missing city"))?;
Ok(json!({"city": city, "temp_f": 72}))
},
);
let mut convo = Conversation::new(ModelId::SONNET_4_6, 1024);
convo.push_user("What's the weather in Paris?");
let result = client.run(&mut convo, ®istry, RunOptions::default()).await?;
println!("{:?}", result.stop_reason);Structs§
- Cost
Budget pricing - Cost budget for the agent loop, paired with the pricing table used to
evaluate
Conversation::cost. - RunOptions
- Optional knobs for the agent loop.