Sub topic 12
Uploading Files Once and Reusing Them
This chapter assumes you can already build an image or PDF request with base64 data, as covered in earlier chapters. It shows how file upload APIs let you send a file once and refer to it by ID afterward.
Why reuse matters
Base64 inline data has two costs. The encoded text is about a third larger than the raw bytes, and you resend it on every request. That adds up when you ask many questions about the same PDF, or run several prompts over the same set of images.
A file upload API changes the flow. You upload the file once and receive a file ID. Later requests carry that ID in a content block instead of the data.
What you gain and what you give up
Reuse reduces upload bandwidth and request size. It also keeps the large payload out of your own logs. Two things stay the same. The model still reads the file's content on each request, so you still pay the token cost of the image or pages every time. Uploading saves transfer, not tokens, unless your provider offers prompt caching for the content.
You also take on file lifecycle work. Stored files have retention rules and storage limits. You must delete files you no longer need, especially sensitive ones.
The pattern
Providers differ in names, endpoints, and which file types a content block accepts. Some APIs are in beta and need a special header or SDK option. Check your provider's documentation for the current shape. The pattern below uses the OpenAI Python SDK, which uploads with a purpose and then references the ID.
from openai import OpenAI
client = OpenAI() # reads OPENAI_API_KEY from the environment
with open("report.pdf", "rb") as f:
uploaded = client.files.create(file=f, purpose="user_data")
print("Uploaded:", uploaded.id)
questions = [
"Summarize the key findings in this report.",
"List every date mentioned in this report.",
]
try:
for question in questions:
response = client.responses.create(
model="MODEL_NAME", # replace with a model that supports PDF input
input=[
{
"role": "user",
"content": [
{"type": "input_file", "file_id": uploaded.id},
{"type": "input_text", "text": question},
],
}
],
)
print(response.output_text)
finally:
client.files.delete(uploaded.id) # remove the stored copy when doneThe finally block deletes the file even if a request fails. Without it, a failed run leaves the document stored.
Choosing between sources
| Source | Best for | Main drawback |
|---|---|---|
| Base64 inline | One-off requests, small files | Resent and enlarged by about a third each time |
| URL | Files already hosted | The provider must be able to fetch it |
| Uploaded file | Many questions about one file | You manage storage and deletion |
Common mistakes
- Assuming an ID works across providers or accounts. A file ID is valid only for the account that uploaded it.
- Treating an ID as permanent. Files can expire or be deleted, so handle a "file not found" error by uploading again.
- Expecting lower token cost. The model still processes the content.
- Forgetting to delete sensitive documents.
Exercise
You ask 20 different questions about the same 40-page PDF. Does uploading it once reduce the token cost of those requests? What does it reduce?
Show answer
Usually not the token cost, because the model still reads the PDF content on each request. It reduces upload bandwidth and request size, since you send a short file ID instead of the full base64 data 20 times. Prompt caching, if your provider supports it, is what reduces repeated token cost.