SPB Git forge

spb/doc-api

Public
2commits 1branches 0releases
15.7 MBsize
maindefault branch
14 days agolast push
Python 88.3% TypeScript 7.6% Shell 4.1%
2.2 KB · 32 lines typescript
Raw Blame History
1/**2 * Vision with the Node SDK: an image referenced by URL (fetched by Anthropic) plus a base64 1x1 PNG in the same request.3 * Note: some hosts (e.g. Wikimedia thumbnails) refuse Anthropic's fetcher → 400 "Unable to download the file";4 * httpbin.org/image/png worked on 2026-09-18.5 * STATUS: LIVE_VERIFIED 2026-09-18 (claude-haiku-4-5-20251001): "pink cartoon pig" description, ~40 input tokens (~$0.0001).6 * Run: node --env-file=.env --experimental-strip-types examples/anthropic/vision/image_url.ts7 */8import Anthropic from "@anthropic-ai/sdk";9import { appendFileSync } from "node:fs";1011const MODEL = process.env.ANTHROPIC_MODEL ?? "claude-haiku-4-5-20251001";12const URL_IMG = process.env.IMAGE_URL ?? "https://httpbin.org/image/png";13const client = new Anthropic({ maxRetries: 1 });14const PNG_B64 = "iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAIAAACQd1PeAAAADElEQVR4nGP4z8AAAAMBAQDJ/pLvAAAAAElFTkSuQmCC";1516try {17  const msg = await client.messages.create({18    model: MODEL, max_tokens: 30,19    messages: [{ role: "user", content: [20      { type: "text", text: "Image 1:" }, { type: "image", source: { type: "url", url: URL_IMG } },21      { type: "text", text: "Image 2:" }, { type: "image", source: { type: "base64", media_type: "image/png", data: PNG_B64 } },22      { type: "text", text: "Describe image 1 in five words, then give the color of image 2." },23    ] }],24  });25  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");26  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");27} catch (e: any) {28  console.error("HTTP", e.status, e.message); // e.g. 400 Unable to download the file29  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");30  process.exit(1);31}32