Skip to main content

Module tools

Module tools 

Source
Expand description

Tool definitions and tool-choice configuration.

Tool is the unit you pass to messages.create(...) to expose either a user-defined function (CustomTool) or a server-side built-in (BuiltinTool) like web search or code execution. ToolChoice controls whether and which tool the model must invoke.

For high-level dispatch (registry, parallel calls, agent loop) see crate::tool_dispatch. For #[derive(Tool)] see claude-api-derive.

§Define and send a custom tool

use claude_api::{Client, messages::{CreateMessageRequest, Tool, CustomTool},
    types::ModelId};
use serde_json::json;
let weather = Tool::Custom(
    CustomTool::new(
        "get_weather",
        json!({
            "type": "object",
            "properties": {"city": {"type": "string"}},
            "required": ["city"]
        }),
    )
    .description("Return the current weather for a city."),
);
let client = Client::new(std::env::var("ANTHROPIC_API_KEY").unwrap());
let resp = client
    .messages()
    .create(
        CreateMessageRequest::builder()
            .model(ModelId::SONNET_4_6)
            .max_tokens(512)
            .tools(vec![weather])
            .user("What's the weather in Tokyo?")
            .build()?,
    )
    .await?;
println!("{:?}", resp.stop_reason);

Structs§

CustomTool
User-defined tool with a JSON Schema describing its input.
UserLocation
Approximate user location, used to bias web-search results.

Enums§

BuiltinTool
Server-side built-in tool from Anthropic’s catalog (web search, computer use, code execution, bash, text editor).
KnownBuiltinTool
All server-side built-in tool variants known to this SDK version.
Tool
A tool definition the model can call during generation.
ToolChoice
How (or whether) the model should invoke a tool on this turn.