Skip to content
AgenticAIWiki
  1. Learn
  2. Part 1
  3. 3. Context windows, tokens and cost
  4. 3.4

Lesson 3.4 · 4 of 4 · 4 min read · Reviewed 24 Nov 2026

What happens when you hit the limit

By the end, you can

  • Explain what happens when a request exceeds limits.
  • Handle the max_tokens stop reason.

Two limits

Limit What it caps What happens
Context window Input plus output for one request Oversized requests are rejected with an error
max_tokens Output for this request (you set it) Generation stops; stop_reason is max_tokens

Checking stop_reason

Every response says why it stopped:

stop_reason Meaning
end_turn The model finished naturally
max_tokens Hit your output cap
stop_sequence Hit one of your stop sequences
tool_use The model wants to call a tool

Always check it. A truncated JSON object will fail to parse; a truncated answer may drop the most important part.

Fixes

Output cut off:

  • Raise max_tokens if the answer genuinely needs more room.
  • Ask for a shorter format.
  • For very long outputs, split the task into parts (chapter 15).

Input too big:

  • Trim old turns and tool results (chapter 59).
  • Summarise history (chapter 60).
  • Retrieve only relevant passages instead of whole documents (Part 4).

Before you hit it

Count tokensToken counting: Measuring how many tokens a request will use before sending it, for example with a token-counting endpoint. for large requests (3.3), and leave room for the output: input plus max_tokens must fit.

Key points

  • If input plus requested output exceeds the context window, the request fails or must be trimmed.
  • If the reply reaches max_tokens, it stops early and the stop reason says so; the answer may be cut mid-sentence.
  • Handle both: trim or summarise input, raise max_tokens where sensible, and check stop_reason in code.
Hands-on labRuns in the Playground

Catch a reply cut off at max_tokens

Detect a truncated reply from its stop_reason and recover instead of using it.

You will have: A function that checks stop_reason, warns on max_tokens and retries with a higher cap.

Steps

  1. Call the model with a very low max_tokens.
  2. Check stop_reason and print that the answer was cut off.
  3. Retry with a higher cap.
  4. Print whether the final answer is complete.

Done when

  • The first call reports stop_reason=max_tokens.
  • The cut-off answer is never treated as final.
  • The script prints whether the final answer is complete.

Open in the Playground No API key needed.

Starter code (Python)
import anthropic

client = anthropic.Anthropic()

def ask(max_tokens):
    msg = client.messages.create(
        model="claude-sonnet-5-5",
        max_tokens=max_tokens,
        messages=[{"role": "user", "content": "List five tips for writing clear tool descriptions."}],
    )
    text = "".join(b.text for b in msg.content if b.type == "text")
    return text, msg.stop_reason, msg.usage.output_tokens

text, reason, used = ask(12)
print("first try:", reason, used)
# TODO: if reason is "max_tokens", say the answer was cut off and retry with a higher cap
# TODO: print whether the final answer is complete
Show a solution
import anthropic

client = anthropic.Anthropic()

def ask(max_tokens):
    msg = client.messages.create(
        model="claude-sonnet-5-5",
        max_tokens=max_tokens,
        messages=[{"role": "user", "content": "List five tips for writing clear tool descriptions."}],
    )
    text = "".join(b.text for b in msg.content if b.type == "text")
    return text, msg.stop_reason, msg.usage.output_tokens

text, reason, used = ask(12)
print(f"first try: stop_reason={reason}, output tokens={used}")
if reason == "max_tokens":
    print("cut off at max_tokens: the answer is incomplete, so do not use it as is")
    text, reason, used = ask(600)
    print(f"second try with a higher cap: stop_reason={reason}, output tokens={used}")
print("complete:", reason != "max_tokens")

Do it Watch stop reasons in the Playground in the Playground. No API key needed.

Quick checkA response ends mid-sentence and its stop_reason is max_tokens. What happened?
How sure are you?
Pick an answer and how sure you are

Finished the chapter? Take the chapter check.