Sub topic 8
Sending several images and placing the prompt
This chapter assumes you can already send one image in a request, as covered earlier. You should know what a content block is and how to encode a file as base64. Here you will send several images in one message and decide where the text prompt goes.
Several images in one message
A user message holds a list of content blocks. To send more images, add more image blocks to that list. The model reads the blocks in order, so order is part of your input.
Each image costs tokens, and most providers cap the number of images per request. Check your provider's documentation for the current limit.
Label each image
Models do not know which image is "the first one" unless you tell them. A reliable pattern is to put a short text block before each image. The label gives you a name to refer to in your question.
import anthropic
client = anthropic.Anthropic() # reads ANTHROPIC_API_KEY from the environment
def image_block(path):
media_type, data = load_as_base64(path) # helper from the earlier chapter
return {
"type": "image",
"source": {"type": "base64", "media_type": media_type, "data": data},
}
content = [
{"type": "text", "text": "Image 1: sales chart for 2023"},
image_block("sales_2023.png"),
{"type": "text", "text": "Image 2: sales chart for 2024"},
image_block("sales_2024.png"),
{"type": "text", "text": "Compare Image 1 and Image 2. Which quarter changed the most?"},
]
message = client.messages.create(
model="MODEL_NAME", # replace with a vision-capable model from the provider's docs
max_tokens=1024,
messages=[{"role": "user", "content": content}],
)
print(message.content[0].text)Where to put the prompt
Providers often advise placing images before the question text. The model then reads the visual material first and sees the task last. This tends to work better when you send many images or long documents. Treat it as a default, not a rule. Test both orders on your own data, because results vary by model.
The labels from the previous section stay next to their images. Only the final question moves to the end.
flowchart LRThe diagram below shows the recommended layout.
Common errors
- Unlabeled images. The model may mix up which image a statement refers to. Add labels.
- Too many images. You may hit the per-request limit or a size limit on the whole payload. Split the work into batches, or downscale each image first.
- Rising cost. Tokens add up per image. Resize before sending, as covered earlier.
- Mismatched media types. Compute the media type for each file. Do not reuse the type from the first image.
- Questions buried in the middle. A question placed between images can be overlooked. Keep it at the end.
Exercise
You send three screenshots and ask, "What changed between them?" The answer mentions "the second screenshot" but describes the wrong one. What two changes to the request would you try first?
Show answer
Add a text label before each image (for example "Screenshot 1", "Screenshot 2", "Screenshot 3") and place the question after all the images. Then refer to the screenshots by those labels in the question.