Claude and the Anthropic SDK · Chapter 3 of 21
Stop reasons, max_tokens and what you no longer send
By the end of this chapter you read stop_reason before you trust a reply, know what max_tokens really limits (thinking included), continue a reply that was cut off without a prefill, stop a reply where you choose with stop_sequences, and know which old settings Python, the SDK and the API now refuse. Build it in your browser: a real editor and terminal, AI that explains, and tests that tell you when it works.
- Part: The Messages API
- 4 tasks
- Python
- In the all labs plan
The first 2 chapters of Claude and the Anthropic SDK are free. This one comes with the all labs plan.
Why you need this
Farhan set max_tokens=100 on every request "to keep the bill down". The next morning Kamla found families' answers ending mid-sentence, and some answers were empty. Nothing had failed: every request came back 200, and the code printed whatever text was there. The text of a cut-off answer looks just like a finished one, and a program that never asks why a reply ended cannot tell them apart.
Arjun also has last year's code. It passes temperature=0 "so the answers stay the same", and now it fails before a single request is sent. This chapter is about the fields that end a reply, and the ones you no longer send.
What you build: 4 tasks
Each task has tests. You write the code in the lab's editor and press Check.
- Task 1
stop_reasons.py: read stop_reason first
Define ask(question, max_tokens, stop_sequences=None) -> str: send question to "claude-opus-5-5" with that max_tokens and stop_sequences=stop_sequences or [], and return one line chosen by reply.stop_reason: complete: TEXT (end_turn), cut off after N tokens: TEXT (max_tokens; N is reply.usage.output_tokens), stopped at SEQ: TEXT (stop_sequence; SEQ is json.dumps(reply.stop_sequence)), or other: STOP.
TEXT is the reply's text blocks, joined. The check makes it send requests that end each way.
- Task 2
continue_reply.py: continue a cut reply
Define complete(question) -> str: send it to "claude-opus-5-5" with max_tokens STEP. While the reply stops with "max_tokens", append {"role": "assistant", "content": reply.content} and {"role": "user", "content": CONTINUE} to the same messages and send again (max_tokens STEP), at most MAX_REQUESTS requests in all.
Return the text of every reply, joined in order with nothing between: the whole answer, the same as Claude gives in one go.
- Task 3
room_for_thinking.py: leave room for thinking
Send LALITA with system=SYSTEM to "claude-opus-5-5" once for each max_tokens in SIZES, in order. For each reply print max_tokens M: STOP, thinking T, text C characters (T is reply.usage.output_tokens_details.thinking_tokens, or 0 when output_tokens_details is None; C is the length of the text blocks, joined).
Then print enough: M, the smallest size whose reply ended with end_turn.
- Task 4 · interview question
Interview: old_settings.py
"Last year's code sets temperature=0 and now fails. Why, and what else is refused?" Call client.messages.create(**OLD_REQUEST), catch the TypeError and print temperature=0: TypeError. Send it again with temperature in the JSON (extra_body={"temperature": 0}, the rest of OLD_REQUEST as it is), catch the anthropic.BadRequestError and print in the JSON: STATUS TYPE. Send it without temperature and print without it: STOP.
Then ask for max_tokens 64000 without streaming: the SDK raises a ValueError before sending. Print max_tokens 64000: and its message up to the first '.'.
What you will learn
- stop_reason: why a reply ended, and what each value asks your code to do.
- max_tokens: a hard limit on output, thinking included.
- Continuing a cut reply: append, never edit, never prefill.
- stop_sequences: end a reply at text you choose, and stop_sequence in the reply.
- What is gone: temperature, top_p, top_k, and non-streaming requests that could run past ten minutes.
More in The Messages API
Build "Stop reasons, max_tokens and what you no longer send" 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 are free. This one comes with the all labs plan.