Claude API · Tools and structured output

Let the SDK run the tool loop

Decorate Python functions with @beta_tool and the tool runner calls them and feeds results back until Claude is done. The docstring describes the tool to Claude.

from anthropic import beta_tool

@beta_tool
def get_weather(location: str, unit: str = "celsius") -> str:
    """Get current weather for a location.

    Args:
        location: City and state, e.g., San Francisco, CA.
        unit: Temperature unit, either "celsius" or "fahrenheit".
    """
    return f"72°F and sunny in {location}"

runner = client.beta.messages.tool_runner(
    model="claude-opus-5-5",
    max_tokens=16000,
    tools=[get_weather],
    messages=[{"role": "user", "content": "What's the weather in Paris?"}],
)
for message in runner:
    print(message)

Official Claude API documentation

More tools and structured output commands

All tools and structured output commands · Claude API cheatsheet

Was this page helpful?