← Blog

How to Chain Claude API Function Calls

2026-10-28 · 5 min read · SubToAPI Team

How to Chain Claude API Function Calls

Chaining Claude API function calls means letting the model's output from one tool call become the input to the next — building a sequence of actions instead of a single request/response pair. You do this by looping: send a message with tools defined, read the tool_use block Claude returns, execute the function, feed the result back as a tool_result, and repeat until Claude stops requesting tools and returns a final text answer.

The short answer: there's no special "chaining" endpoint. You implement a loop in your own backend that keeps appending messages to the same conversation until the model signals it's done (stop_reason is end_turn instead of tool_use). The rest of this article covers the patterns that make that loop reliable in production: state management, sequencing logic, error recovery, and when to parallelize versus chain strictly in order.

The basic chaining loop

At its core, chaining is a while loop around a single conversation:

let messages = [{ role: "user", content: "Book a flight and then email me the confirmation" }];

while (true) {
  const response = await callClaude(messages, tools);
  messages.push({ role: "assistant", content: response.content });

  const toolUse = response.content.find(b => b.type === "tool_use");
  if (!toolUse) break; // Claude gave a final answer, chain is done

  const result = await runTool(toolUse.name, toolUse.input);
  messages.push({
    role: "user",
    content: [{ type: "tool_result", tool_use_id: toolUse.id, content: JSON.stringify(result) }]
  });
}

Each iteration appends the assistant's tool_use and your tool_result to the same messages array, so Claude has full context when deciding the next step. This is the pattern behind agentic workflows, data-lookup pipelines, and multi-step automations.

Designing the chain: sequential vs. branching

Not every chain is a straight line. Three common shapes come up repeatedly:

Decide the shape before writing the loop. If your functions are independent, don't force Claude to call them one at a time — batch the tool_result blocks for all pending tool_use calls in a single assistant turn before sending the next request.

Managing state across calls

The biggest source of bugs in chained function calls isn't the model — it's state drift in your own code. A few rules that hold up well:

  1. Keep the full message history. Don't summarize or trim it mid-chain unless you're hitting context limits; Claude needs the prior tool_use/tool_result pairs to reason correctly about what's already been done.
  2. Match tool_use_id exactly. Every tool_result must reference the id of the tool_use block it answers. Mismatches cause the API to reject the request or, worse, silently confuse the model.
  3. Serialize results consistently. Return structured JSON from your functions, not free-form text. Claude handles structured tool outputs more reliably across multiple hops.
  4. Cap the loop. Set a max iteration count (10–15 is usually plenty) so a misbehaving chain can't run indefinitely and burn tokens.

Handling errors mid-chain

A function call can fail halfway through a chain — an API times out, a database query returns empty, a third-party service rate-limits you. Don't silently drop the step. Return the error as the tool_result content:

{
  "type": "tool_result",
  "tool_use_id": "toolu_01abc",
  "content": "Error: upstream service returned 429. Retry after 30s.",
  "is_error": true
}

Claude can see the error and decide whether to retry, try an alternative function, or surface the failure to the user — which is usually better than your code making that call blindly. This is also why chains should be idempotent where possible: if Claude retries a step, running it twice shouldn't duplicate side effects like charges or emails.

Where SubToAPI fits

If you're already chaining function calls against Claude, the parts that get tedious fast are issuing and rotating API keys per application, tracking token usage per chain, and streaming partial output back to a frontend while the loop is still running server-side. SubToAPI turns your existing Claude access into a standard HTTPS API with sub_live_... application keys, built-in streaming, and per-key usage metadata, so your chaining loop looks the same but you get team seats and cost visibility without building that tooling yourself. See the tool use docs and streaming docs for the request/response shapes, or start with the quickstart. Plans start at the Solo tier on pricing, with a free trial at signup.

Keeping chains observable

Long chains are hard to debug after the fact if you don't log each hop. At minimum, log: the tool name and input, the result (or error), the stop_reason of each model response, and token usage per call. This lets you answer the two questions that come up constantly in production — "why did the chain stop early?" and "why did this cost so much?" — without reconstructing the conversation from scratch.

questions

Does Claude support chaining function calls natively, or do I have to build the loop myself? You build the loop yourself. Claude returns one tool_use block (or several) per response; your code executes the function and sends the result back as the next message. There's no built-in orchestration endpoint for multi-step chains.

Can I run two function calls at the same time instead of strictly in sequence? Yes, if Claude requests multiple independent tool_use blocks in one response, execute them concurrently and return all the tool_result blocks together in your next message rather than chaining them one by one.

What's the biggest cause of broken chains? Mismatched tool_use_id values and trimmed message history. Keep every tool_use/tool_result pair intact in the conversation, and the model will correctly track what's already been done across the chain.

Turn your Claude access into an HTTPS API

SubToAPI gives you application API keys, streaming, tool use and usage insights on top of your existing Claude access — set up in minutes.

Start free  Read the quickstart →