Skip to main content
🔒 Preview mode. The first 15 Foundations lessons are free; this one is Pro. Start a 7-day trial to unlock the editor, AI hints and the rest of the curriculum. Card required, cancel any time in Dashboard.Start 7-day trial →
← CoursesAI Engineering with PythonModule 1 · LLM API FundamentalsStop sequences and stop_reasonpredict13 / 105
+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.
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:

💬 Discussion

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