Skip to main content
← CoursesAI Engineering with PythonModule 1 · LLM API FundamentalsStop sequences and stop_reasonpredict13 / 101
💬 Discuss🧪 Playground+75 XP
Task
📝 **Question:** Your call returned `stop_reason="max_tokens"`. What does this mean for the response? 📋 Pick the right answer. 💡 **Hint:** `stop_reason` tells you WHY Claude stopped writing.

Keep going

🔮 Predict the output

Read the code carefully

def simulate_with_stop(chunks, stop_sequences):
    out = ""
    for chunk in chunks:
        out += chunk
        for stop in stop_sequences:
            if stop in out:
                return out.split(stop)[0], "stop_sequence"
    return out, "end_turn"

text, reason = simulate_with_stop(
    chunks=["Here is JSON: ", "<json>", '{"x": 1}', "</json>", " bye"],
    stop_sequences=["</json>"],
)
print(text)
print(reason)

What will the program print? Write here:

📣 Help someone learn PythonShare this lesson with a friend — the first 15 are free, no signup.Tweet

💬 Discussion

Be the first to ask a question or share a tip.
Sign in to join the discussion. Reading is free.
Loading discussion…