Python 88.3%
TypeScript 7.6%
Shell 4.1%
1"""Files API with the Python SDK: generate a minimal 1-page PDF, upload it (client.files.upload), reference it as a2document by file_id with citations enabled (page_location), list with the ids filter, then delete.3STATUS: LIVE_VERIFIED 2026-09-18 (claude-haiku-4-5-20251001): upload 200 (607 bytes, downloadable False),4message 200 with page_location citation, list-by-ids 1 hit, delete type file_deleted. ≈$0.002.5Run: .venv/bin/python examples/anthropic/files/files_pdf_reference.py6"""7from __future__ import annotations89import os10import sys11from pathlib import Path1213sys.path.insert(0, str(Path(__file__).resolve().parents[3]))14from scripts import live # noqa: E4021516import anthropic # noqa: E4021718MODEL = os.environ.get("ANTHROPIC_MODEL", "claude-haiku-4-5-20251001")19client = anthropic.Anthropic(max_retries=1)202122def tiny_pdf(text: str) -> bytes:23 stream = f"BT /F1 18 Tf 40 750 Td ({text}) Tj ET".encode()24 objs = [b"<< /Type /Catalog /Pages 2 0 R >>", b"<< /Type /Pages /Kids [3 0 R] /Count 1 >>",25 b"<< /Type /Page /Parent 2 0 R /MediaBox [0 0 612 792] /Contents 4 0 R /Resources << /Font << /F1 5 0 R >> >> >>",26 b"<< /Length %d >>\nstream\n" % len(stream) + stream + b"\nendstream",27 b"<< /Type /Font /Subtype /Type1 /BaseFont /Helvetica >>"]28 out, offs = b"%PDF-1.4\n", []29 for i, o in enumerate(objs, 1):30 offs.append(len(out)); out += f"{i} 0 obj\n".encode() + o + b"\nendobj\n"31 xref = len(out)32 out += f"xref\n0 {len(objs)+1}\n0000000000 65535 f \n".encode() + b"".join(f"{o:010d} 00000 n \n".encode() for o in offs)33 return out + f"trailer\n<< /Size {len(objs)+1} /Root 1 0 R >>\nstartxref\n{xref}\n%%EOF\n".encode()343536f = client.files.upload(file=("atlas.pdf", tiny_pdf("The sky is blue. Grass is green."), "application/pdf"))37print("uploaded:", f.id, f.mime_type, f.size_bytes, "downloadable=", f.downloadable, "expires_at=", getattr(f, "expires_at", None))38live.log_request("anthropic", "POST", "/v1/files", 200, 0, "example files/files_pdf_reference.py upload")39try:40 msg = client.messages.create(model=MODEL, max_tokens=60, messages=[{"role": "user", "content": [41 {"type": "document", "source": {"type": "file", "file_id": f.id}, "title": "Colors PDF", "citations": {"enabled": True}},42 {"type": "text", "text": "What color is grass? One sentence, cite."}]}])43 cites = [c for b in msg.content if b.type == "text" and b.citations for c in b.citations]44 print("answer:", msg.content[0].text, "| citation:", cites[0].model_dump() if cites else None, "| input_tokens:", msg.usage.input_tokens)45 live.log_request("anthropic", "POST", "/v1/messages", 200, 0.002, f"example files/files_pdf_reference.py message {MODEL}")46 page = client.files.list(ids=[f.id]) if "ids" in client.files.list.__code__.co_varnames else None47 listed = [x.id for x in (page.data if page else client.files.list(limit=5).data)]48 print("list contains upload:", f.id in listed)49 live.log_request("anthropic", "GET", "/v1/files", 200, 0, "example files/files_pdf_reference.py list")50finally:51 deleted = client.files.delete(f.id)52 print("deleted:", deleted.id, deleted.type)53 live.log_request("anthropic", "DELETE", "/v1/files/{file_id}", 200, 0, "example files/files_pdf_reference.py delete")54