handling-stop-reasons changedbuild-with-claude/handling-stop-reasons
Nearest release: v2.1.328, published 7 hours after this site recorded the change. Shown because the two are within 24 hours of each other. Nothing here says the release caused the edit.
Recorded here
Lines+73added
Lines−44removed
From line
1,381
where the diff opens
First seen
14 Aug 2026
this site's first read of the page
Recorded edits32to this page, all time
#### When a server tool is still waiting
The whole hunk
from line 1381, old and new numbered
/
from line 1381
13811381 ```
13821382
13831383 ```java Java
1384 void main() {
1385 AnthropicClient client = AnthropicOkHttpClient.fromEnv();
1384 AnthropicClient client = AnthropicOkHttpClient.fromEnv();
13861385
1387 Tool weatherTool = Tool.builder()
1388 .name("get_weather")
1389 .description("Get the current weather in a given location")
1390 .inputSchema(Tool.InputSchema.builder()
1391 .properties(JsonValue.from(Map.of(
1392 "location", Map.of("type", "string", "description", "City and state")
1393 )))
1394 .putAdditionalProperty("required", JsonValue.from(List.of("location")))
1395 .build())
1396 .build();
1386 Tool weatherTool = Tool.builder()
1387 .name("get_weather")
1388 .description("Get the current weather in a given location")
1389 .inputSchema(Tool.InputSchema.builder()
1390 .properties(JsonValue.from(Map.of(
1391 "location", Map.of("type", "string", "description", "City and state")
1392 )))
1393 .putAdditionalProperty("required", JsonValue.from(List.of("location")))
1394 .build())
1395 .build();
13971396
1398 Message response = client.messages().create(
1399 MessageCreateParams.builder()
1400 .model(Model.CLAUDE_OPUS_5_5)
1401 .maxTokens(1024L)
1402 .addTool(weatherTool)
1403 .addUserMessage("What is the weather in San Francisco?")
1404 .build()
1405 );
1397 Message response = client.messages().create(
1398 MessageCreateParams.builder()
1399 .model(Model.CLAUDE_OPUS_5_5)
1400 .maxTokens(1024L)
1401 .addTool(weatherTool)
1402 .addUserMessage("What is the weather in San Francisco?")
1403 .build()
1404 );
14061405
1407 if (response.stopReason().map(StopReason.TOOL_USE::equals).orElse(false)) {
1408 // Extract and run the tool
1409 for (ContentBlock block : response.content()) {
1410 block.toolUse().ifPresent(toolUse -> {
1411 // Run toolUse.name() with toolUse.input() and return the result to Claude
1412 });
1413 }
1406 if (response.stopReason().map(StopReason.TOOL_USE::equals).orElse(false)) {
1407 // Extract and run the tool
1408 for (ContentBlock block : response.content()) {
1409 block.toolUse().ifPresent(toolUse -> {
1410 // Run toolUse.name() with toolUse.input() and return the result to Claude
1411 });
14141412 }
1413 }
14151414 ```
14161415
14171416 ```php PHP
from line 1477
14781477 ```
14791478</CodeGroup>
14801479
1481A `tool_use` response can also contain a `server_tool_use` block whose `id` has no matching result block. That server tool call is not complete, and this response does not carry its result. In the common case, Claude calls a [server tool](https://platform.claude.com/docs/en/agents-and-tools/tool-use/server-tools) and one of your client tools in the same group of parallel tool calls: the API returns without running the server tool so that you can run the client tools first. There is no other marker for the state; detect it by checking each `server_tool_use` or `mcp_tool_use` block’s `id` for a matching result block.
1480#### When a server tool is still waiting
14821481
1482Claude can call a [server tool](https://platform.claude.com/docs/en/agents-and-tools/tool-use/server-tools) and a client tool (a tool that your code runs) in the same group of [parallel tool calls](https://platform.claude.com/docs/en/agents-and-tools/tool-use/parallel-tool-use). The API then returns a `tool_use` response without running the server tool so that you can run the client tools first.
1483
1484The waiting call is a `server_tool_use` or `mcp_tool_use` block with no result block. Nothing else in the response marks it. After a server tool runs, its result block, such as `web_search_tool_result`, holds the call’s `id` in its `tool_use_id` field.
1485
14831486<Note>
14841487 With [programmatic tool calling](https://platform.claude.com/docs/en/agents-and-tools/tool-use/programmatic-tool-calling), the same response shape means something different. The client `tool_use` block comes from code that is running in the `code_execution` tool rather than from Claude directly, and its `caller` field names the `code_execution` block that called it. That code has already started: it is paused waiting for your `tool_result` blocks, and sending them resumes the execution instead of starting a deferred tool. The `code_execution` block’s own result block arrives once the code completes, which can take more than one round of tool results. The follow-up user message itself is the same in both cases; with programmatic tool calling, also pass back the `id` from the response’s `container` field, as that page shows.
14851488</Note>
14861489
1490This mixed response has a waiting server tool and a client tool call:
1491
14871492```json A mixed tool_use response
14881493{
14891494 "stop_reason": "tool_use",
from line 1509
15041509}
15051510```
15061511
1507The continuation is a user message of `tool_result` blocks, one for every `tool_use` block in the response (see [Handle tool calls](https://platform.claude.com/docs/en/agents-and-tools/tool-use/handle-tool-calls)), with two extra rules: that message must contain nothing except the `tool_result` blocks, and the request must keep the same `tools` array. A resume request that no longer defines the waiting server tool fails with a 400 whose message ends ``but no `web_search` tool was provided``. The API attaches your results to the still-open assistant turn, runs the deferred server tool (for paused code execution, resumes it), and continues the turn. For a server tool Claude called directly, the next response’s `content` starts with the result block that answers the previous response’s `server_tool_use` `id`.
1512To continue from a mixed response like this one, build a follow-up request:
15081513
15141. Keep the earlier messages and add the response’s `content` unchanged as an `assistant` message.
15152. Add a user message that holds only `tool_result` blocks, one for each client `tool_use` block. A waiting call gets none.
15163. Send the request with the same `tools` array as before so that the waiting server tool is still defined.
1517
1518For the fields of a `tool_result` block, see [Handle tool calls](https://platform.claude.com/docs/en/agents-and-tools/tool-use/handle-tool-calls). The user message in the follow-up request looks like this:
1519
15091520```json The follow-up user message
15101521{
15111522 "role": "user",
from line 1530
15191530}
15201531```
15211532
1522Adding anything after the `tool_result` blocks in that user message, such as text, ends the assistant turn; for a server tool Claude called directly, the request then fails with a 400 `invalid_request_error` that names the unresolved server tool:
1533The API adds your results to Claude’s unfinished turn, runs the waiting server tool, and continues. For a server tool that Claude called directly, the next response’s `content` starts with that tool’s result block.
15231534
1524```text wrap
1525`web_search` tool use with id `srvtoolu_01HxbWnMRmbWyMfUtJKC45rA` was found without a corresponding `web_search_tool_result` block
1526```
1535A loop that follows the preceding steps for every `tool_use` response doesn’t need to detect a waiting server tool. On later requests, keep three separate messages in `messages`, in this order: the first response, your `tool_result` message, and the next response.
15271536
1528Leaving out a `tool_result`, or putting one after other content, fails earlier with the standard `tool_use ids were found without tool_result blocks immediately after` error instead. To give Claude more input, send it as a separate user message after the turn completes.
1537Two cases need one more field in the follow-up request:
15291538
1539* **The response includes a `container`:** Send its ID back (see the code execution tool’s [Container reuse](https://platform.claude.com/docs/en/agents-and-tools/tool-use/code-execution-tool#container-reuse) section).
1540* **The waiting block is an [`mcp_tool_use` block](https://platform.claude.com/docs/en/agents-and-tools/mcp-connector#mcp-tool-use-block):** Send the same `mcp_servers` as before.
1541
1542Each of these mistakes in the follow-up request fails with a 400 `invalid_request_error`. The messages shown are for web search. For other server tools, the names in them differ.
1543
1544| Mistake | The error message includes |
1545| ------------------------------------------------------------------------------------------ | ------------------------------------------------------------------------------ |
1546| `tools` no longer defines the waiting server tool | ``but no `web_search` tool was provided`` |
1547| Text or other content comes after the `tool_result` blocks | ``was found without a corresponding `web_search_tool_result` block`` |
1548| With programmatic tool calling, text or other content comes after the `tool_result` blocks | `only tool_result blocks are allowed` |
1549| Text or other content comes before the `tool_result` blocks | `` `tool_use` ids were found without `tool_result` blocks immediately after `` |
1550| A `tool_result` block is missing | `` `tool_use` ids were found without `tool_result` blocks immediately after `` |
1551
1552Content after the `tool_result` blocks fails because it ends Claude’s turn while the server tool is still waiting. To give Claude more input, continue until a response’s `stop_reason` is neither `tool_use` nor `pause_turn`. Then, send the input as a new user message in the same `messages` list.
1553
15301554### pause\_turn
15311555
15321556Returned when the server-side sampling loop reaches its iteration limit while running [server tools](https://platform.claude.com/docs/en/agents-and-tools/tool-use/server-tools) such as web search. The default limit is 10 iterations for each request.
15331557
1534When this occurs, the response might contain a `server_tool_use` block without a corresponding result block. To let Claude finish processing, continue the conversation by sending the response back as-is. A response that leaves a client `tool_use` block waiting on you never has a `stop_reason` of `pause_turn`: when Claude stops to call your tools, `stop_reason` is [`tool_use`](https://platform.claude.com/docs/en/build-with-claude/handling-stop-reasons#tool-use), and you continue it by sending the client `tool_result` blocks instead of the response itself.
1558When Claude stops to call a tool that you run, the stop reason is [`tool_use`](https://platform.claude.com/docs/en/build-with-claude/handling-stop-reasons#tool-use), never `pause_turn`.
15351559
1560To let Claude complete a paused turn, build a new request:
1561
15621. Keep the earlier messages and add the response’s `content` unchanged as an `assistant` message, including any `server_tool_use` block that has no result block.
15632. Send the request with the same `tools` as before and no user message after the `assistant` message.
1564
1565If the response includes a `container`, also send its ID back so that the turn keeps its files. For details, see the code execution tool’s [Container reuse](https://platform.claude.com/docs/en/agents-and-tools/tool-use/code-execution-tool#container-reuse) section. The following examples use web search and send no container ID:
1566
15361567<CodeGroup>
15371568 ```bash cURL
15381569 # The SDKs handle continuation directly. With cURL, inspect stop_reason
from line 1776
17451776 ```
17461777</CodeGroup>
17471778
1748<Note>
1749 Your application should handle `pause_turn` in any agent loop that uses server tools. Add the assistant’s response to your messages array and make another API request to let Claude continue.
1750</Note>
1779Your application should handle `pause_turn` in any agent loop that uses server tools. For a loop that handles repeated pauses and sends the container ID back, see [Implement retry logic for pause\_turn](https://platform.claude.com/docs/en/build-with-claude/handling-stop-reasons#implement-retry-logic-for-pause-turn).
17511780
17521781### refusal
17531782
No line in this hunk matches that.