How to Send Images to an LLM API: base64 vs URL (OpenAI & Anthropic)
To send an image to an LLM through an API, you put the image in the user message as a content block, next to the text. There are two ways to supply it: a publicly reachable URL, or the file encoded as base64 and embedded directly in the request. Local files and images on an internal network can only go in as base64. If the image already sits on a public image host, a URL is easier. Below are both the OpenAI format and the Anthropic format, which are the two most common ways to upload an image to an LLM API.
The model names in the examples are placeholders. Which models accept image input, how many images per request, and how large they can be: check the provider's official documentation.
How do the two formats differ in structure?
| Item | OpenAI format | Anthropic format |
|---|---|---|
| Endpoint | Chat Completions (/v1/chat/completions) | Messages (/v1/messages) |
| Image block type | image_url | image |
| Passing a URL | put the address in image_url.url | set source.type to url, then add url |
| Passing base64 | put a data URL in url (data:image/png;base64,...) | set source.type to base64, then fill in media_type and data |
| base64 prefix | includes the data: prefix | no prefix: just the encoded string, with the type given separately |
The last row is the one people mix up most. OpenAI wants the whole data URL; Anthropic wants only the raw base64 string, with the media type in its own field.
How do you write it in the OpenAI format?
First, the URL approach. content changes from a string to an array, with the text and the image each taking one entry:
curl https://api.openai.com/v1/chat/completions \
-H "Authorization: Bearer $OPENAI_API_KEY" \
-H "Content-Type: application/json" \
-d '{
"model": "<支持图片输入的模型>",
"messages": [{
"role": "user",
"content": [
{"type": "text", "text": "这张图里有什么?"},
{"type": "image_url", "image_url": {"url": "https://example.com/demo.png"}}
]
}]
}'
Now the base64 approach, in Python:
import base64
from openai import OpenAI
client = OpenAI() # See the links at the end for setting the endpoint URL and key
with open("demo.png", "rb") as f:
b64 = base64.b64encode(f.read()).decode()
resp = client.chat.completions.create(
model="<支持图片输入的模型>",
messages=[{
"role": "user",
"content": [
{"type": "text", "text": "这张图里有什么?"},
{"type": "image_url",
"image_url": {"url": f"data:image/png;base64,{b64}"}},
],
}],
)
print(resp.choices[0].message.content)
The image/png in the data URL must match the real file format. If the file is a JPEG, write image/jpeg. OpenAI also has a Responses API whose image block uses different field names from the ones above; if you use that endpoint, follow the official documentation.
How do you write it in the Anthropic format?
import base64
import anthropic
client = anthropic.Anthropic()
with open("demo.png", "rb") as f:
b64 = base64.standard_b64encode(f.read()).decode()
msg = client.messages.create(
model="<支持图片输入的模型>",
max_tokens=1024,
messages=[{
"role": "user",
"content": [
{"type": "image",
"source": {"type": "base64",
"media_type": "image/png",
"data": b64}},
{"type": "text", "text": "这张图里有什么?"},
],
}],
)
print(msg.content[0].text)
To use a URL instead, replace source with {"type": "url", "url": "https://example.com/demo.png"}. Also, Anthropic's documentation recommends putting images before the text, which is why the image block comes first above.
Should you use a URL or base64?
- Local files, screenshots, user uploads: use base64. It doesn't depend on any external network.
- Image already on the public internet: use a URL, which keeps the request body small. But the provider's servers must be able to reach that address. Images that require a login, sit behind hotlink protection, or are visible only on an internal network can't be fetched by the server and will produce an error.
- Images with private content: with a URL, the model provider has to download the image from your host; with base64, it travels with the request. Either way the provider sees the image, and whether it is retained is governed by that provider's privacy policy.
- base64 inflates the payload by about a third. If you put several images in one request, watch the request body size limits on your gateway and proxies.
Where do you look up size, count, and format limits?
These differ by provider and change over time, so I'm not putting specific numbers here. Here is how to look them up:
- Open the Vision or Images page in the provider's official documentation and find the "Limits" section.
- Note four things: supported formats (PNG, JPEG, WebP, and GIF are common; the documentation is authoritative), the maximum size per image, the maximum number of images per request, and the rules for resizing based on image resolution.
- If you go through a relay or aggregation gateway, the gateway may impose its own request body limit, so check the gateway's documentation as well.
- Before going live, test once with an image close to the limit. Don't rely on the documentation alone.
How are images billed?
Images are converted into input tokens and counted together with the text in your input usage. The conversion rules differ by provider: some split the image into tiles based on its dimensions, others estimate from pixel area, and many have a "detail level" setting or a resize cap. For the exact formula, go by the official documentation.
To check it yourself, send the same request once without the image and note the input tokens in the response's usage. Then send it again with the image. The difference is the cost of that image. Repeat with a smaller image to see how size affects tokens. If your bill doesn't add up, see our post How to check when API billing looks wrong.
How do you troubleshoot common errors?
- 400, with a message about the wrong media type or format: check that
media_typeor the data URL prefix matches the real file format. For Anthropic,datamust not contain thedata:...;base64,prefix. - 400, with a message that the request is too large: the image is too big or there are too many of them. Shrink the images and try again.
- A message that the image can't be downloaded: the URL isn't publicly reachable, or it requires authentication. Switch to base64.
- The model says it "can't see the image": that model may not support image input. Switch to one that does, or confirm in the official model list.
- Images disappear when going through a relay: some gateways forward only text fields. Verify with a direct curl call first, then work through each layer.
What should you know when using an aggregation API like AGI Plan?
If you want to pay in RMB and reach multiple models through a single endpoint, an aggregation gateway is one option. AGI Plan is compatible with both the OpenAI and Anthropic API formats, so in principle both approaches above work; you only need to swap in its endpoint URL and key. A disclosure: AGI Plan is a commercial partner of cdnping. It uses monthly subscriptions, settles in RMB, and doesn't require an overseas credit card, and the service states plainly that it is not an officially authorized channel. Some models are marked "overseas only." Which models support image input, and whether they are available, is determined by agiplan.dev/models and the official notes. For getting started, see agiplan.dev/startup.
What should you do next?
- Use the curl example above with a small image to get the URL approach working, and confirm the model supports images.
- Then switch to base64 to handle local files.
- Measure the token difference with and without an image, so you know the cost before you start making calls in bulk.
For changing the endpoint URL and environment variables, see How to change base_url in the openai Python library and How to change the endpoint URL in the Anthropic Python SDK. To verify the request format the simplest way first, see Your first LLM request with curl. To debug image requests in Postman, see Testing LLM APIs with Postman / Apifox. Image request bodies are large and responses are slow, so you may also want to check connection times to each provider's API with the cdnping online speed test, and for background on latency see Why AI API latency is high.