/** * Vision with the Node SDK: an image referenced by URL (fetched by Anthropic) plus a base64 1x1 PNG in the same request. * Note: some hosts (e.g. Wikimedia thumbnails) refuse Anthropic's fetcher → 400 "Unable to download the file"; * httpbin.org/image/png worked on 2026-09-18. * STATUS: LIVE_VERIFIED 2026-09-18 (claude-haiku-4-5-20251001): "pink cartoon pig" description, ~40 input tokens (~$0.0001). * Run: node --env-file=.env --experimental-strip-types examples/anthropic/vision/image_url.ts */ import Anthropic from "@anthropic-ai/sdk"; import { appendFileSync } from "node:fs"; const MODEL = process.env.ANTHROPIC_MODEL ?? "claude-haiku-4-5-20251001"; const URL_IMG = process.env.IMAGE_URL ?? "https://httpbin.org/image/png"; const client = new Anthropic({ maxRetries: 1 }); const PNG_B64 = "iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAIAAACQd1PeAAAADElEQVR4nGP4z8AAAAMBAQDJ/pLvAAAAAElFTkSuQmCC"; try { const msg = await client.messages.create({ model: MODEL, max_tokens: 30, messages: [{ role: "user", content: [ { type: "text", text: "Image 1:" }, { type: "image", source: { type: "url", url: URL_IMG } }, { type: "text", text: "Image 2:" }, { type: "image", source: { type: "base64", media_type: "image/png", data: PNG_B64 } }, { type: "text", text: "Describe image 1 in five words, then give the color of image 2." }, ] }], }); console.log(msg.content[0].type === "text" ? msg.content[0].text : msg.content[0].type, "| usage:", msg.usage.input_tokens, "in /", msg.usage.output_tokens, "out"); appendFileSync("reports/live-requests.jsonl", JSON.stringify({ ts: new Date().toISOString(), provider: "anthropic", method: "POST", path: "/v1/messages", status: 200, est_cost_usd: 0.0001, note: `example vision/image_url.ts ${MODEL}` }) + "\n"); } catch (e: any) { console.error("HTTP", e.status, e.message); // e.g. 400 Unable to download the file appendFileSync("reports/live-requests.jsonl", JSON.stringify({ ts: new Date().toISOString(), provider: "anthropic", method: "POST", path: "/v1/messages", status: e.status ?? 0, est_cost_usd: 0, note: `example vision/image_url.ts failed: ${String(e.message).slice(0, 120)}` }) + "\n"); process.exit(1); }