Skip to content

Claude Model Image Recognition

Claude provides multiple models that support image recognition (Vision). This document uses claude-sonnet-4-5-20250929 as an example to demonstrate how to perform image recognition through the Anthropic Messages API.

Basic Configuration

Before using the API, please make sure you have obtained an API Key. If not, please refer to Create API Key.

Basic Information

  • API Base URL: https://api.agentsflare.com/anthropic/v1/messages
  • Authentication: Bearer Token (passed via the x-api-key header)
  • Content Type: application/json
  • Anthropic Version: 2023-06-01

Request Example

Image Recognition

The examples below consistently use base64 to pass images, which is compatible with both the Anthropic dedicated channel and the AWS Bedrock standard channel.

python
#!/usr/bin/env python3
# -*- coding: utf-8 -*-

import base64
import mimetypes
import os
import sys

import anthropic

BASE_URL = "https://api.agentsflare.com/anthropic"
API_KEY = ""
MODEL = "claude-sonnet-4-5-20250929"


def file_to_base64(path: str) -> tuple[str, str]:
    if not os.path.isfile(path):
        raise FileNotFoundError(f"File not found: {path}")

    mime, _ = mimetypes.guess_type(path)
    if mime is None:
        mime = "image/jpeg"

    with open(path, "rb") as f:
        data = base64.b64encode(f.read()).decode("utf-8")

    return data, mime


def main():
    if len(sys.argv) < 2:
        print(f"Usage: {sys.argv[0]} /path/to/image.jpg")
        sys.exit(1)

    image_path = sys.argv[1]
    image_data, media_type = file_to_base64(image_path)

    client = anthropic.Anthropic(
        api_key=API_KEY,
        base_url=BASE_URL,
    )

    message = client.messages.create(
        model=MODEL,
        max_tokens=1024,
        messages=[
            {
                "role": "user",
                "content": [
                    {
                        "type": "text",
                        "text": "Please analyze this image in detail, describing what you see, the scene, the subject, any text, and any noteworthy details.",
                    },
                    {
                        "type": "image",
                        "source": {
                            "type": "base64",
                            "media_type": media_type,
                            "data": image_data,
                        },
                    },
                ],
            }
        ],
    )

    print(message.content[0].text)


if __name__ == "__main__":
    main()
javascript
import fs from "fs";
import path from "path";
import Anthropic from "@anthropic-ai/sdk";

const BASE_URL = "https://api.agentsflare.com/anthropic";
const API_KEY = process.env.ANTHROPIC_API_KEY || ""; // Recommended to use environment variables
const MODEL = "claude-sonnet-4-5-20250929";

function fileToBase64(filePath) {
  if (!fs.existsSync(filePath)) {
    throw new Error(`File not found: ${filePath}`);
  }

  const ext = path.extname(filePath).toLowerCase();
  let mediaType = "image/jpeg";
  if (ext === ".png") mediaType = "image/png";
  else if (ext === ".webp") mediaType = "image/webp";
  else if (ext === ".gif") mediaType = "image/gif";

  const data = fs.readFileSync(filePath).toString("base64");
  return { data, mediaType };
}

async function main() {
  const imagePath = process.argv[2];
  if (!imagePath) {
    console.error(`Usage: ${process.argv[1]} /path/to/image.jpg`);
    process.exit(1);
  }

  if (!API_KEY) {
    console.error("Please set the ANTHROPIC_API_KEY environment variable or fill in API_KEY in the code.");
    process.exit(1);
  }

  const { data, mediaType } = fileToBase64(imagePath);

  const client = new Anthropic({
    apiKey: API_KEY,
    baseURL: BASE_URL,
  });

  const message = await client.messages.create({
    model: MODEL,
    max_tokens: 1024,
    messages: [
      {
        role: "user",
        content: [
          {
            type: "text",
            text: "Please analyze this image in detail, describing what you see, the scene, the subject, any text, and any noteworthy details.",
          },
          {
            type: "image",
            source: {
              type: "base64",
              media_type: mediaType,
              data: data,
            },
          },
        ],
      },
    ],
  });

  console.log(message.content[0].text);
}

main().catch((err) => {
  console.error(err);
  process.exit(1);
});
javascript
const fs = require("fs");
const path = require("path");
const Anthropic = require("@anthropic-ai/sdk");

const BASE_URL = "https://api.agentsflare.com/anthropic";
const API_KEY = process.env.ANTHROPIC_API_KEY || ""; // Recommended to use environment variables
const MODEL = "claude-sonnet-4-5-20250929";

function fileToBase64(filePath) {
  if (!fs.existsSync(filePath)) {
    throw new Error(`File not found: ${filePath}`);
  }

  const ext = path.extname(filePath).toLowerCase();
  let mediaType = "image/jpeg";
  if (ext === ".png") mediaType = "image/png";
  else if (ext === ".webp") mediaType = "image/webp";
  else if (ext === ".gif") mediaType = "image/gif";

  const data = fs.readFileSync(filePath).toString("base64");
  return { data, mediaType };
}

