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%
1.7 KB · 28 lines typescript
Raw Blame History
1/**2 * Streaming citations with the Node SDK: citations arrive as content_block_delta { delta.type: "citations_delta" }3 * events; finalMessage() reassembles text blocks with their citations arrays.4 * STATUS: LIVE_VERIFIED 2026-09-18 (claude-haiku-4-5-20251001): 1 citations_delta (char_location 17-32) (~$0.0007).5 * Run: node --env-file=.env --experimental-strip-types examples/anthropic/citations/citations_stream.ts6 */7import Anthropic from "@anthropic-ai/sdk";8import { appendFileSync } from "node:fs";910const MODEL = process.env.ANTHROPIC_MODEL ?? "claude-haiku-4-5-20251001";11const client = new Anthropic({ maxRetries: 1 });1213const citations: unknown[] = [];14const stream = client.messages.stream({15  model: MODEL, max_tokens: 100,16  messages: [{ role: "user", content: [17    { type: "document", source: { type: "text", media_type: "text/plain", data: "The sky is blue. Grass is green." }, title: "Colors", citations: { enabled: true } },18    { type: "text", text: "What color is grass? Answer in one sentence and cite." },19  ] }],20});21stream.on("streamEvent", (ev) => {22  if (ev.type === "content_block_delta" && ev.delta.type === "citations_delta") citations.push(ev.delta.citation);23});24const final = await stream.finalMessage();25console.log("citations_delta events:", citations.length, JSON.stringify(citations[0]));26console.log("final text blocks:", final.content.map((b) => (b.type === "text" ? { text: b.text, n: b.citations?.length ?? 0 } : b.type)));27appendFileSync("reports/live-requests.jsonl", JSON.stringify({ ts: new Date().toISOString(), provider: "anthropic", method: "POST", path: "/v1/messages", status: 200, est_cost_usd: 0.0007, note: `example citations/citations_stream.ts ${MODEL}` }) + "\n");28