CCAPrep
← All labs
D1·Fill in the blanks

Completing the agentic loop

Fill in the stop-reason handling for a manual tool-use loop that also uses a server-side tool, so it terminates correctly instead of spinning or exiting early.

while True:
    response = client.messages.create(
        model="claude-opus-5", max_tokens=16000, tools=tools, messages=messages
    )

    if response.stop_reason == "▁▁▁ 1 ▁▁▁":
        break

    if response.stop_reason == "▁▁▁ 2 ▁▁▁":
        messages.append({"role": "assistant", "content": response.content})
        continue

    messages.append({"role": "assistant", "content": response.content})
    messages.append({"role": "user", "content": collect_results(response)})

1.Blank 1 — which stop_reason means Claude is finished?

2.Blank 2 — a server-side tool hit its internal iteration cap. Which stop_reason, and why re-send with no new user message?