Claude and the Anthropic SDK (TypeScript) · Chapter 7 of 21

Parallel calls, errors in tools, and tool_choice

By the end of this chapter you can handle a reply that asks for several tools at once, send every result back the way the API requires, tell Claude when a tool failed, turn parallel calls off when your systems need one call at a time, and say why forcing a tool is refused on Claude Opus 5.5 and what to do instead. 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

Kamla does not ask about one claim at a time. On Monday morning she pastes five claim ids into one message: "How much will these pay?". Your loop from chapter 6 asked for one tool, ran it and sent the result. Now Claude answers that message with two or more tool_use blocks in one reply, and the loop has to answer all of them, in the right place, or the next request is a 400.

The tools themselves fail too. Parchi's claims desk system locks a claim while someone is editing it, and a lookup throws. If your loop crashes, Kamla gets nothing; if it sends the error text as if it were the claim, Claude may answer anyway, confidently and wrongly. And Arjun has old code that forced Claude to call a tool with tool_choice; on Opus 5.5 that request is refused before Claude reads a word of it.

What you build: 4 tasks

Each task has tests. You write the code in the lab's editor and press Check.

  1. Task 1

    both-claims.ts: two claims in one go

    Ask 'claude-opus-5-5' (max_tokens 4096, system: SYSTEM, tools: TOOLS, both given) exactly: How much will CLM-2402 and CLM-2411 pay?

    Loop for at most 6 turns. After each reply print turn N: STOP_REASON, K tool calls (K is the number of tool_use blocks). While stop_reason is 'tool_use', push the reply as it came (role 'assistant', content: reply.content), run every tool_use with runTool, and push ONE user message holding a tool_result for each (tool_use_id, content, is_error). When it stops for another reason, print the text of its text blocks and stop.

  2. Task 2

    locked-claim.ts: a tool that fails

    Ask 'claude-opus-5-5' (max_tokens 4096, system: SYSTEM, tools: TOOLS) exactly: How much will CLM-2405 pay?

    Run the loop as in task 1 (at most 6 turns), but run get_claim with deskLookup (given), which throws for CLM-2405, and the other tools with runTool. Catch the error: print tool error: MESSAGE, and send the message as the tool_result's content with is_error: true. At the end print the text of the last reply. Print no turn lines.

  3. Task 3

    one-at-a-time.ts: no parallel calls

    Ask 'claude-opus-5-5' (max_tokens 4096, system: SYSTEM, tools: TOOLS) exactly: How much will CLM-2403 and CLM-2412 pay?

    Send tool_choice: { type: 'auto', disable_parallel_tool_use: true } on every request, and loop for at most 8 turns. For each tool call print turn N: TOOL INPUT, where INPUT is JSON.stringify(call.input). When the loop ends, print the text of the last reply, then requests: R (how many requests you sent).

  4. Task 4 · interview question

    Interview: forced-choice.ts

    "Arjun's old code forced Sahay to call get_claim. On Opus 5.5 every request fails. Why, and what do you write instead?" Send 'claude-opus-5-5' (max_tokens 4096, tools: TOOLS, no system) the message How much will CLM-2415 pay? with tool_choice: { type: 'tool', name: 'get_claim' }. Catch the Anthropic.BadRequestError and print refused: STATUS TYPE (err.status, err.type).

    Then send it again with no tool_choice, get_claim made strict (strict: true, and additionalProperties: false in its input_schema), and the message How much will CLM-2415 pay? Use the get_claim tool. Print tool: NAME INPUT from the reply's tool_use block (INPUT as JSON.stringify).

What you will learn

  • Parallel tool calls: several tool_use blocks in one reply, and all their results in one user message.
  • The pairing rules that turn a wrong loop into a 400, word for word.
  • is_error: true: how a tool says "I failed" so Claude does not mistake the error for data.
  • tool_choice: auto, none, and disable_parallel_tool_use.
  • Forced tool choice (any, tool): why it is a 400 on Opus 5.5 and Sonnet 5.5, and the three things that replace it.

More in Tools and structure

Build "Parallel calls, errors in tools, and tool_choice" 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.