async function main() {
  const imagePath = process.argv[2];
  if (!imagePath) {
    console.error(`Usage: ${process.argv[1]} /path/to/image.jpg`);
    process.exit(1);
  }

  if (!API_KEY) {
    console.error("Please set the ANTHROPIC_API_KEY environment variable or fill in API_KEY in the code.");
    process.exit(1);
  }

  const { data, mediaType } = fileToBase64(imagePath);

  const client = new Anthropic({
    apiKey: API_KEY,
    baseURL: BASE_URL,
  });

  const message = await client.messages.create({
    model: MODEL,
    max_tokens: 1024,
    messages: [
      {
        role: "user",
        content: [
          {
            type: "text",
            text: "Please analyze this image in detail, describing what you see, the scene, the subject, any text, and any noteworthy details.",
          },
          {
            type: "image",
            source: {
              type: "base64",
              media_type: mediaType,
              data: data,
            },
          },
        ],
      },
    ],
  });

  console.log(message.content[0].text);
}

main().catch((err) => {
  console.error(err);
  process.exit(1);
});
go
package main

import (
	"bytes"
	"encoding/base64"
	"encoding/json"
	"fmt"
	"io"
	"mime"
	"net/http"
	"os"
	"path/filepath"
)

const (
	BASE_URL = "https://api.agentsflare.com/anthropic/v1"
	MODEL    = "claude-sonnet-4-5-20250929"
)

func fileToBase64(path string) (string, string, error) {
	info, err := os.Stat(path)
	if err != nil || info.IsDir() {
		return "", "", fmt.Errorf("File not found: %s", path)
	}

	ext := filepath.Ext(path)
	mediaType := mime.TypeByExtension(ext)
	if mediaType == "" {
		mediaType = "image/jpeg"
	}

	b, err := os.ReadFile(path)
	if err != nil {
		return "", "", err
	}

	return base64.StdEncoding.EncodeToString(b), mediaType, nil
}

func main() {
	if len(os.Args) < 2 {
		fmt.Printf("Usage: %s /path/to/image.jpg\n", os.Args[0])
		os.Exit(1)
	}

	apiKey := os.Getenv("ANTHROPIC_API_KEY")
	if apiKey == "" {
		fmt.Println("Please set the ANTHROPIC_API_KEY environment variable.")
		os.Exit(1)
	}

	imagePath := os.Args[1]
	imageData, mediaType, err := fileToBase64(imagePath)
	if err != nil {
		fmt.Println(err)
		os.Exit(1)
	}

	reqBody := map[string]any{
		"model":      MODEL,
		"max_tokens": 1024,
		"messages": []any{
			map[string]any{
				"role": "user",
				"content": []any{
					map[string]any{
						"type": "text",
						"text": "Please analyze this image in detail, describing what you see, the scene, the subject, any text, and any noteworthy details.",
					},
					map[string]any{
						"type": "image",
						"source": map[string]any{
							"type":       "base64",
							"media_type": mediaType,
							"data":       imageData,
						},
					},
				},
			},
		},
	}

	bodyBytes, _ := json.Marshal(reqBody)

	req, err := http.NewRequest("POST", BASE_URL+"/messages", bytes.NewReader(bodyBytes))
	if err != nil {
		fmt.Println(err)
		os.Exit(1)
	}

	req.Header.Set("x-api-key", apiKey)
	req.Header.Set("anthropic-version", "2023-06-01")
	req.Header.Set("Content-Type", "application/json")

	resp, err := http.DefaultClient.Do(req)
	if err != nil {
		fmt.Println(err)
		os.Exit(1)
	}
	defer resp.Body.Close()

	respBytes, _ := io.ReadAll(resp.Body)
	if resp.StatusCode < 200 || resp.StatusCode >= 300 {
		fmt.Printf("HTTP %d\n%s\n", resp.StatusCode, string(respBytes))
		os.Exit(1)
	}

	var out struct {
		Content []struct {
			Text string `json:"text"`
			Type string `json:"type"`
		} `json:"content"`
	}
	if err := json.Unmarshal(respBytes, &out); err != nil {
		fmt.Println(string(respBytes))
		return
	}

	if len(out.Content) == 0 {
		fmt.Println("")
		return
	}

	fmt.Println(out.Content[0].Text)
}

Note: The size of a single image should not exceed 20M, otherwise the model may return an error.

Channel Differences

Claude image recognition support for image sources differs by channel:

ChannelImage URLbase64Description
Anthropic Dedicated Channel✅ Supported✅ SupportedYou can use an image URL directly or upload base64.
AWS Bedrock Standard Channel❌ Not supported✅ SupportedOnly base64-encoded images are supported; URLs are not allowed.

Therefore, the examples above consistently use base64, which works with both channels. If you are sure you are using the Anthropic dedicated channel, you can also change the source of the image block to URL format:

json
{
  "type": "image",
  "source": {
    "type": "url",
    "url": "https://example.com/image.jpg"
  }
}

Supported Models

The following Claude models support image recognition (Vision) and can be used through this interface (sorted by recommendation):

  • claude-sonnet-4-5-20250929 Recommended
  • claude-sonnet-4-6 Recommended
  • claude-opus-4-7 New
  • claude-opus-4-8 New
  • claude-sonnet-5 New
  • claude-fable-5 New
  • claude-mythos-5 New
  • claude-haiku-4-5-20251001
  • claude-opus-4-6
  • claude-opus-4-5-20251101
  • claude-opus-4-1-20250805
  • claude-sonnet-4-20250514 Deprecated
  • claude-opus-4-20250514 Deprecated

💡 Tip

The model field in the request example can be replaced with any model name above.

This documentation is licensed under CC BY-SA 4.0.