How to beginner · 3 min read

How to analyze image with Claude in python

Quick answer
Use the anthropic Python SDK to send an image analysis request to claude-3-5-sonnet-20241022. Upload the image as base64 or URL in the message content and specify the task in the system prompt to get descriptive analysis from Claude.

PREREQUISITES

  • Python 3.8+
  • Anthropic API key
  • pip install anthropic>=0.20

Setup

Install the anthropic Python SDK and set your API key as an environment variable.

bash
pip install anthropic>=0.20

Step by step

Use the Anthropic client to send a message with the image data encoded in base64 or as a URL. The system parameter instructs Claude to analyze the image content.

python
import os
import base64
import anthropic

# Initialize client
client = anthropic.Anthropic(api_key=os.environ["ANTHROPIC_API_KEY"])

# Load and encode image as base64
with open("example.jpg", "rb") as image_file:
    image_base64 = base64.b64encode(image_file.read()).decode("utf-8")

# Prepare prompt with image data
system_prompt = "You are an AI assistant that analyzes images. The user will provide an image in base64 format. Describe the content of the image in detail."
user_message = f"data:image/jpeg;base64,{image_base64}"

# Send request
response = client.messages.create(
    model="claude-3-5-sonnet-20241022",
    max_tokens=1024,
    system=system_prompt,
    messages=[{"role": "user", "content": user_message}]
)

# Print analysis
print(response.content[0].text)
output
A detailed description of the image content printed to the console.

Common variations

  • Use an image URL instead of base64 by sending the URL string in the user message.
  • Adjust max_tokens for longer or shorter responses.
  • Use other Claude models like claude-3-opus-20240229 for different response styles.

Troubleshooting

  • If you get an authentication error, verify your ANTHROPIC_API_KEY environment variable is set correctly.
  • If the response is empty or irrelevant, ensure the image data is correctly base64 encoded and included in the message.
  • Check for rate limits or quota issues on your Anthropic account dashboard.

Key Takeaways

  • Use the Anthropic Python SDK with claude-3-5-sonnet-20241022 for image analysis tasks.
  • Encode images as base64 strings or provide URLs in the user message content.
  • Set clear instructions in the system prompt to guide Claude's image description.
  • Always load API keys from environment variables for security.
  • Adjust max_tokens and model choice based on your analysis needs.
Verified 2026-04 · claude-3-5-sonnet-20241022
Verify ↗