SPB Git

Repository Public Research preview

tools-and-agents.md 3.4 KB

# Tools and agents

# Defining a tool

json
{
  "name": "get_weather",
  "description": "Get the current weather for a city. Use it for any weather question. Returns a short text summary.",
  "strict": true,
  "input_schema": {
    "type": "object",
    "properties": {"city": {"type": "string", "description": "City name"}},
    "required": ["city"],
    "additionalProperties": false
  }
}
  • The description decides whether the tool gets used: say what it does, when to use it, what comes back.
  • strict: true (on the tool, not on tool_choice) guarantees schema-valid input. It needs additionalProperties: false and every property listed in required.
  • Tool inputs are model output. Validate them before acting, and parse with a JSON parser, never with string matching.

# The loop

  1. Send messages + tools.
  2. If stop_reason is tool_use: append the entire assistant content; run every tool_use block (they may be several — run them in parallel); append one user message holding all the tool_result blocks (is_error: true for failures, never drop one).
  3. Repeat until end_turn. Always cap the number of turns.
  4. pause_turn means a server tool paused: resend unchanged.

tool_choice: auto (default) and none work everywhere. any and tool are rejected by claude-fable-5-1, claude-fable-5-2, claude-opus-5-5, claude-sonnet-5-5.

# Tool runner (SDK beta helper)

TypeScript: betaZodTool({name, description, inputSchema: z.object({…}), run}) from @anthropic-ai/sdk/helpers/beta/zod, then await client.beta.messages.toolRunner({model, max_tokens, max_iterations, tools, messages}). Python: @beta_tool on a typed function with a docstring, then client.beta.messages.tool_runner(...).until_done(). It validates inputs against the schema and loops for you. Use it unless you need a control flow it cannot express.

# Server tools (run by Anthropic)

Declare them in tools; results arrive as content blocks in the same response.

Tool type Notes
Web search web_search_20260209 web_search_20250305 on Haiku 4.5. Optional max_uses, allowed_domains
Web fetch web_fetch_20260209 web_fetch_20250910 on Haiku 4.5
Code execution code_execution_20260521 Do not add it next to the 20260209 web tools: they already execute code
Tool search tool_search_tool_bm25_20251119 Mark other tools defer_loading: true; never all of them

Server-tool failures come back as HTTP 200 with an error object inside the result block. They are billed per use on top of tokens.

# Client tools defined by Anthropic

{"type": "bash_20250124", "name": "bash"}, {"type": "text_editor_20250728", "name": "str_replace_based_edit_tool"}, {"type": "memory_20250818", "name": "memory"} — declared by type, no input_schema; you execute them. A custom tool that you name bash is a different tool.

# Which agent architecture

Approach You write Runs where
Manual loop The loop and the tools Your infrastructure
Tool runner The tools Your infrastructure
Managed Agents (beta) Agent config, optional custom tools Anthropic sandbox
Claude Agent SDK (separate package) A prompt and options Your infrastructure, with built-in file/shell tools

Before building an agent, check: is the task open-ended, worth the cost, within the model's ability, and are errors recoverable? If not, use a single call or a fixed workflow.

Files main
Menu

Theme