Claude and the Anthropic SDK (TypeScript) · 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 the API and the SDK 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
- TypeScript
- In the all labs plan
The first 2 chapters of Claude and the Anthropic SDK (TypeScript) 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 sets temperature: 0 "so the answers stay the same", and every request now fails with a 400. 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.ts: read stop_reason first
Export ask(question, maxTokens, stopSequences = []): send question to 'claude-opus-5-5' with max_tokens: maxTokens and stop_sequences: stopSequences, and return one line chosen by reply.stop_reason: complete: TEXT (end_turn), cut off after N tokens: TEXT (max_tokens; N is usage.output_tokens), stopped at SEQ: TEXT (stop_sequence; SEQ is JSON.stringify(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.ts: continue a cut reply
Export complete(question): 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.ts: 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 usage.output_tokens_details?.thinking_tokens, or 0; 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.ts
"Last year's code sets temperature: 0 and now fails. Why, and what else is refused?" Send OLD_REQUEST, catch the Anthropic.BadRequestError and print temperature: STATUS TYPE. Send it again without temperature and print without it: STOP.
Then ask for max_tokens 64000 without streaming: the SDK throws an Anthropic.AnthropicError that is not an Anthropic.APIError, 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 (TypeScript) are free. This one comes with the all labs plan.