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.2 KB · 18 lines typescript
Raw Blame History
1// TypeScript SDK streaming helper: client.messages.stream() with .on('text') and finalMessage().2// STATUS: LIVE_VERIFIED 2026-09-18 with @anthropic-ai/sdk 0.126.0.3// Run: node --env-file=.env --experimental-strip-types examples/anthropic/streaming/sdk_stream.ts4import Anthropic from "@anthropic-ai/sdk";5import { appendFileSync } from "node:fs";67const client = new Anthropic();8const types: string[] = [];9const stream = client.messages10  .stream({ model: "claude-haiku-4-5-20251001", max_tokens: 16, messages: [{ role: "user", content: "Reply with OK." }] })11  .on("streamEvent", (ev) => types.push(ev.type))12  .on("text", (t) => process.stdout.write(t));13const final = await stream.finalMessage();14console.log("\nevents:", types.join(" "));15console.log("final:", final.stop_reason, final.usage.output_tokens, "output tokens | request-id", stream.request_id);16// low-memory alternative: for await (const ev of await client.messages.create({ ..., stream: true })) { ... }17appendFileSync("reports/live-requests.jsonl", JSON.stringify({ ts: new Date().toISOString().replace(/\.\d+Z$/, "Z"), provider: "anthropic", method: "POST", path: "/v1/messages", status: 200, est_cost_usd: 0.00004, note: "example sdk_stream.ts" }) + "\n");18