30.3 Images, documents and the Files API
- Approved by
- Panaversity ·
In everyday life. A clerk who receives the same contract every week does not photocopy it each time. She files it once and says "the contract in drawer 4". But "drawer 4" means something only in her own office. Say it to the branch office across town, and they open their own drawer 4 and find nothing there. Sending a file once and naming it after that is what the Files API does. And the name works only in the place where the file was filed.
What you will learn. You practice this in Lab 30, Tasks 1 to 3.
- How does Claude read an image or a PDF?
- What does uploading once give you?
- Which faults come from the file, not the model?
- Who can read a file?
- Which file ids work in which environment?
Why it matters. Many of Brightline's small vendors still send paper invoices, and Ana photographs them on her phone. An application that reads only typed text cannot read them. And a file sent once is named by an id that only one place can read. So the id that works on your laptop can fail in production.
At work. It is Monday, May 17, 2027. The photo and the scan work on your laptop, by their ids. Nobody has said which workspace their ids belong to in production.
How does Claude read an image or a PDF?
In a content block that holds the file. A message can hold more than text. An image block holds a picture. A document block holds a PDF, which Claude reads as the text of each page plus an image of each page.1
Claude reads four image types: JPEG, PNG, GIF and WebP.2 A HEIC photo is not one of them. A document block also takes plain text, such as a .csv file, but it refuses Word and Excel files, which must be converted first.1
A media type is the label that says what kind of file the bytes are, such as image/jpeg or application/pdf. Each block takes its content in one of three ways:21
| Way | What you send | Fits when |
|---|---|---|
| Inline | The file's bytes, encoded as text (base64), with its media type | A small file, used once |
| A web address | A URL the API fetches | A public file. Never a private invoice |
| A file id | The id the Files API gave you when you uploaded the file | A file used in many requests, or by many steps |
Figure 30.3. Three ways in, and the workspace line. A file id belongs to the workspace that uploaded it, so the production key gets a 404 for a staging id.
What does uploading once give you?
An id you name in every later request, instead of the bytes. You upload the file, get back an id, and put the id in the image or document block. You send the bytes once, not on every turn of the loop. The Files API is out of beta and needs no beta header, and both SDKs call it as client.files.3
The file is not free each time you use it. File operations are free, but "file content used in Messages requests is priced as input tokens."3 So an id makes each request smaller to send. It does not make the model read less.
A file stays until you delete it, or until an expiry you set when you upload it. 30.2 says what that means for what is kept.
Which faults come from the file, not the model?
Each one in this table. The fix is always to change the request. The SDK does not retry a 400, a 404 or a 413, so those reach your code at once.4
| What you see | Cause | Fix |
|---|---|---|
| An error for a type the API does not support | A HEIC photo labeled as JPEG, or a Word file sent as a PDF | Check the media type before you send. Convert the file to a supported type, and send the type that matches the bytes |
| 404 for a file id | The file was deleted, or it was uploaded with another workspace's key3 | Upload it with this environment's key, and store the id with the workspace it belongs to |
| The request fails before the model runs | The file's expiry has passed3 | Upload it again, with an expiry that lasts longer than the invoice is in use |
413 request_too_large, or an error about pages | The request is over 128 MB, a file is over 500 MB, or a PDF has more pages than the limit431 | Split the PDF, shrink the image, or send fewer files in one request |
| 400 that names the content block | A file id sent in the wrong kind of block, such as a PDF's id in an image block3 | Send an image's id in an image block, and a PDF's or a text file's id in a document block |
| 400 when you download a file | You asked for the content of a file you uploaded. Only files made by skills or code execution can be downloaded3 | Keep your own copy of what you upload |
| A good-looking answer with wrong amounts | A blurred or tiny scan, read without complaint2 | Not an API fault. Check the amounts against the purchase order, and send unreadable scans to a person |
Each of the first six is a fault in what was sent, not in the model. Most are integration faults (21.9), which means your code sent the wrong thing. The 404 for another workspace's id is a fault in configuration, the cell this section's last part fills. Retrying sends the same request again, and fails the same way. The last row is not an error at all. So a scan Claude misread is a case for Chapter 24's suite, not for an error handler.
The cheapest place to catch a bad type is before any request. The worker's media-type check is a few lines of its own code. They read each file's type and refuse anything but the four image types and PDF. It refuses [INV-9103] on the laptop, and a test proves it.
Who can read a file?
Any key for the workspace that uploaded it. "Files are not scoped to an end user, conversation, or session. Any API key with access to a workspace can access any files uploaded to that workspace."3 So never accept a file id from a user or from a document. Keep your own table of which company and which person uploaded each file, and check it on every request before the id reaches Claude.
Brightline's worker keeps that table in data/file-ids.json: each id with its workspace and its company. Brightline's portal also serves Cedar Ridge Supply. Suppose a Cedar Ridge file id reaches the worker during a Brightline question. The worker's own table refuses it before any request. In Lab 30, Task 3, a test sends that id, and a second test proves the first one fails if the check is removed.
If each company must be kept fully apart, give each its own workspace. Anthropic calls the workspace "the isolation boundary for files".3 Delete a file when its invoice is closed, or set an expiry when you upload it. An expiry sets how long the file lives. It is not proof that the content is gone.3
Which file ids work in which environment?
Only the ids uploaded in the workspace that environment's key reads. This is the third row of docs/environments.md. Each environment's credential reads one workspace, and staging and production each have one (23.4).
Your laptop uses the staging key in .secrets/anthropic.env, so development and staging share the staging workspace. Production has its own. "The portal's production key never sits on your computer" (Chapter 23's reference). So an id you uploaded on the laptop answers 404 in production.
The trap is in the kit. The portal's morning job reads stored ids from data/file-ids.json. Unless production uploads its own files, it would reuse staging's ids, and every one would fail.
| Row | Development: the laptop | Staging: CI and the staging workspace | Production: the portal |
|---|---|---|---|
| 3 Which workspace the file ids belong to | Staging, through the key in .secrets/anthropic.env. Proof: data/file-ids.json[*].workspace | Same. CI's live job: [its credential] | Production, through the portal's production key. Proof: config/prompts.yaml#production.workspace is production, and the ingestion reads only ids whose workspace matches it |
This is the cell Tuesday's check failed on (the point). You never record production's 404 yourself, because you never hold the production key.
Amazon Bedrock and Claude Models on Google Cloud (Google managed) have no Files API, and take images and PDFs as base64 only.256 If one environment ran there, its row-3 cell would say "inline only", and code that sends file ids would fail there. 30.8 lists each platform.
The operating rule: send a file once and reuse its id, and check the media type before you send. Treat a file error as yours to fix. Never take a file id from outside your own table, and store each id with the workspace that can read it.
Do this now. Ask your coding agent to find every file this project sends to Claude. Copy this line:
Find every place this project sends Claude an image or a PDF. For each one, say whether it sends the bytes, a web address or a file id, and what checks the file's type before it is sent. Then, for each file id the project stores, say which workspace it belongs to, which company it belongs to, and which environment's key can read it. Name any environment that would use an id it cannot read. Change no file.
Read next. 30.4 sorts every error by its class, and decides which ones may go to another model.
Check yourself
Question 1 / 6
0 answered
The worker sends Claude a phone photo saved as HEIC but labeled as JPEG, and the request fails. What is the right correction?
Sources
- Owner
- Panaversity