Claude and the Anthropic SDK (TypeScript) · 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 with a zod schema and their own code, 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: true 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
  • TypeScript
  • In the all labs plan

The first 2 chapters of Claude and the Anthropic SDK (TypeScript) 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, push the assistant turn, push 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 switch case, 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: input is typed unknown, and you cast it.

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.

  1. Task 1

    sahay-tools.ts: tools the runner can run

    Export getClaim and getPolicy, each made with betaZodTool: getClaim named get_claim with a zod input of one string field claim_id, getPolicy named get_policy with one string field policy_no, each with a description that says what it looks up.

    Each run function calls runTool (given) with its input and returns the result's text, or throws an Error with that text when isError is true.

  2. Task 2

    run-the-loop.ts: the runner does the loop

    Run client.beta.messages.toolRunner with model 'claude-opus-5-5', max_tokens 4096, system: SYSTEM (given), tools: [getClaim, getPolicy] from ./sahay-tools.ts, and one user message, exactly: How much will CLM-2416 pay?

    Await the runner to get the final message. Print the text of its text blocks, then stop_reason: STOP.

  3. Task 3

    each-turn.ts: 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 await, 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 await runner.done() and print the text of its text blocks.

  4. Task 4 · interview question

    Interview: strict-numbers.ts

    "The claims desk tool takes the claim's number, 2405, and its schema says number. 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: [deskTool(strict), getPolicy]: first deskTool(false), then deskTool(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.stringify), then, indented by two spaces, the first line of the run's final text.

What you will learn

  • betaZodTool: a tool defined once, with a zod input schema and a run function.
  • client.beta.messages.toolRunner(...): the loop, run by the SDK; await runner versus iterating it with for await.
  • 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: true: 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 (TypeScript) are free. This one comes with the all labs plan.