The Files API in Claude is a game-changer for anyone working with large documents. Instead of sending the same PDF, contract, or report with every API call, you upload it once, get a file ID, and reuse it across unlimited requests. In this guide, we'll walk through how to use the Files API, best practices, and how it can cut your API costs dramatically.
What Is the Files API and Why Should You Care?
Imagine analyzing the same 50-page technical specification 100 times. Without the Files API, you'd send all 50 pages with every request. With Files API, you upload once and reference it by ID in subsequent calls. The result? Massive savings in tokens and API costs.
Beyond cost, here's what makes Files API valuable:
- Reduced token consumption: No need to resend large documents repeatedly
- Faster processing: Files are cached on Anthropic servers, speeding up subsequent requests
- Cleaner API calls: Simple file ID references keep your code readable
- Flexible document types: Works with PDFs, images, text files, Office documents, and more
Whether you're building a document intelligence app, processing logs, or analyzing contracts, Files API is essential for production systems.
Understanding Files API Fundamentals
What Is a File ID?
When you upload a document through the Files API, Anthropic assigns a unique file_id. You'll use this ID to reference the file in future requests.
Example file ID: file_xyz789abc123
Supported File Types
Files API accepts a wide variety of formats:
- Documents: PDF, text, markdown
- Structured data: JSON, CSV, JSONL, XML
- Office files: DOCX, XLSX, PPTX (text extraction only)
- Images: JPG, JPEG, PNG, GIF, WebP
- Archives: ZIP, TAR.GZ (hierarchical text extraction)
Each file can be up to 20 MB in size.
Storage, Retention, and Lifecycle
Files remain on Anthropic's servers for up to 30 days from the last access. After that, they're automatically deleted. There's no storage quota per account, but you'll need to re-upload files if you need them beyond 30 days. Accessing a file resets its 30-day timer.
Getting Started: Installation and Setup
Install the SDK
pip install anthropic python-dotenvConfigure Your API Key
Use environment variables to keep your API key secure:
export ANTHROPIC_API_KEY='your-actual-key'Or create a .env file:
ANTHROPIC_API_KEY=your-actual-key
Then load it in Python:
from dotenv import load_dotenv
import os
load_dotenv()
api_key = os.getenv('ANTHROPIC_API_KEY')Hands-On: Implementing the Files API
Step 1: Upload a File
import os
from anthropic import Anthropic
# Initialize the client
client = Anthropic()
# Upload a file
with open('quarterly_report.pdf', 'rb') as f:
response = client.beta.files.create(
file=(f.name, f, 'application/pdf')
)
file_id = response.id
print(f"File uploaded successfully!")
print(f"File ID: {file_id}")
print(f"Filename: {response.filename}")
print(f"Size: {response.size_bytes} bytes")Expected output:
File uploaded successfully!
File ID: file_xyz789abc123
Filename: quarterly_report.pdf
Size: 245680 bytes
Step 2: Reference the File in API Requests
Once uploaded, use the file ID to include the document in your message:
file_id = "file_xyz789abc123" # from Step 1
message = client.beta.messages.create(
model="claude-opus-4-1",
max_tokens=1024,
messages=[
{
"role": "user",
"content": [
{
"type": "text",
"text": "Please summarize the key findings in this report. Focus on revenue trends and risk factors."
},
{
"type": "document",
"source": {
"type": "file",
"file_id": file_id
}
}
]
}
]
)
print(message.content[0].text)Expected output:
Key Findings Summary:
Revenue Trends:
- Q1 revenue increased 12% year-over-year...
Risk Factors:
- Market volatility in key regions...
- Supply chain dependencies...
Step 3: List All Uploaded Files
# Retrieve all files you've uploaded
files = client.beta.files.list()
print(f"Total uploaded files: {len(files.data)}")
for file in files.data:
print(f"- {file.filename} | ID: {file.id}")
print(f" Size: {file.size_bytes} bytes")
print(f" Created: {file.created_at}")Step 4: Delete a File
# Clean up: delete a file you no longer need
delete_response = client.beta.files.delete(file_id="file_xyz789abc123")
print(f"File deleted: {delete_response.deleted}")Real-World Cost Optimization Example
Scenario: Analyzing the Same Document 100 Times
Without Files API
- Document size: 100 KB (approximately 25,000 tokens)
- Number of requests: 100
- Total tokens sent: 2,500,000 tokens
With Files API
- Initial upload: 25,000 tokens (one time)
- Each subsequent request: ~5,000 reference tokens × 100 requests
- Total tokens: 525,000 tokens
Cost savings: Approximately 79%
This compounds quickly. If you're processing documents daily, the savings add up to thousands of dollars per month.
Common Errors and Solutions
File Size Exceeds Limit
Error: File exceeds maximum size of 20MB
Solution:
- Split large files into smaller chunks
- For PDFs, remove unnecessary pages or compress images
- For images, reduce resolution before uploading
File Not Found (Expired)
Error: File not found or has expired
Solution:
- Files older than 30 days are deleted automatically
- For long-term storage, re-upload periodically
- Consider storing files in your own S3 bucket for permanent access
Authentication Failure
Error: Invalid API key or insufficient permissions
Solution:
- Verify your API key is correct and current
- Check that your account has Files API access (requires paid plan)
- Regenerate your key in the Anthropic console if needed
Wrapping Up
The Files API is essential for building cost-effective, production-grade applications that work with documents. Whether you're building a compliance checker, log analyzer, or document summarizer, Files API saves you tokens, time, and money.
Common use cases:
- Recurring contract and document review
- Continuous log and error analysis
- Compliance document screening
- Internal knowledge base summarization
Start with the API Quickstart Guide to understand the basics, then layer in Files API for document persistence. For even more cost savings, combine Files API with Automatic Caching to cache frequently accessed content.
Ready to scale? Explore Batch Processing for asynchronous, large-scale document workflows.
For deeper Python file handling knowledge, Fluent Python by Luciano Ramalho is an excellent reference.