total-tokens.test.ts 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794
  1. /**
  2. * Test totalTokens field across all providers.
  3. *
  4. * totalTokens represents the total number of tokens processed by the LLM,
  5. * including input (with cache) and output (with thinking). This is the
  6. * base for calculating context size for the next request.
  7. *
  8. * - OpenAI Completions: Uses native total_tokens field
  9. * - OpenAI Responses: Uses native total_tokens field
  10. * - Google: Uses native totalTokenCount field
  11. * - Anthropic: Computed as input + output + cacheRead + cacheWrite
  12. * - Other OpenAI-compatible providers: Uses native total_tokens field
  13. */
  14. import { describe, expect, it } from "vitest";
  15. import { getModel } from "../src/models.ts";
  16. import { complete } from "../src/stream.ts";
  17. import type { Api, Context, Model, StreamOptions, Usage } from "../src/types.ts";
  18. type StreamOptionsWithExtras = StreamOptions & Record<string, unknown>;
  19. import { hasAzureOpenAICredentials, resolveAzureDeploymentName } from "./azure-utils.ts";
  20. import { hasBedrockCredentials } from "./bedrock-utils.ts";
  21. import { hasCloudflareAiGatewayCredentials, hasCloudflareWorkersAICredentials } from "./cloudflare-utils.ts";
  22. import { resolveApiKey } from "./oauth.ts";
  23. // Resolve OAuth tokens at module level (async, runs before tests)
  24. const oauthTokens = await Promise.all([
  25. resolveApiKey("anthropic"),
  26. resolveApiKey("github-copilot"),
  27. resolveApiKey("openai-codex"),
  28. ]);
  29. const [anthropicOAuthToken, githubCopilotToken, openaiCodexToken] = oauthTokens;
  30. // Generate a long system prompt to trigger caching (>2k bytes for most providers)
  31. const LONG_SYSTEM_PROMPT = `You are a helpful assistant. Be concise in your responses.
  32. Here is some additional context that makes this system prompt long enough to trigger caching:
  33. ${Array(50)
  34. .fill(
  35. "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris.",
  36. )
  37. .join("\n\n")}
  38. Remember: Always be helpful and concise.`;
  39. async function testTotalTokensWithCache<TApi extends Api>(
  40. llm: Model<TApi>,
  41. options: StreamOptionsWithExtras = {},
  42. ): Promise<{ first: Usage; second: Usage }> {
  43. // First request - no cache
  44. const context1: Context = {
  45. systemPrompt: LONG_SYSTEM_PROMPT,
  46. messages: [
  47. {
  48. role: "user",
  49. content: "What is 2 + 2? Reply with just the number.",
  50. timestamp: Date.now(),
  51. },
  52. ],
  53. };
  54. const response1 = await complete(llm, context1, options);
  55. expect(response1.stopReason).toBe("stop");
  56. // Second request - should trigger cache read (same system prompt, add conversation)
  57. const context2: Context = {
  58. systemPrompt: LONG_SYSTEM_PROMPT,
  59. messages: [
  60. ...context1.messages,
  61. response1, // Include previous assistant response
  62. {
  63. role: "user",
  64. content: "What is 3 + 3? Reply with just the number.",
  65. timestamp: Date.now(),
  66. },
  67. ],
  68. };
  69. const response2 = await complete(llm, context2, options);
  70. expect(response2.stopReason).toBe("stop");
  71. return { first: response1.usage, second: response2.usage };
  72. }
  73. function logUsage(label: string, usage: Usage) {
  74. const computed = usage.input + usage.output + usage.cacheRead + usage.cacheWrite;
  75. console.log(` ${label}:`);
  76. console.log(
  77. ` input: ${usage.input}, output: ${usage.output}, cacheRead: ${usage.cacheRead}, cacheWrite: ${usage.cacheWrite}`,
  78. );
  79. console.log(` totalTokens: ${usage.totalTokens}, computed: ${computed}`);
  80. }
  81. function assertTotalTokensEqualsComponents(usage: Usage) {
  82. const computed = usage.input + usage.output + usage.cacheRead + usage.cacheWrite;
  83. expect(usage.totalTokens).toBe(computed);
  84. }
  85. describe("totalTokens field", () => {
  86. // =========================================================================
  87. // Anthropic
  88. // =========================================================================
  89. describe.skipIf(!process.env.ANTHROPIC_API_KEY)("Anthropic (API Key)", () => {
  90. it(
  91. "claude-sonnet-4-5 - should return totalTokens equal to sum of components",
  92. { retry: 3, timeout: 60000 },
  93. async () => {
  94. const llm = getModel("anthropic", "claude-sonnet-4-5");
  95. console.log(`\nAnthropic / ${llm.id}:`);
  96. const { first, second } = await testTotalTokensWithCache(llm, { apiKey: process.env.ANTHROPIC_API_KEY });
  97. logUsage("First request", first);
  98. logUsage("Second request", second);
  99. assertTotalTokensEqualsComponents(first);
  100. assertTotalTokensEqualsComponents(second);
  101. // Anthropic should have cache activity
  102. const hasCache = second.cacheRead > 0 || second.cacheWrite > 0 || first.cacheWrite > 0;
  103. expect(hasCache).toBe(true);
  104. },
  105. );
  106. });
  107. describe("Anthropic (OAuth)", () => {
  108. it.skipIf(!anthropicOAuthToken)(
  109. "claude-sonnet-4 - should return totalTokens equal to sum of components",
  110. { retry: 3, timeout: 60000 },
  111. async () => {
  112. const llm = getModel("anthropic", "claude-sonnet-4-6");
  113. console.log(`\nAnthropic OAuth / ${llm.id}:`);
  114. const { first, second } = await testTotalTokensWithCache(llm, { apiKey: anthropicOAuthToken });
  115. logUsage("First request", first);
  116. logUsage("Second request", second);
  117. assertTotalTokensEqualsComponents(first);
  118. assertTotalTokensEqualsComponents(second);
  119. // Anthropic should have cache activity
  120. const hasCache = second.cacheRead > 0 || second.cacheWrite > 0 || first.cacheWrite > 0;
  121. expect(hasCache).toBe(true);
  122. },
  123. );
  124. });
  125. // =========================================================================
  126. // OpenAI
  127. // =========================================================================
  128. describe.skipIf(!process.env.OPENAI_API_KEY)("OpenAI Completions", () => {
  129. it(
  130. "gpt-4o-mini - should return totalTokens equal to sum of components",
  131. { retry: 3, timeout: 60000 },
  132. async () => {
  133. const { compat: _compat, ...baseModel } = getModel("openai", "gpt-4o-mini")!;
  134. void _compat;
  135. const llm: Model<"openai-completions"> = {
  136. ...baseModel,
  137. api: "openai-completions",
  138. };
  139. console.log(`\nOpenAI Completions / ${llm.id}:`);
  140. const { first, second } = await testTotalTokensWithCache(llm);
  141. logUsage("First request", first);
  142. logUsage("Second request", second);
  143. assertTotalTokensEqualsComponents(first);
  144. assertTotalTokensEqualsComponents(second);
  145. },
  146. );
  147. });
  148. describe.skipIf(!process.env.OPENAI_API_KEY)("OpenAI Responses", () => {
  149. it(
  150. "claude-haiku-4.5 - should return totalTokens equal to sum of components",
  151. { retry: 3, timeout: 60000 },
  152. async () => {
  153. const llm = getModel("openai", "gpt-4o");
  154. console.log(`\nOpenAI Responses / ${llm.id}:`);
  155. const { first, second } = await testTotalTokensWithCache(llm);
  156. logUsage("First request", first);
  157. logUsage("Second request", second);
  158. assertTotalTokensEqualsComponents(first);
  159. assertTotalTokensEqualsComponents(second);
  160. },
  161. );
  162. });
  163. describe.skipIf(!hasAzureOpenAICredentials())("Azure OpenAI Responses", () => {
  164. it(
  165. "gpt-4o-mini - should return totalTokens equal to sum of components",
  166. { retry: 3, timeout: 60000 },
  167. async () => {
  168. const llm = getModel("azure-openai-responses", "gpt-4o-mini");
  169. const azureDeploymentName = resolveAzureDeploymentName(llm.id);
  170. const azureOptions = azureDeploymentName ? { azureDeploymentName } : {};
  171. console.log(`\nAzure OpenAI Responses / ${llm.id}:`);
  172. const { first, second } = await testTotalTokensWithCache(llm, azureOptions);
  173. logUsage("First request", first);
  174. logUsage("Second request", second);
  175. assertTotalTokensEqualsComponents(first);
  176. assertTotalTokensEqualsComponents(second);
  177. },
  178. );
  179. });
  180. // =========================================================================
  181. // Google
  182. // =========================================================================
  183. describe.skipIf(!process.env.GEMINI_API_KEY)("Google", () => {
  184. it(
  185. "gemini-2.0-flash - should return totalTokens equal to sum of components",
  186. { retry: 3, timeout: 60000 },
  187. async () => {
  188. const llm = getModel("google", "gemini-2.0-flash");
  189. console.log(`\nGoogle / ${llm.id}:`);
  190. const { first, second } = await testTotalTokensWithCache(llm);
  191. logUsage("First request", first);
  192. logUsage("Second request", second);
  193. assertTotalTokensEqualsComponents(first);
  194. assertTotalTokensEqualsComponents(second);
  195. },
  196. );
  197. });
  198. // =========================================================================
  199. // xAI
  200. // =========================================================================
  201. describe.skipIf(!process.env.XAI_API_KEY)("xAI", () => {
  202. it(
  203. "grok-3-fast - should return totalTokens equal to sum of components",
  204. { retry: 3, timeout: 60000 },
  205. async () => {
  206. const llm = getModel("xai", "grok-3-fast");
  207. console.log(`\nxAI / ${llm.id}:`);
  208. const { first, second } = await testTotalTokensWithCache(llm, { apiKey: process.env.XAI_API_KEY });
  209. logUsage("First request", first);
  210. logUsage("Second request", second);
  211. assertTotalTokensEqualsComponents(first);
  212. assertTotalTokensEqualsComponents(second);
  213. },
  214. );
  215. });
  216. // =========================================================================
  217. // Groq
  218. // =========================================================================
  219. describe.skipIf(!process.env.GROQ_API_KEY)("Groq", () => {
  220. it(
  221. "openai/gpt-oss-120b - should return totalTokens equal to sum of components",
  222. { retry: 3, timeout: 60000 },
  223. async () => {
  224. const llm = getModel("groq", "openai/gpt-oss-120b");
  225. console.log(`\nGroq / ${llm.id}:`);
  226. const { first, second } = await testTotalTokensWithCache(llm, { apiKey: process.env.GROQ_API_KEY });
  227. logUsage("First request", first);
  228. logUsage("Second request", second);
  229. assertTotalTokensEqualsComponents(first);
  230. assertTotalTokensEqualsComponents(second);
  231. },
  232. );
  233. });
  234. // =========================================================================
  235. // Cerebras
  236. // =========================================================================
  237. describe.skipIf(!process.env.CEREBRAS_API_KEY)("Cerebras", () => {
  238. it(
  239. "gpt-oss-120b - should return totalTokens equal to sum of components",
  240. { retry: 3, timeout: 60000 },
  241. async () => {
  242. const llm = getModel("cerebras", "gpt-oss-120b");
  243. console.log(`\nCerebras / ${llm.id}:`);
  244. const { first, second } = await testTotalTokensWithCache(llm, { apiKey: process.env.CEREBRAS_API_KEY });
  245. logUsage("First request", first);
  246. logUsage("Second request", second);
  247. assertTotalTokensEqualsComponents(first);
  248. assertTotalTokensEqualsComponents(second);
  249. },
  250. );
  251. });
  252. // =========================================================================
  253. // Cloudflare Workers AI
  254. // =========================================================================
  255. describe.skipIf(!hasCloudflareWorkersAICredentials())("Cloudflare Workers AI", () => {
  256. it(
  257. "@cf/moonshotai/kimi-k2.6 - should return totalTokens equal to sum of components",
  258. { retry: 3, timeout: 60000 },
  259. async () => {
  260. const llm = getModel("cloudflare-workers-ai", "@cf/moonshotai/kimi-k2.6");
  261. console.log(`\nCloudflare Workers AI / ${llm.id}:`);
  262. const { first, second } = await testTotalTokensWithCache(llm, {
  263. apiKey: process.env.CLOUDFLARE_API_KEY,
  264. });
  265. logUsage("First request", first);
  266. logUsage("Second request", second);
  267. assertTotalTokensEqualsComponents(first);
  268. assertTotalTokensEqualsComponents(second);
  269. },
  270. );
  271. });
  272. // =========================================================================
  273. // Cloudflare AI Gateway
  274. // =========================================================================
  275. describe.skipIf(!hasCloudflareAiGatewayCredentials())("Cloudflare AI Gateway", () => {
  276. it(
  277. "workers-ai/@cf/moonshotai/kimi-k2.6 - should return totalTokens equal to sum of components",
  278. { retry: 3, timeout: 60000 },
  279. async () => {
  280. const llm = getModel("cloudflare-ai-gateway", "workers-ai/@cf/moonshotai/kimi-k2.6");
  281. console.log(`\nCloudflare AI Gateway / ${llm.id}:`);
  282. const { first, second } = await testTotalTokensWithCache(llm, {
  283. apiKey: process.env.CLOUDFLARE_API_KEY,
  284. });
  285. logUsage("First request", first);
  286. logUsage("Second request", second);
  287. assertTotalTokensEqualsComponents(first);
  288. assertTotalTokensEqualsComponents(second);
  289. },
  290. );
  291. });
  292. // =========================================================================
  293. // Hugging Face
  294. // =========================================================================
  295. describe.skipIf(!process.env.HF_TOKEN)("Hugging Face", () => {
  296. it("Kimi-K2.5 - should return totalTokens equal to sum of components", { retry: 3, timeout: 60000 }, async () => {
  297. const llm = getModel("huggingface", "moonshotai/Kimi-K2.5");
  298. console.log(`\nHugging Face / ${llm.id}:`);
  299. const { first, second } = await testTotalTokensWithCache(llm, { apiKey: process.env.HF_TOKEN });
  300. logUsage("First request", first);
  301. logUsage("Second request", second);
  302. assertTotalTokensEqualsComponents(first);
  303. assertTotalTokensEqualsComponents(second);
  304. });
  305. });
  306. // =========================================================================
  307. // Together AI
  308. // =========================================================================
  309. describe.skipIf(!process.env.TOGETHER_API_KEY)("Together AI", () => {
  310. it("Kimi-K2.6 - should return totalTokens equal to sum of components", { retry: 3, timeout: 60000 }, async () => {
  311. const llm = getModel("together", "moonshotai/Kimi-K2.6");
  312. console.log(`\nTogether AI / ${llm.id}:`);
  313. const { first, second } = await testTotalTokensWithCache(llm, {
  314. apiKey: process.env.TOGETHER_API_KEY,
  315. reasoningEffort: "high",
  316. });
  317. logUsage("First request", first);
  318. logUsage("Second request", second);
  319. assertTotalTokensEqualsComponents(first);
  320. assertTotalTokensEqualsComponents(second);
  321. });
  322. });
  323. // =========================================================================
  324. // z.ai
  325. // =========================================================================
  326. describe.skipIf(!process.env.ZAI_API_KEY)("z.ai", () => {
  327. it(
  328. "glm-4.5-air - should return totalTokens equal to sum of components",
  329. { retry: 3, timeout: 60000 },
  330. async () => {
  331. const llm = getModel("zai", "glm-4.5-air");
  332. console.log(`\nz.ai / ${llm.id}:`);
  333. const { first, second } = await testTotalTokensWithCache(llm, { apiKey: process.env.ZAI_API_KEY });
  334. logUsage("First request", first);
  335. logUsage("Second request", second);
  336. assertTotalTokensEqualsComponents(first);
  337. assertTotalTokensEqualsComponents(second);
  338. },
  339. );
  340. });
  341. // =========================================================================
  342. // Mistral
  343. // =========================================================================
  344. describe.skipIf(!process.env.MISTRAL_API_KEY)("Mistral", () => {
  345. it(
  346. "devstral-medium-latest - should return totalTokens equal to sum of components",
  347. { retry: 3, timeout: 60000 },
  348. async () => {
  349. const llm = getModel("mistral", "devstral-medium-latest");
  350. console.log(`\nMistral / ${llm.id}:`);
  351. const { first, second } = await testTotalTokensWithCache(llm, { apiKey: process.env.MISTRAL_API_KEY });
  352. logUsage("First request", first);
  353. logUsage("Second request", second);
  354. assertTotalTokensEqualsComponents(first);
  355. assertTotalTokensEqualsComponents(second);
  356. },
  357. );
  358. });
  359. // =========================================================================
  360. // MiniMax
  361. // =========================================================================
  362. describe.skipIf(!process.env.MINIMAX_API_KEY)("MiniMax", () => {
  363. it(
  364. "MiniMax-M2.7 - should return totalTokens equal to sum of components",
  365. { retry: 3, timeout: 60000 },
  366. async () => {
  367. const llm = getModel("minimax", "MiniMax-M2.7");
  368. console.log(`\nMiniMax / ${llm.id}:`);
  369. const { first, second } = await testTotalTokensWithCache(llm, { apiKey: process.env.MINIMAX_API_KEY });
  370. logUsage("First request", first);
  371. logUsage("Second request", second);
  372. assertTotalTokensEqualsComponents(first);
  373. assertTotalTokensEqualsComponents(second);
  374. },
  375. );
  376. });
  377. // =========================================================================
  378. // Xiaomi MiMo
  379. // =========================================================================
  380. describe.skipIf(!process.env.XIAOMI_API_KEY)("Xiaomi MiMo (API billing)", () => {
  381. it(
  382. "mimo-v2.5-pro - should return totalTokens equal to sum of components",
  383. { retry: 3, timeout: 60000 },
  384. async () => {
  385. const llm = getModel("xiaomi", "mimo-v2.5-pro");
  386. console.log(`\nXiaomi MiMo / ${llm.id}:`);
  387. const { first, second } = await testTotalTokensWithCache(llm, { apiKey: process.env.XIAOMI_API_KEY });
  388. logUsage("First request", first);
  389. logUsage("Second request", second);
  390. assertTotalTokensEqualsComponents(first);
  391. assertTotalTokensEqualsComponents(second);
  392. },
  393. );
  394. });
  395. // =========================================================================
  396. // Xiaomi MiMo Token Plan CN
  397. // =========================================================================
  398. describe.skipIf(!process.env.XIAOMI_TOKEN_PLAN_CN_API_KEY)("Xiaomi MiMo Token Plan (CN)", () => {
  399. it(
  400. "mimo-v2.5-pro - should return totalTokens equal to sum of components",
  401. { retry: 3, timeout: 60000 },
  402. async () => {
  403. const llm = getModel("xiaomi-token-plan-cn", "mimo-v2.5-pro");
  404. console.log(`\nXiaomi MiMo Token Plan CN / ${llm.id}:`);
  405. const { first, second } = await testTotalTokensWithCache(llm, {
  406. apiKey: process.env.XIAOMI_TOKEN_PLAN_CN_API_KEY,
  407. });
  408. logUsage("First request", first);
  409. logUsage("Second request", second);
  410. assertTotalTokensEqualsComponents(first);
  411. assertTotalTokensEqualsComponents(second);
  412. },
  413. );
  414. });
  415. // =========================================================================
  416. // Xiaomi MiMo Token Plan AMS
  417. // =========================================================================
  418. describe.skipIf(!process.env.XIAOMI_TOKEN_PLAN_AMS_API_KEY)("Xiaomi MiMo Token Plan (AMS)", () => {
  419. it(
  420. "mimo-v2.5-pro - should return totalTokens equal to sum of components",
  421. { retry: 3, timeout: 60000 },
  422. async () => {
  423. const llm = getModel("xiaomi-token-plan-ams", "mimo-v2.5-pro");
  424. console.log(`\nXiaomi MiMo Token Plan AMS / ${llm.id}:`);
  425. const { first, second } = await testTotalTokensWithCache(llm, {
  426. apiKey: process.env.XIAOMI_TOKEN_PLAN_AMS_API_KEY,
  427. });
  428. logUsage("First request", first);
  429. logUsage("Second request", second);
  430. assertTotalTokensEqualsComponents(first);
  431. assertTotalTokensEqualsComponents(second);
  432. },
  433. );
  434. });
  435. // =========================================================================
  436. // Xiaomi MiMo Token Plan SGP
  437. // =========================================================================
  438. describe.skipIf(!process.env.XIAOMI_TOKEN_PLAN_SGP_API_KEY)("Xiaomi MiMo Token Plan (SGP)", () => {
  439. it(
  440. "mimo-v2.5-pro - should return totalTokens equal to sum of components",
  441. { retry: 3, timeout: 60000 },
  442. async () => {
  443. const llm = getModel("xiaomi-token-plan-sgp", "mimo-v2.5-pro");
  444. console.log(`\nXiaomi MiMo Token Plan SGP / ${llm.id}:`);
  445. const { first, second } = await testTotalTokensWithCache(llm, {
  446. apiKey: process.env.XIAOMI_TOKEN_PLAN_SGP_API_KEY,
  447. });
  448. logUsage("First request", first);
  449. logUsage("Second request", second);
  450. assertTotalTokensEqualsComponents(first);
  451. assertTotalTokensEqualsComponents(second);
  452. },
  453. );
  454. });
  455. // =========================================================================
  456. // Kimi For Coding
  457. // =========================================================================
  458. describe.skipIf(!process.env.KIMI_API_KEY)("Kimi For Coding", () => {
  459. it(
  460. "kimi-k2-thinking - should return totalTokens equal to sum of components",
  461. { retry: 3, timeout: 60000 },
  462. async () => {
  463. const llm = getModel("kimi-coding", "kimi-k2-thinking");
  464. console.log(`\nKimi For Coding / ${llm.id}:`);
  465. const { first, second } = await testTotalTokensWithCache(llm, { apiKey: process.env.KIMI_API_KEY });
  466. logUsage("First request", first);
  467. logUsage("Second request", second);
  468. assertTotalTokensEqualsComponents(first);
  469. assertTotalTokensEqualsComponents(second);
  470. },
  471. );
  472. });
  473. // =========================================================================
  474. // Vercel AI Gateway
  475. // =========================================================================
  476. describe.skipIf(!process.env.AI_GATEWAY_API_KEY)("Vercel AI Gateway", () => {
  477. it(
  478. "google/gemini-2.5-flash - should return totalTokens equal to sum of components",
  479. { retry: 3, timeout: 60000 },
  480. async () => {
  481. const llm = getModel("vercel-ai-gateway", "google/gemini-2.5-flash");
  482. console.log(`\nVercel AI Gateway / ${llm.id}:`);
  483. const { first, second } = await testTotalTokensWithCache(llm, { apiKey: process.env.AI_GATEWAY_API_KEY });
  484. logUsage("First request", first);
  485. logUsage("Second request", second);
  486. assertTotalTokensEqualsComponents(first);
  487. assertTotalTokensEqualsComponents(second);
  488. },
  489. );
  490. });
  491. // =========================================================================
  492. // OpenRouter - Multiple backend providers
  493. // =========================================================================
  494. describe.skipIf(!process.env.OPENROUTER_API_KEY)("OpenRouter", () => {
  495. it(
  496. "anthropic/claude-sonnet-4 - should return totalTokens equal to sum of components",
  497. { retry: 3, timeout: 60000 },
  498. async () => {
  499. const llm = getModel("openrouter", "anthropic/claude-sonnet-4");
  500. console.log(`\nOpenRouter / ${llm.id}:`);
  501. const { first, second } = await testTotalTokensWithCache(llm, { apiKey: process.env.OPENROUTER_API_KEY });
  502. logUsage("First request", first);
  503. logUsage("Second request", second);
  504. assertTotalTokensEqualsComponents(first);
  505. assertTotalTokensEqualsComponents(second);
  506. },
  507. );
  508. it(
  509. "deepseek/deepseek-chat - should return totalTokens equal to sum of components",
  510. { retry: 3, timeout: 60000 },
  511. async () => {
  512. const llm = getModel("openrouter", "deepseek/deepseek-chat");
  513. console.log(`\nOpenRouter / ${llm.id}:`);
  514. const { first, second } = await testTotalTokensWithCache(llm, { apiKey: process.env.OPENROUTER_API_KEY });
  515. logUsage("First request", first);
  516. logUsage("Second request", second);
  517. assertTotalTokensEqualsComponents(first);
  518. assertTotalTokensEqualsComponents(second);
  519. },
  520. );
  521. it(
  522. "mistralai/mistral-small-3.2-24b-instruct - should return totalTokens equal to sum of components",
  523. { retry: 3, timeout: 60000 },
  524. async () => {
  525. const llm = getModel("openrouter", "mistralai/mistral-small-3.2-24b-instruct");
  526. console.log(`\nOpenRouter / ${llm.id}:`);
  527. const { first, second } = await testTotalTokensWithCache(llm, { apiKey: process.env.OPENROUTER_API_KEY });
  528. logUsage("First request", first);
  529. logUsage("Second request", second);
  530. assertTotalTokensEqualsComponents(first);
  531. assertTotalTokensEqualsComponents(second);
  532. },
  533. );
  534. it(
  535. "google/gemini-2.5-flash - should return totalTokens equal to sum of components",
  536. { retry: 3, timeout: 60000 },
  537. async () => {
  538. const llm = getModel("openrouter", "google/gemini-2.5-flash");
  539. console.log(`\nOpenRouter / ${llm.id}:`);
  540. const { first, second } = await testTotalTokensWithCache(llm, { apiKey: process.env.OPENROUTER_API_KEY });
  541. logUsage("First request", first);
  542. logUsage("Second request", second);
  543. assertTotalTokensEqualsComponents(first);
  544. assertTotalTokensEqualsComponents(second);
  545. },
  546. );
  547. it(
  548. "deepseek/deepseek-chat - should return totalTokens equal to sum of components",
  549. { retry: 3, timeout: 60000 },
  550. async () => {
  551. const llm = getModel("openrouter", "deepseek/deepseek-chat");
  552. console.log(`\nOpenRouter / ${llm.id}:`);
  553. const { first, second } = await testTotalTokensWithCache(llm, { apiKey: process.env.OPENROUTER_API_KEY });
  554. logUsage("First request", first);
  555. logUsage("Second request", second);
  556. assertTotalTokensEqualsComponents(first);
  557. assertTotalTokensEqualsComponents(second);
  558. },
  559. );
  560. });
  561. // =========================================================================
  562. // GitHub Copilot (OAuth)
  563. // =========================================================================
  564. describe("GitHub Copilot (OAuth)", () => {
  565. it.skipIf(!githubCopilotToken)(
  566. "claude-haiku-4.5 - should return totalTokens equal to sum of components",
  567. { retry: 3, timeout: 60000 },
  568. async () => {
  569. const llm = getModel("github-copilot", "claude-haiku-4.5");
  570. console.log(`\nGitHub Copilot / ${llm.id}:`);
  571. const { first, second } = await testTotalTokensWithCache(llm, { apiKey: githubCopilotToken });
  572. logUsage("First request", first);
  573. logUsage("Second request", second);
  574. assertTotalTokensEqualsComponents(first);
  575. assertTotalTokensEqualsComponents(second);
  576. },
  577. );
  578. it.skipIf(!githubCopilotToken)(
  579. "claude-sonnet-4 - should return totalTokens equal to sum of components",
  580. { retry: 3, timeout: 60000 },
  581. async () => {
  582. const llm = getModel("github-copilot", "claude-sonnet-4.6");
  583. console.log(`\nGitHub Copilot / ${llm.id}:`);
  584. const { first, second } = await testTotalTokensWithCache(llm, { apiKey: githubCopilotToken });
  585. logUsage("First request", first);
  586. logUsage("Second request", second);
  587. assertTotalTokensEqualsComponents(first);
  588. assertTotalTokensEqualsComponents(second);
  589. },
  590. );
  591. });
  592. // =========================================================================
  593. // =========================================================================
  594. // =========================================================================
  595. // =========================================================================
  596. describe.skipIf(!hasBedrockCredentials())("Amazon Bedrock", () => {
  597. it(
  598. "claude-sonnet-4-5 - should return totalTokens equal to sum of components",
  599. { retry: 3, timeout: 60000 },
  600. async () => {
  601. const llm = getModel("amazon-bedrock", "global.anthropic.claude-sonnet-4-5-20250929-v1:0");
  602. console.log(`\nAmazon Bedrock / ${llm.id}:`);
  603. const { first, second } = await testTotalTokensWithCache(llm);
  604. logUsage("First request", first);
  605. logUsage("Second request", second);
  606. assertTotalTokensEqualsComponents(first);
  607. assertTotalTokensEqualsComponents(second);
  608. },
  609. );
  610. });
  611. // =========================================================================
  612. // OpenAI Codex (OAuth)
  613. // =========================================================================
  614. describe("OpenAI Codex (OAuth)", () => {
  615. it.skipIf(!openaiCodexToken)(
  616. "gpt-5.5 - should return totalTokens equal to sum of components",
  617. { retry: 3, timeout: 60000 },
  618. async () => {
  619. const llm = getModel("openai-codex", "gpt-5.5");
  620. console.log(`\nOpenAI Codex / ${llm.id}:`);
  621. const { first, second } = await testTotalTokensWithCache(llm, { apiKey: openaiCodexToken });
  622. logUsage("First request", first);
  623. logUsage("Second request", second);
  624. assertTotalTokensEqualsComponents(first);
  625. assertTotalTokensEqualsComponents(second);
  626. },
  627. );
  628. });
  629. });