#!/usr/bin/env python3 """stop_sequences with the Python SDK → stop_reason == 'stop_sequence' and msg.stop_sequence == '3'. STATUS: LIVE_VERIFIED 2026-09-18 with anthropic 1.7.0 (.venv). Run: .venv/bin/python examples/anthropic/messages/stop_sequences.py Key comes from .env (loaded by scripts/live.py); never printed. """ import sys from pathlib import Path sys.path.insert(0, str(Path(__file__).resolve().parents[3])) from scripts import live # noqa: E402 (loads .env, gives log_request) from anthropic import Anthropic # noqa: E402 client = Anthropic() MODEL = "claude-haiku-4-5-20251001" msg = client.messages.create(model=MODEL, max_tokens=32, stop_sequences=["3"], system="Output only what is asked.", messages=[{"role": "user", "content": "Count: 1 2 3 4 5"}]) print(repr(msg.content[0].text if msg.content else ""), msg.stop_reason, msg.stop_sequence) assert msg.stop_reason == "stop_sequence" and msg.stop_sequence == "3" live.log_request("anthropic", "POST", "/v1/messages", 200, 0.00005, "example stop_sequences.py")