Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Usage Patterns

This section is tuned for fast copy/paste and common agent runtime pitfalls.

Pattern: Explicit Completion for Autonomous Loops

Use require_done_tool(true) when agents should not stop just because the model emits plain text.

#![allow(unused)]
fn main() {
use agent_sdk_rs::{Agent, AnthropicModel};
use agent_sdk_rs::tools::claude_code::all_tools;

async fn run() -> Result<(), Box<dyn std::error::Error>> {
let model = AnthropicModel::from_env("claude-sonnet-4-5")?;
let mut agent = Agent::builder()
    .model(model)
    .tools(all_tools())
    .require_done_tool(true)
    .max_iterations(64)
    .build()?;

let _ = agent.query("Inspect repository and return risks").await?;
Ok(())
}
}

Pattern: Keep Tool Inputs Strict

Use additionalProperties: false and required fields in each tool schema.

Why:

  • prevents silent typo args
  • clearer model contract
  • safer retries

Common Pitfalls

PitfallSymptomFix
No explicit stop tool for autonomous runsearly/ambiguous completionadd done tool + require_done_tool(true)
Loose tool schematool receives malformed argstighten JSON schema + required keys
No iteration capinfinite tool loopsset max_iterations
Mixed provider-specific assumptionsadapter swap breaks behaviorstay inside ChatModel + shared Model* types

Evidence