Claude and the Anthropic SDK · Chapter 8 of 21
The SDK's tool runner
By the end of this chapter you can let the official SDK run Sahay's tool loop for you: tools written once as typed Python functions, a runner that calls Claude, runs every tool and sends the results back until Claude is done, a cap on how many requests it may send, and strict for inputs that always fit their schema. Build it in your browser: a real editor and terminal, AI that explains, and tests that tell you when it works.
- Part: Tools and structure
- 4 tasks
- Python
- In the all labs plan
The first 2 chapters of Claude and the Anthropic SDK are free. This one comes with the all labs plan.
Why you need this
In chapters 6 and 7 you wrote the tool loop by hand: send, find the tool_use blocks, run each one, append the assistant turn, append one user message of results, check the stop reason, go round again. It works, and you now know exactly what crosses the wire. But every new tool means another JSON schema written by hand, another if name == ... branch, and another place for the pairing rules to break. And nothing checks that the input Claude sends matches the schema before your code uses it: block.input is just a dictionary.
Meher wants Sahay to grow (the deadline checker, the hospital list, the pharmacy rules), and Arjun does not want twelve hand-written loops. The SDK has a tool runner that runs the loop for you, and its tools carry their own code and a typed, checked input. Anthropic's own guidance is to reach for it first for a custom-tool agent, and to write the loop by hand only when you need control it does not give.
What you build: 4 tasks
Each task has tests. You write the code in the lab's editor and press Check.
- Task 1
lookups.py: tools the runner can run
Write two functions with @beta_tool (from anthropic import beta_tool): get_claim(claim_id: str) -> str and get_policy(policy_no: str) -> str, each with a docstring that says what it looks up and an Args: line for its argument.
Each one calls run_tool (given) with its argument and returns the result's text, or raises RuntimeError with that text when is_error is True.
- Task 2
run_the_loop.py: the runner does the loop
Make client.beta.messages.tool_runner(...) with model "claude-opus-5-5", max_tokens 4096, system=SYSTEM (given), tools=[lookups.get_claim, lookups.get_policy], and one user message, exactly: How much will CLM-2416 pay?
Call runner.until_done() to get the final message. Print the text of its text blocks, then stop_reason: STOP.
- Task 3
each_turn.py: one turn at a time
Make the same runner for the message How much will CLM-2413 and CLM-2414 pay?, with max_iterations=5. Iterate it with for message in runner:, and for each message print turn N: STOP_REASON [TOOLS], where TOOLS are the names of its tool_use blocks joined with ", " (empty brackets when there are none).
After the loop, get the final message with runner.until_done() and print the text of its text blocks.
- Task 4 · interview question
Interview: strict_numbers.py
"The claims desk tool takes the claim's number, 2405, and its type hint says int. Why did it fail, and what fixes it?" Run a runner twice for How much will CLM-2405 pay? (claude-opus-5-5, max_tokens 4096, system=SYSTEM, max_iterations=5), with tools=[desk_tool(strict), lookups.get_policy]: first desk_tool(False), then desk_tool(True).
For each run print strict S: input INPUT (S is False or True, INPUT the first get_claim_by_number call's input as json.dumps), then, indented by two spaces, the first line of the run's final text.
What you will learn
- @beta_tool: a tool defined once, as a typed function with a docstring.
- client.beta.messages.tool_runner(...): the loop, run by the SDK; runner.until_done() versus iterating it with for message in runner:.
- What the runner does for you (the loop, the pairing, input checks, errors as is_error) and what it does not (your stop-reason rules).
- max_iterations, and why you still read the last stop_reason.
- strict: tool inputs that always match the schema.
More in Tools and structure
Build The SDK's tool runner yourself
Parchi, a Jaipur startup, helps families claim health insurance. You build Sahay, a claims assistant that reads bills and policies with Claude, and measure what caching, models and effort cost. A real editor and terminal in your browser, AI that explains, and a Check that says when it works.
The first 2 chapters of Claude and the Anthropic SDK are free. This one comes with the all labs plan.