CLAUDE LABJP
MCP — The July 28 MCP spec release candidate drops the Mcp-Session-Id header and goes stateless, so remote MCP servers no longer need sticky sessionsAPPS — The same release adds MCP Apps for server-rendered UI and a Tasks extension for long-running workMEMORY — The Python 0.116.0, TypeScript 0.110.0, and Go 1.56.0 SDKs now send agent-memory-2026-07-22 on every memory store callSPILL — Output from agent_toolset and MCP tools past 100K characters now spills to a file in the sandbox, with the model receiving a truncated preview it can expandBG — MCP tool calls running past two minutes move to the background automatically, keeping the session usable; tune it with CLAUDE_CODE_MCP_AUTO_BACKGROUND_MSRESUME — Typing /resume in the agent view opens a picker of past sessions and brings your pick back as a background sessionMCP — The July 28 MCP spec release candidate drops the Mcp-Session-Id header and goes stateless, so remote MCP servers no longer need sticky sessionsAPPS — The same release adds MCP Apps for server-rendered UI and a Tasks extension for long-running workMEMORY — The Python 0.116.0, TypeScript 0.110.0, and Go 1.56.0 SDKs now send agent-memory-2026-07-22 on every memory store callSPILL — Output from agent_toolset and MCP tools past 100K characters now spills to a file in the sandbox, with the model receiving a truncated preview it can expandBG — MCP tool calls running past two minutes move to the background automatically, keeping the session usable; tune it with CLAUDE_CODE_MCP_AUTO_BACKGROUND_MSRESUME — Typing /resume in the agent view opens a picker of past sessions and brings your pick back as a background session
Articles/API & SDK
API & SDK/2026-04-04Intermediate

Claude API Files API — Persist Documents and Slash API Costs

Learn how to use Claude API's Files API to persist documents and avoid re-uploading costs. Upload PDFs and long documents once, reuse them across multiple API requests with this practical implementation guide.

Files API2Claude API115document processing2cost optimization13Python17

Re-uploading the same fifty-page specification on every request is the quiet tax of document-heavy Claude integrations. The Files API removes it. You upload the document once, receive a file ID, and reference that ID for as long as you need it. What follows is the upload flow, the reuse patterns that hold up in production, and the token math behind the savings.

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-dotenv

Configure 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.

Share

Thank You for Reading

Claude Lab is ad-free, supported entirely by members like you. We publish practical guides daily with implementation code, benchmarks, and production-ready patterns. If you've found it useful, we'd love to have you on board.

  • Copy-paste ready implementation code
  • New advanced guides published daily
  • $5/mo or $10 for lifetime access
View Membership →

If you found this article helpful, a small tip ($1.50) would mean a lot to us. Your support helps keep this site ad-free and covers server and hosting costs.

Related Articles

API & SDK2026-07-02
Introductory Pricing Has an End Date — Effective-Dated Cost Forecasts for the Sonnet 5 Price Step
Claude Sonnet 5's introductory $2/$10 pricing ends on 2026-08-31 and reverts to $3/$15. A static price map will quietly understate your September forecast by a third. Here is an effective-dated price table and forecast design that absorbs the step.
API & SDK2026-06-25
Reach a Remote MCP Server in a Single API Request: Implementing the Messages API MCP Connector
How to call a remote MCP server's tools using only the Messages API's mcp_servers and mcp_toolset—no local MCP client. Covers allowlist/denylist design, response handling, and the pitfalls to avoid before unattended production use.
API & SDK2026-06-22
Your Claude Files API Storage Is Quietly Filling Up — Dedup With a Content-Hash Ledger and Reap the Orphans
Use the Files API in an automated pipeline and the same file gets uploaded again and again while orphaned files pile up unnoticed. Here is a content-hash dedup ledger plus an orphan GC design, with working code.
📚RECOMMENDED BOOKS
Build a Large Language Model (From Scratch)
Sebastian Raschka
LLM Dev
Prompt Engineering for LLMs
Berryman & Ziegler
Prompting
AI Engineering
Chip Huyen
AI Eng
* Contains affiliate links
See all →