Python 88.3%
TypeScript 7.6%
Shell 4.1%
1// count_tokens: plain, +system, +tool2// STATUS: LIVE_VERIFIED 2026-09-18 with @anthropic-ai/sdk 0.126.0.3// Run: node --env-file=.env --experimental-strip-types examples/anthropic/messages/count_tokens.ts (ANTHROPIC_API_KEY read from env by the SDK)4import Anthropic from "@anthropic-ai/sdk";5import { appendFileSync } from "node:fs";67const client = new Anthropic();8const MODEL = "claude-haiku-4-5-20251001";9const log = (path: string, status: number, note: string) =>10 appendFileSync("reports/live-requests.jsonl", JSON.stringify({ ts: new Date().toISOString().replace(/\.\d+Z$/, "Z"), provider: "anthropic", method: "POST", path, status, est_cost_usd: 0.00005, note }) + "\n");11const messages = [{ role: "user" as const, content: "Reply with OK." }];12const tool = { name: "get_weather", description: "Get current weather for a city.",13 input_schema: { type: "object" as const, properties: { city: { type: "string" } }, required: ["city"] } };14console.log("plain :", (await client.messages.countTokens({ model: MODEL, messages })).input_tokens);15console.log("+system:", (await client.messages.countTokens({ model: MODEL, messages, system: "You are a helpful assistant." })).input_tokens);16console.log("+tool :", (await client.messages.countTokens({ model: MODEL, messages, tools: [tool] })).input_tokens);17for (let i = 0; i < 3; i++) log("/v1/messages/count_tokens", 200, "example count_tokens.ts");18