Sub topic 7
What images and pages cost in tokens and time
This chapter assumes you can call an LLM chat API from Python, you know what a token and a context window are, and you have read the earlier chapters on building image requests and resizing images.
How images become tokens
A vision model does not read pixels directly. The provider splits each image into patches or tiles and turns each one into tokens. Those tokens count against your context window and your bill in the same way as text tokens.
Providers use different formulas, so treat any number here as an example and check the current documentation. Two patterns hold in general:
- Cost grows with image area, not file size. A 200 KB JPEG and a 4 MB PNG of the same pixel dimensions cost about the same.
- Providers cap the dimensions. A larger image is usually scaled down on the server, so you pay for upload time and gain nothing in detail.
Some providers publish an approximation. For example, one widely documented rule estimates tokens as width × height ÷ 750. Under that rule, a 1000×1000 image costs about 1,333 tokens.
def estimate_image_tokens(width, height, pixels_per_token=750):
"""Rough estimate. Replace pixels_per_token with your provider's rule."""
return round(width * height / pixels_per_token)
for size in [(500, 500), (1000, 1000), (1568, 1568)]:
print(size, estimate_image_tokens(*size))What PDFs cost
A natively sent PDF usually costs twice: the provider extracts the text of each page, and it also renders each page as an image. A page of text can therefore cost more than the same text pasted into a prompt. A 30-page report may use tens of thousands of tokens.
| Input | Token cost driver | Latency driver |
|---|---|---|
| One image | Pixel area after any server-side scaling | Upload size, then input processing |
| Several images | Sum of each image | Total upload size |
| Native PDF | Extracted text plus page images | Page count and upload size |
| Extracted text only | Text length | Smallest of the options |
Time costs
Latency has three parts: uploading the request, processing the input tokens, and generating the answer. Base64 adds about 33% to the payload size, which matters on slow connections. Sending a URL removes the upload but adds a fetch by the provider. Large inputs also increase time to first token.
Measuring instead of guessing
The response reports the actual usage. Log it for every request so you can compare variants.
import time
def timed_call(client, **kwargs):
start = time.perf_counter()
message = client.messages.create(**kwargs)
elapsed = time.perf_counter() - start
usage = message.usage
print(f"input={usage.input_tokens} output={usage.output_tokens} time={elapsed:.1f}s")
return messageWays to reduce cost
- Downscale to your provider's recommended maximum side before sending.
- Crop to the region that matters, such as one table instead of a full page.
- Send only the pages you need from a PDF.
- Use text extraction for documents that are plain text, and keep vision for pages with charts or scans.
- Reuse uploaded files or prompt caching where your provider supports them.
Exercise
A 1200×900 image is sent with a provider that estimates tokens as width × height ÷ 750. About how many tokens does it cost, and does converting it from PNG to JPEG change that number?
Show answer
1200 × 900 = 1,080,000 pixels. Divided by 750, that is about 1,440 tokens. Converting the format does not change it, because cost depends on pixel dimensions, not file size or format.