Send images to Claude using the Anthropic API. Python and Node.js examples for base64 images, URLs, PDFs, and multi-image analysis. Works with Sonnet 4.6 and Haiku 4.5.
Claude's API supports multimodal inputs: you can pass images, PDFs, and text together in a single API call. Sonnet 4.6 and Haiku 4.5 both support vision. Opus 4.7 supports vision as well but is slower — for image-heavy workloads, Sonnet 4.6 is the sweet spot.
import anthropic, base64, pathlib
client = anthropic.Anthropic()
image_data = base64.standard_b64encode(pathlib.Path("diagram.png").read_bytes()).decode("utf-8")
message = client.messages.create(
model="claude-sonnet-4-6",
max_tokens=1024,
messages=[
{
"role": "user",
"content": [
{
"type": "image",
"source": {
"type": "base64",
"media_type": "image/png",
"data": image_data,
},
},
{"type": "text", "text": "Describe this diagram and list all labels."}
],
}
],
)
print(message.content[0].text)
message = client.messages.create(
model="claude-sonnet-4-6",
max_tokens=512,
messages=[
{
"role": "user",
"content": [
{
"type": "image",
"source": {
"type": "url",
"url": "https://example.com/chart.png",
},
},
{"type": "text", "text": "What trend does this chart show?"}
],
}
],
)
print(message.content[0].text)
def encode(path):
return base64.standard_b64encode(pathlib.Path(path).read_bytes()).decode("utf-8")
message = client.messages.create(
model="claude-sonnet-4-6",
max_tokens=1024,
messages=[
{
"role": "user",
"content": [
{"type": "image", "source": {"type": "base64", "media_type": "image/png", "data": encode("before.png")}},
{"type": "image", "source": {"type": "base64", "media_type": "image/png", "data": encode("after.png")}},
{"type": "text", "text": "What changed between image 1 (before) and image 2 (after)?"}
],
}
],
)
print(message.content[0].text)
image/jpeg, image/png, image/gif, image/webptype: "document" with media_type: "application/pdf"import Anthropic from "@anthropic-ai/sdk";
import { readFileSync } from "fs";
const client = new Anthropic();
const imageData = readFileSync("screenshot.png").toString("base64");
const response = await client.messages.create({
model: "claude-sonnet-4-6",
max_tokens: 512,
messages: [
{
role: "user",
content: [
{
type: "image",
source: { type: "base64", media_type: "image/png", data: imageData }
},
{ type: "text", text: "Extract all text visible in this screenshot." }
]
}
]
});
console.log(response.content[0].text);
Images are converted to tokens internally. A 1080×1080 image costs roughly 1,600–2,200 tokens depending on detail level. At Sonnet 4.6 rates ($3/M input tokens) that's ~$0.005 per image — negligible for batch pipelines.
Use the Claude Cost Calculator to model monthly costs for image-heavy workloads. The Prompt-Pricing Recommender can help you choose Haiku vs Sonnet for vision tasks.