Claude API Function Calling: Multi-Step Workflow Guide
Multi-step function calling with the Claude API means chaining several tool calls together in a single logical task — Claude calls one tool, reads the result, decides whether it needs another tool, calls that one, and keeps going until it has enough information to answer. This is different from a single tool call, where Claude calls one function and returns a final response immediately. The workflow pattern requires you to manage conversation state across multiple round trips, handle partial failures, and know when to stop looping.
This article walks through how multi-step tool use actually works under the hood, how to structure the request/response loop in code, and the common failure modes that trip people up when they first build one of these pipelines.
How multi-step tool use works
Claude's tool use (function calling) is stateless per request — each API call is a fresh snapshot of the conversation. The "multi-step" part comes entirely from your application loop, not from anything Claude does automatically between calls. The pattern looks like this:
- You send a user message plus a list of available tools.
- Claude responds with either a final text answer, or a
tool_useblock requesting a specific function with specific arguments. - You execute that function in your own code (hit a database, call an internal API, run a calculation).
- You send the result back as a
tool_resultmessage, appended to the same conversation. - Claude reads the result and decides: answer now, or call another tool.
- Repeat steps 2–5 until Claude returns a plain text response with no tool calls.
The key detail is that you own the loop. Claude never executes code directly — it only requests that a function be called with certain arguments, and your backend is responsible for running it and feeding the result back in the exact format Claude expects.
Setting up the tools array
Each tool needs a name, description, and JSON schema for its input. The description matters more than people expect — vague descriptions lead to Claude picking the wrong tool or the wrong step order.
{
"tools": [
{
"name": "lookup_order",
"description": "Fetch order details by order ID, including status and shipping info.",
"input_schema": {
"type": "object",
"properties": {
"order_id": { "type": "string" }
},
"required": ["order_id"]
}
},
{
"name": "cancel_order",
"description": "Cancel an order by ID. Only call this after confirming the order is still pending.",
"input_schema": {
"type": "object",
"properties": {
"order_id": { "type": "string" }
},
"required": ["order_id"]
}
}
]
}
Notice the second tool's description explicitly tells Claude the precondition — that's often enough to get correct multi-step ordering (lookup before cancel) without extra prompt engineering.
Building the loop
Here's a minimal JavaScript loop against SubToAPI's messages endpoint, which exposes the same tool-use request/response shape so you don't have to change your integration code:
async function runAgentLoop(messages, tools) {
while (true) {
const res = await fetch("https://api.subtoapi.app/v1/messages", {
method: "POST",
headers: {
"Authorization": `Bearer ${process.env.SUBTOAPI_KEY}`,
"Content-Type": "application/json"
},
body: JSON.stringify({
model: "claude-sonnet-4-5",
max_tokens: 1024,
messages,
tools
})
});
const data = await res.json();
messages.push({ role: "assistant", content: data.content });
const toolCalls = data.content.filter(b => b.type === "tool_use");
if (toolCalls.length === 0) {
return data.content; // final answer, stop looping
}
const toolResults = [];
for (const call of toolCalls) {
const result = await executeTool(call.name, call.input);
toolResults.push({
type: "tool_result",
tool_use_id: call.id,
content: JSON.stringify(result)
});
}
messages.push({ role: "user", content: toolResults });
}
}
executeTool is your own dispatcher — a switch statement or a map of function name to handler. This is also where you enforce authorization checks, since Claude will happily request cancel_order if it thinks that's the right next step; your code decides whether it's actually allowed to run.
Managing state and avoiding runaway loops
A few things matter once you move past a toy example:
- Cap the number of steps. Add a max iteration count (5–10 is usually plenty) and break out with an error message if Claude keeps calling tools without converging on an answer.
- Keep tool results small. Dumping an entire database row or a huge JSON blob back into the conversation burns tokens fast across multiple steps. Summarize or trim before sending it back.
- Log every step. Multi-step workflows fail silently if you don't record which tool was called, with what arguments, and what came back — this is your only debugging trail when a workflow produces a wrong final answer.
- Handle tool errors explicitly. If a function call fails (bad ID, timeout, permission denied), send that failure back as the tool result content rather than crashing the loop. Claude can often recover by trying a different approach or asking the user for clarification.
If you're building this against SubToAPI, usage metadata is attached to every response so you can track token consumption per step — useful for spotting workflows that loop more than expected before they become a cost problem. Full request/response shapes are in /docs/messages and /docs/tools, and there's a working streaming + tool-use example in /docs/streaming if you want to show intermediate steps to users as they happen rather than waiting for the whole chain to finish.
When to use multi-step vs. a single call
Not every task needs a loop. If you can gather all the required data in one tool call, don't build a multi-step pipeline just because it's possible — it adds latency and failure surface for no benefit. Multi-step workflows earn their complexity when the next action genuinely depends on the result of the previous one: look up a record, then decide whether to update or cancel it; search for a document, then summarize the specific sections found; check inventory, then place an order only if stock is available.
questions
Does Claude automatically chain tool calls without my code looping? No. Every tool call requires your application to execute the function and send the result back in a new request. Claude decides what to call next, but your code drives each round trip.
How many steps can a multi-step workflow have? There's no hard platform limit, but in practice you should cap iterations (5–10) in your own loop to avoid runaway token usage or infinite back-and-forth from an unresolved request.
Can I test multi-step tool use without building a full backend first? Yes — you can prototype the loop with curl or a short script against a hosted API like SubToAPI (see /docs/quickstart) before wiring it into your production service, since the request/response format stays the same either way.