"""Citations from search_result blocks (RAG) and from a custom-content document, with the Python SDK. search_result → search_result_location {source, title, search_result_index, start/end_block_index}; custom content document → content_block_location. STATUS: LIVE_VERIFIED 2026-09-18 (claude-haiku-4-5-20251001): both citation kinds returned (~$0.0015). Run: .venv/bin/python examples/anthropic/citations/citations_search_results.py """ from __future__ import annotations import os import sys from pathlib import Path sys.path.insert(0, str(Path(__file__).resolve().parents[3])) from scripts import live # noqa: E402 import anthropic # noqa: E402 MODEL = os.environ.get("ANTHROPIC_MODEL", "claude-haiku-4-5-20251001") client = anthropic.Anthropic(max_retries=1) QUESTION = {"type": "text", "text": "What color is grass? Answer in one sentence and cite."} BLOCKS = [{"type": "text", "text": "The sky is blue."}, {"type": "text", "text": "Grass is green."}] def run(label: str, block: dict) -> None: msg = client.messages.create(model=MODEL, max_tokens=100, messages=[{"role": "user", "content": [block, QUESTION]}]) for b in msg.content: if b.type == "text" and b.citations: print(f"{label}: {b.text!r} ->", [c.model_dump() for c in b.citations]) live.log_request("anthropic", "POST", "/v1/messages", 200, 0.0007, f"example citations/citations_search_results.py {label} {MODEL}") run("search_result", {"type": "search_result", "source": "kb://colors/1", "title": "Color facts", "content": BLOCKS, "citations": {"enabled": True}}) run("custom content", {"type": "document", "source": {"type": "content", "content": BLOCKS}, "title": "Colors", "citations": {"enabled": True}})