#!/usr/bin/env python3 """Multi-turn conversation with the Python SDK (the history is replayed on every call; the API is stateless). STATUS: LIVE_VERIFIED 2026-09-18 with anthropic 1.7.0 (.venv). Run: .venv/bin/python examples/anthropic/messages/multi_turn.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" history = [{"role": "user", "content": "Remember the code word: PLUM."}, {"role": "assistant", "content": "Noted."}, {"role": "user", "content": "Reply with only the code word."}] msg = client.messages.create(model=MODEL, max_tokens=16, messages=history) history.append({"role": "assistant", "content": msg.content}) # replay assistant blocks verbatim next turn print(msg.content[0].text, "|", msg.stop_reason) live.log_request("anthropic", "POST", "/v1/messages", 200, 0.00005, "example multi_turn.py")