Loading source
Pulling the file list, source metadata, and syntax-aware rendering for this listing.
Source from repo
Build LLM-powered apps with the Anthropic Claude API or SDK across Python, TypeScript, Java, Go, Ruby, C#, and PHP.
Files
Skill
Size
Entrypoint
Format
Open file
Syntax-highlighted preview of this file as included in the skill package.
python/claude-api/files-api.md
1# Files API — Python23The Files API uploads files for use in Messages API requests. Reference files via `file_id` in content blocks, avoiding re-uploads across multiple API calls.45**Beta:** Pass `betas=["files-api-2025-04-14"]` in your API calls (the SDK sets the required header automatically).67## Key Facts89- Maximum file size: 500 MB10- Total storage: 100 GB per organization11- Files persist until deleted12- File operations (upload, list, delete) are free; content used in messages is billed as input tokens13- Not available on Amazon Bedrock or Google Vertex AI1415---1617## Upload a File1819```python20import anthropic2122client = anthropic.Anthropic()2324uploaded = client.beta.files.upload(25file=("report.pdf", open("report.pdf", "rb"), "application/pdf"),26)27print(f"File ID: {uploaded.id}")28print(f"Size: {uploaded.size_bytes} bytes")29```3031---3233## Use a File in Messages3435### PDF / Text Document3637```python38response = client.beta.messages.create(39model="claude-opus-4-7",40max_tokens=16000,41messages=[{42"role": "user",43"content": [44{"type": "text", "text": "Summarize the key findings in this report."},45{46"type": "document",47"source": {"type": "file", "file_id": uploaded.id},48"title": "Q4 Report", # optional49"citations": {"enabled": True} # optional, enables citations50}51]52}],53betas=["files-api-2025-04-14"],54)55for block in response.content:56if block.type == "text":57print(block.text)58```5960### Image6162```python63image_file = client.beta.files.upload(64file=("photo.png", open("photo.png", "rb"), "image/png"),65)6667response = client.beta.messages.create(68model="claude-opus-4-7",69max_tokens=16000,70messages=[{71"role": "user",72"content": [73{"type": "text", "text": "What's in this image?"},74{75"type": "image",76"source": {"type": "file", "file_id": image_file.id}77}78]79}],80betas=["files-api-2025-04-14"],81)82```8384---8586## Manage Files8788### List Files8990```python91files = client.beta.files.list()92for f in files.data:93print(f"{f.id}: {f.filename} ({f.size_bytes} bytes)")94```9596### Get File Metadata9798```python99file_info = client.beta.files.retrieve_metadata("file_011CNha8iCJcU1wXNR6q4V8w")100print(f"Filename: {file_info.filename}")101print(f"MIME type: {file_info.mime_type}")102```103104### Delete a File105106```python107client.beta.files.delete("file_011CNha8iCJcU1wXNR6q4V8w")108```109110### Download a File111112Only files created by the code execution tool or skills can be downloaded (not user-uploaded files).113114```python115file_content = client.beta.files.download("file_011CNha8iCJcU1wXNR6q4V8w")116file_content.write_to_file("output.txt")117```118119---120121## Full End-to-End Example122123Upload a document once, ask multiple questions about it:124125```python126import anthropic127128client = anthropic.Anthropic()129130# 1. Upload once131uploaded = client.beta.files.upload(132file=("contract.pdf", open("contract.pdf", "rb"), "application/pdf"),133)134print(f"Uploaded: {uploaded.id}")135136# 2. Ask multiple questions using the same file_id137questions = [138"What are the key terms and conditions?",139"What is the termination clause?",140"Summarize the payment schedule.",141]142143for question in questions:144response = client.beta.messages.create(145model="claude-opus-4-7",146max_tokens=16000,147messages=[{148"role": "user",149"content": [150{"type": "text", "text": question},151{152"type": "document",153"source": {"type": "file", "file_id": uploaded.id}154}155]156}],157betas=["files-api-2025-04-14"],158)159print(f"\nQ: {question}")160text = next((b.text for b in response.content if b.type == "text"), "")161print(f"A: {text[:200]}")162163# 3. Clean up when done164client.beta.files.delete(uploaded.id)165```166