Python 88.3%
TypeScript 7.6%
Shell 4.1%
1/**2 * Structured outputs + streaming with the Node SDK: JSON arrives as ordinary text_delta chunks; accumulate with3 * finalMessage() then JSON.parse. Also counts tokens with output_config.format (no grammar compilation).4 * STATUS: LIVE_VERIFIED 2026-09-18 (claude-haiku-4-5-20251001): parsed {name:'Ada',age:36,city:'Lyon'}; count_tokens 205 (~$0.0003).5 * Run: node --env-file=.env --experimental-strip-types examples/anthropic/structured-output/json_schema_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 });12const log = (path: string, status: number, note: string) =>13 appendFileSync("reports/live-requests.jsonl", JSON.stringify({ ts: new Date().toISOString(), provider: "anthropic", method: "POST", path, status, est_cost_usd: 0.0003, note }) + "\n");1415const schema = {16 type: "object",17 properties: { name: { type: "string" }, age: { type: "integer" }, city: { type: "string" } },18 required: ["name", "age", "city"], additionalProperties: false,19};20const params = {21 model: MODEL, max_tokens: 100,22 output_config: { format: { type: "json_schema", schema } },23 messages: [{ role: "user", content: "Ada is 36 and lives in Lyon. Extract the person." }],24} as const;2526const deltas: string[] = [];27const stream = client.messages.stream(params as any).on("text", (t) => deltas.push(t));28const final = await stream.finalMessage();29const text = final.content[0].type === "text" ? final.content[0].text : "";30console.log("chunks:", deltas.length, "parsed:", JSON.parse(text));31log("/v1/messages", 200, `example structured-output/json_schema_stream.ts stream ${MODEL}`);3233const count = await client.messages.countTokens({ model: MODEL, messages: params.messages as any, output_config: params.output_config } as any);34console.log("count_tokens with format:", count.input_tokens);35log("/v1/messages/count_tokens", 200, `example structured-output/json_schema_stream.ts count ${MODEL}`);36