DEV Community

Sangmin Lee
Sangmin Lee

Posted on • Originally published at claudeguide.io

Tool Use in Claude Agent SDK: Complete Guide with Real Examples

Originally published at claudeguide.io/claude-agent-tool-use

Tool Use in Claude Agent SDK: Complete Guide with Real Examples

Tool use lets Claude call functions you define — databases, APIs, file systems, calculators — and act on the results. You pass a list of JSON Schema tool definitions; when Claude decides to use one, the API returns a tool_use content block you execute and feed back as a tool_result. Claude then continues until it has a final answer. This guide covers every step with 20+ copy-paste tool examples.


How Claude Tool Use Works

Claude's tool use follows a four-step loop:

  1. You send a message plus a tools list of JSON Schema definitions.
  2. Claude responds with a tool_use content block naming the tool and its input arguments.
  3. You execute the tool locally and send back a tool_result message.
  4. Claude continues — calling more tools or returning a final text response.

The loop repeats until stop_reason is "end_turn" rather than "tool_use".

According to Anthropic's 2025 usage data, agents that give Claude well-described tools reduce hallucinations by roughly 40% compared to prompting Claude to answer from memory alone. Tool descriptions are not decoration — they are the primary signal Claude uses to decide when to call a function.


Complete Working Example: Start to Finish

This minimal Python snippet defines one tool, sends a user question, handles the tool call, and gets a final answer. Read this before jumping to the full 20-tool library below.


python
import anthropic
import json

client = anthropic.Anthropic()

# Step 1: Define the tool
tools = [
    {
        "name": "get_stock_price",
        "description": "Get the current stock price for a ticker symbol.",
        "input_schema": {
            "type": "object",
            "properties": {
                "ticker": {
                    "type": "string",
                    "description": "Stock ticker symbol, e.g. AAPL, TSLA"
                }
            },
            "required": ["ticker"]
        }
    }
]

messages = [{"role": "user", "content": "What is Apple's stock price?"}]

# Step 2: First API call — Claude decides to use the tool
response = client.messages.create(
    model="claude-sonnet-4-5",
    max_tokens=1024,
    tools=tools,
    messages=messages
)

# Step 3: Handle tool_use content block
if response.stop_reason == "tool_use":
    tool_use_block = next(b for b in response.content if b.type == "tool_use")
    tool_name = tool_use_block.name          # "get_stock_price"
    tool_input = tool_use_block.input        # {"ticker": "AAPL"}
    tool_use_id = tool_use_block.id

    # Execute the tool (your real implementation here)
    def get_stock_price(ticker: str) -

Complete, runnable Python and TypeScript code throughout.

[→ Get Agent SDK Cookbook — $49](https://shoutfirst.gumroad.com/l/ogxhmy?utm_source=claudeguide&utm_medium=article&utm_campaign=claude-agent-tool-use)

*30-day money-back guarantee. Instant download.*
Enter fullscreen mode Exit fullscreen mode

Top comments (0)