handling-stop-reasons changedbuild-with-claude/handling-stop-reasons
Nearest release: v2.1.304, published under an hour 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+27added
Lines−69removed
From line
61
where the diff opens
First seen
14 Aug 2026
this site's first read of the page
Recorded edits19to this page, all time
The whole hunk
from line 61, old and new numbered
/
from line 61
6161 "model": "claude-opus-5-5",
6262 "max_tokens": 1024,
6363 "messages": [{"role": "user", "content": "Hello!"}]
64 }' | jq 'if .stop_reason == "end_turn" then (.content[] | select(.type == "text") | .text) else . end'
64 }'
6565 ```
6666
6767 ```bash CLI
from line 68
6868 ant messages create \
6969 --model claude-opus-5-5 \
7070 --max-tokens 1024 \
71 --message '{role: user, content: "Hello!"}' \
72 --format json | jq 'if .stop_reason == "end_turn" then (.content[] | select(.type == "text") | .text) else . end'
71 --message '{role: user, content: "Hello!"}'
7372 ```
7473
7574 ```python Python
from line 753
754753 "model": "claude-opus-5-5",
755754 "max_tokens": 10,
756755 "messages": [{"role": "user", "content": "Explain quantum physics"}]
757 }' | jq '.stop_reason'
756 }'
758757 ```
759758
760759 ```bash CLI
from line 760
761760 ant messages create \
762761 --model claude-opus-5-5 \
763762 --max-tokens 10 \
764 --message '{role: user, content: "Explain quantum physics"}' \
765 --format json | jq '.stop_reason'
763 --message '{role: user, content: "Explain quantum physics"}'
766764 ```
767765
768766 ```python Python
from line 893
895893<Accordion title="Incomplete tool use blocks">
896894 If Claude's response is cut off because it hit the `max_tokens` limit, and the truncated response contains an incomplete tool use block, you'll need to retry the request with a higher `max_tokens` value to get the full tool use.
897895
898 <CodeGroup exclude="shell:cURL">
899 ```bash CLI
900 RESPONSE=$(ant messages create --max-tokens 1024 --format jsonl <<'YAML'
901 model: claude-opus-5-5
902 tools:
903 - name: get_weather
904 description: Get the current weather in a given location
905 input_schema:
906 type: object
907 properties:
908 location:
909 type: string
910 required:
911 - location
912 messages:
913 - role: user
914 content: What is the weather in San Francisco?
915 YAML
916 )
896 <CodeGroup exclude="shell:cURL, shell:CLI">
897 <CodeGroupItem language="cURL">
898 This workflow needs more logic than a single shell command. Use the [Claude SDK](https://platform.claude.com/docs/en/cli-sdks-libraries/overview) to run it.
899 </CodeGroupItem>
917900
918 # Check if the response was truncated mid tool use
919 STOP_REASON=$(jq -r '.stop_reason' <<<"$RESPONSE")
920 LAST_TYPE=$(jq -r '.content[-1].type' <<<"$RESPONSE")
921 if [ "$STOP_REASON" = "max_tokens" ] && [ "$LAST_TYPE" = "tool_use" ]; then
922 # Retry with a higher max_tokens
923 ant messages create --max-tokens 4096 <<'YAML'
924 model: claude-opus-5-5
925 tools:
926 - name: get_weather
927 description: Get the current weather in a given location
928 input_schema:
929 type: object
930 properties:
931 location:
932 type: string
933 required:
934 - location
935 messages:
936 - role: user
937 content: What is the weather in San Francisco?
938 YAML
939 fi
940 ```
901 <CodeGroupItem language="CLI">
902 This workflow needs more logic than a single shell command. Use the [Claude SDK](https://platform.claude.com/docs/en/cli-sdks-libraries/overview) to run it.
903 </CodeGroupItem>
941904
942905 ```python Python
943906 # Check if response was truncated during tool use
from line 1066
11031066 "max_tokens": 1024,
11041067 "stop_sequences": ["END", "STOP"],
11051068 "messages": [{"role": "user", "content": "Generate text until you say END"}]
1106 }' | jq '{stop_reason, stop_sequence}'
1069 }'
11071070 ```
11081071
11091072 ```bash CLI
from line 1074
11111074 --model claude-opus-5-5 \
11121075 --max-tokens 1024 \
11131076 --stop-sequence END --stop-sequence STOP \
1114 --message '{role: user, content: "Generate text until you say END"}' \
1115 --format json | jq '{stop_reason, stop_sequence}'
1077 --message '{role: user, content: "Generate text until you say END"}'
11161078 ```
11171079
11181080 ```python Python
from line 1218
12561218 }
12571219 }],
12581220 "messages": [{"role": "user", "content": "What is the weather in San Francisco?"}]
1259 }' | jq '.stop_reason, (.content[] | select(.type == "tool_use"))'
1221 }'
12601222 ```
12611223
12621224 ```bash CLI
1263 ant messages create --format json <<'YAML' | jq '.stop_reason, (.content[] | select(.type == "tool_use"))'
1225 ant messages create <<'YAML'
12641226 model: claude-opus-5-5
12651227 max_tokens: 1024
12661228 messages:
from line 1553
15911553 "max_tokens": 4096,
15921554 "tools": [{"type": "web_search_20250305", "name": "web_search"}],
15931555 "messages": [{"role": "user", "content": "Search for latest AI news"}]
1594 }' | jq '{stop_reason, content}'
1556 }'
15951557 ```
15961558
15971559 ```bash CLI
15981560 # Inspect stop_reason; if it is pause_turn, re-run with the assistant
15991561 # response appended to --message.
1600 ant messages create --format json <<'YAML' | jq '{stop_reason, content}'
1562 ant messages create <<'YAML'
16011563 model: claude-opus-5-5
16021564 max_tokens: 4096
16031565 tools:
from line 1771
18091771 "model": "claude-opus-5-5",
18101772 "max_tokens": 1024,
18111773 "messages": [{"role": "user", "content": "[Unsafe request]"}]
1812 }' | jq '{stop_reason, stop_details}'
1774 }'
18131775 ```
18141776
18151777 ```bash CLI
from line 1778
18161778 ant messages create \
18171779 --model claude-opus-5-5 \
18181780 --max-tokens 1024 \
1819 --message '{role: user, content: "[Unsafe request]"}' \
1820 --format json | jq '{stop_reason, stop_details}'
1781 --message '{role: user, content: "[Unsafe request]"}'
18211782 ```
18221783
18231784 ```python Python
from line 1927
19661927 "model": "claude-opus-5-5",
19671928 "max_tokens": 20000,
19681929 "messages": [{"role": "user", "content": "Large input that uses most of context window..."}]
1969 }' | jq '.stop_reason'
1930 }'
19701931 ```
19711932
19721933 ```bash CLI
from line 1934
19731934 ant messages create \
19741935 --model claude-opus-5-5 \
19751936 --max-tokens 20000 \
1976 --message '{role: user, content: "Large input that uses most of context window..."}' \
1977 --format json | jq '.stop_reason'
1937 --message '{role: user, content: "Large input that uses most of context window..."}'
19781938 ```
19791939
19801940 ```python Python
from line 2613
26532613 ```bash cURL
26542614 # cURL exits non-zero on HTTP errors with --fail-with-body; inspect
26552615 # $? for errors and stop_reason for successful responses.
2656 curl --fail-with-body -sS https://api.anthropic.com/v1/messages \
2616 curl --fail-with-body https://api.anthropic.com/v1/messages \
26572617 -H "x-api-key: $ANTHROPIC_API_KEY" \
26582618 -H "anthropic-version: 2023-06-01" \
26592619 -H "content-type: application/json" \
from line 2621
26612621 "model": "claude-opus-5-5",
26622622 "max_tokens": 1024,
26632623 "messages": [{"role": "user", "content": "Hello!"}]
2664 }' | jq '.stop_reason'
2624 }'
26652625 ```
26662626
26672627 ```bash CLI
from line 2629
26692629 ant messages create \
26702630 --model claude-opus-5-5 \
26712631 --max-tokens 1024 \
2672 --message '{role: user, content: "Hello!"}' \
2673 --format json | jq '.stop_reason'
2632 --message '{role: user, content: "Hello!"}'
26742633 ```
26752634
26762635 ```python Python
from line 2841
28822841
28832842 ```bash CLI
28842843 # stop_reason appears in the message_delta event.
2885 ant messages create --stream --format jsonl \
2844 ant messages create --stream \
28862845 --model claude-opus-5-5 \
28872846 --max-tokens 1024 \
2888 --message '{role: user, content: "Hello!"}' |
2889 jq -c 'select(.type == "message_delta") | .delta.stop_reason'
2847 --message '{role: user, content: "Hello!"}'
28902848 ```
28912849
28922850 ```python Python
No line in this hunk matches that.