C.W.K.
Stream
Lesson 01 of 05 · published

Sending Images

~22 min · vision, images, input

Level 0Tokenizer
0 XP0/54 lessons0/10 achievements
0/120 XP to next level120 XP to go0% complete

Vision-capable models can analyze images sent via URL or base64 encoding. The Responses API uses input_image type, while Chat Completions uses image_url.

Two transports, one cost

Vision pricing is computed from the encoded image (dimensions × detail tiles) — not from how the bytes reached the API. URL is convenient when the image is already on a public host (S3, R2, your CDN); base64 is convenient when the image is private or generated locally. They cost the same.

For local development, base64 is the path of least resistance. For production with public CDN, URL keeps your request bodies small. Either way, the model sees the same pixels.

Code

Image via URL (Responses)·python
response = client.responses.create(
    model="gpt-4.1",
    input=[{
        "role": "user",
        "content": [
            {"type": "input_text", "text": "What is in this image?"},
            {
                "type": "input_image",
                "image_url": "https://example.com/photo.jpg",
                "detail": "high",  # "low", "high", "auto"
            },
        ],
    }],
)
Image via base64 (Responses)·python
import base64

def encode_image(path: str) -> str:
    with open(path, "rb") as f:
        return base64.b64encode(f.read()).decode("utf-8")

b64 = encode_image("photo.jpg")

response = client.responses.create(
    model="gpt-4.1",
    input=[{
        "role": "user",
        "content": [
            {"type": "input_text", "text": "Describe this image in detail."},
            {"type": "input_image", "image_url": f"data:image/jpeg;base64,{b64}", "detail": "high"},
        ],
    }],
)
Image (Chat Completions shape)·python
completion = client.chat.completions.create(
    model="gpt-4o",
    messages=[{
        "role": "user",
        "content": [
            {"type": "text", "text": "What's in this image?"},
            {"type": "image_url", "image_url": {"url": "https://example.com/image.jpg", "detail": "auto"}},
        ],
    }],
)

External links

Exercise

Send the same image both as a URL and as base64. Compare token usage to confirm they match. Then send a 3MB image at detail=low vs detail=high; compare costs.

Progress

Progress is local-only — sign in to sync across devices.
Spotted a bug or have feedback on this page?Report an Issue

Comments 0

🔔 Reply notifications (sign in)
Sign inPlease sign in to comment.

No comments yet — be the first.