How to send multiple images to Claude API
Quick answer
To send multiple images to the Claude API, encode each image as a base64 string and include them as separate messages with the appropriate content type in the
messages parameter. Use the anthropic.Anthropic client with the messages.create method, specifying model="claude-3-5-sonnet-20241022" and passing the images as base64-encoded strings in the message content.PREREQUISITES
Python 3.8+Anthropic API keypip install anthropic>=0.20
Setup
Install the official Anthropic Python SDK and set your API key as an environment variable.
- Install SDK:
pip install anthropic>=0.20 - Set environment variable:
export ANTHROPIC_API_KEY='your_api_key_here'(Linux/macOS) orset ANTHROPIC_API_KEY=your_api_key_here(Windows)
pip install anthropic>=0.20 Step by step
This example shows how to send two images encoded in base64 to the Claude API using the anthropic SDK. Each image is included as a separate message with role: "user" and the content containing the base64 string prefixed by ".
import os
import base64
from anthropic import Anthropic
# Initialize client
client = Anthropic(api_key=os.environ["ANTHROPIC_API_KEY"])
# Helper to load and encode image as base64 string
def encode_image_to_base64(path):
with open(path, "rb") as f:
return base64.b64encode(f.read()).decode("utf-8")
# Encode multiple images
image1_b64 = encode_image_to_base64("./image1.png")
image2_b64 = encode_image_to_base64("./image2.jpg")
# Prepare messages with images as base64 strings
messages = [
{"role": "user", "content": f"<image>{image1_b64}"},
{"role": "user", "content": f"<image>{image2_b64}"}
]
# Call Claude API
response = client.messages.create(
model="claude-3-5-sonnet-20241022",
max_tokens=1024,
system="You are a helpful assistant that can interpret multiple images.",
messages=messages
)
print(response.content[0].text) output
Assistant's response text interpreting the two images
Common variations
- Async usage: Use
asyncioandawait client.messages.acreate(...)for asynchronous calls. - Different models: Swap
model="claude-3-5-sonnet-20241022"with other Claude 3.5 variants likeclaude-3-5-haiku-20241022. - Streaming: Use the
stream=Trueparameter and iterate over the response for real-time output.
import asyncio
from anthropic import Anthropic
async def send_images_async():
client = Anthropic(api_key=os.environ["ANTHROPIC_API_KEY"])
response = await client.messages.acreate(
model="claude-3-5-sonnet-20241022",
max_tokens=1024,
system="You are a helpful assistant.",
messages=[
{"role": "user", "content": f"<image>{encode_image_to_base64('./image1.png')}"},
{"role": "user", "content": f"<image>{encode_image_to_base64('./image2.jpg')}"}
]
)
print(response.content[0].text)
asyncio.run(send_images_async()) output
Assistant's response text interpreting the two images
Troubleshooting
- If you get an authentication error, verify your
ANTHROPIC_API_KEYenvironment variable is set correctly. - If images are not recognized, ensure they are properly base64 encoded and prefixed with
<image>. - For large images, consider resizing or compressing to avoid exceeding token limits.
Key Takeaways
- Encode each image as a base64 string and prefix with
in the message content. - Send multiple images as separate
usermessages in themessagesarray. - Use the
anthropic.Anthropicclient withmessages.createand specify the Claude 3.5 Sonnet model. - Async and streaming calls are supported for advanced use cases.
- Always set your API key securely via environment variables to avoid authentication errors.