Skip to content

SOP: Implement Tool Use ​

Updated: 2026-05-26

Define tools, implement the agentic loop, and handle tool calls in your application.

Prerequisites ​

  • Working Messages API integration
  • Understanding of JSON Schema for tool definitions

Steps ​

Step 1: Define Your Tool Schema ​

python
tools = [
    {
        "name": "get_weather",
        "description": "Get current weather for a location",
        "input_schema": {
            "type": "object",
            "properties": {
                "location": {
                    "type": "string",
                    "description": "City and state, e.g. San Francisco, CA"
                }
            },
            "required": ["location"]
        }
    }
]

Step 2: Send the Request with Tools ​

bash
curl https://api.anthropic.com/v1/messages \
  -H "Content-Type: application/json" \
  -H "x-api-key: $ANTHROPIC_API_KEY" \
  -H "anthropic-version: 2023-06-01" \
  -d '{
    "model": "claude-sonnet-4-6",
    "max_tokens": 1024,
    "tools": [
      {
        "name": "get_weather",
        "description": "Get current weather for a location",
        "input_schema": {
          "type": "object",
          "properties": {
            "location": {
              "type": "string",
              "description": "City and state"
            }
          },
          "required": ["location"]
        }
      }
    ],
    "messages": [
      {"role": "user", "content": "What is the weather in San Francisco?"}
    ]
  }'

Step 3: Handle the Tool Use Response ​

Parse stop_reason: "tool_use" and extract tool_use blocks:

python
def handle_tool_response(response):
    if response.stop_reason == "tool_use":
        for block in response.content:
            if block.type == "tool_use":
                tool_name = block.name
                tool_input = block.input
                tool_id = block.id
                # Execute your tool logic
                result = execute_tool(tool_name, tool_input)
                return {
                    "role": "assistant",
                    "content": [{"type": "tool_use", "id": tool_id, "name": tool_name, "input": tool_input}]
                }, result

Step 4: Send Results Back and Continue the Loop ​

python
def send_tool_result(tool_use_id, result):
    return client.messages.create(
        model="claude-sonnet-4-6",
        max_tokens=1024,
        messages=[
            {"role": "user", "content": "What is the weather in San Francisco?"},
            {"role": "assistant", "content": [{"type": "tool_use", ...}]},
            {"role": "user", "content": [{"type": "tool_result", "tool_use_id": tool_use_id, "content": result}]}
        ]
    )

Verification Checklist ​

  • [ ] Tool schema is valid JSON Schema (type: object)
  • [ ] Claude returns stop_reason: "tool_use" when appropriate
  • [ ] tool_use block contains name, input, and id
  • [ ] Tool result is sent back with matching tool_use_id
  • [ ] Loop continues until stop_reason: "end_turn" or "max_tokens"

See Also ​