| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794 |
- /**
- * Test totalTokens field across all providers.
- *
- * totalTokens represents the total number of tokens processed by the LLM,
- * including input (with cache) and output (with thinking). This is the
- * base for calculating context size for the next request.
- *
- * - OpenAI Completions: Uses native total_tokens field
- * - OpenAI Responses: Uses native total_tokens field
- * - Google: Uses native totalTokenCount field
- * - Anthropic: Computed as input + output + cacheRead + cacheWrite
- * - Other OpenAI-compatible providers: Uses native total_tokens field
- */
- import { describe, expect, it } from "vitest";
- import { getModel } from "../src/models.ts";
- import { complete } from "../src/stream.ts";
- import type { Api, Context, Model, StreamOptions, Usage } from "../src/types.ts";
- type StreamOptionsWithExtras = StreamOptions & Record<string, unknown>;
- import { hasAzureOpenAICredentials, resolveAzureDeploymentName } from "./azure-utils.ts";
- import { hasBedrockCredentials } from "./bedrock-utils.ts";
- import { hasCloudflareAiGatewayCredentials, hasCloudflareWorkersAICredentials } from "./cloudflare-utils.ts";
- import { resolveApiKey } from "./oauth.ts";
- // Resolve OAuth tokens at module level (async, runs before tests)
- const oauthTokens = await Promise.all([
- resolveApiKey("anthropic"),
- resolveApiKey("github-copilot"),
- resolveApiKey("openai-codex"),
- ]);
- const [anthropicOAuthToken, githubCopilotToken, openaiCodexToken] = oauthTokens;
- // Generate a long system prompt to trigger caching (>2k bytes for most providers)
- const LONG_SYSTEM_PROMPT = `You are a helpful assistant. Be concise in your responses.
- Here is some additional context that makes this system prompt long enough to trigger caching:
- ${Array(50)
- .fill(
- "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.",
- )
- .join("\n\n")}
- Remember: Always be helpful and concise.`;
- async function testTotalTokensWithCache<TApi extends Api>(
- llm: Model<TApi>,
- options: StreamOptionsWithExtras = {},
- ): Promise<{ first: Usage; second: Usage }> {
- // First request - no cache
- const context1: Context = {
- systemPrompt: LONG_SYSTEM_PROMPT,
- messages: [
- {
- role: "user",
- content: "What is 2 + 2? Reply with just the number.",
- timestamp: Date.now(),
- },
- ],
- };
- const response1 = await complete(llm, context1, options);
- expect(response1.stopReason).toBe("stop");
- // Second request - should trigger cache read (same system prompt, add conversation)
- const context2: Context = {
- systemPrompt: LONG_SYSTEM_PROMPT,
- messages: [
- ...context1.messages,
- response1, // Include previous assistant response
- {
- role: "user",
- content: "What is 3 + 3? Reply with just the number.",
- timestamp: Date.now(),
- },
- ],
- };
- const response2 = await complete(llm, context2, options);
- expect(response2.stopReason).toBe("stop");
- return { first: response1.usage, second: response2.usage };
- }
- function logUsage(label: string, usage: Usage) {
- const computed = usage.input + usage.output + usage.cacheRead + usage.cacheWrite;
- console.log(` ${label}:`);
- console.log(
- ` input: ${usage.input}, output: ${usage.output}, cacheRead: ${usage.cacheRead}, cacheWrite: ${usage.cacheWrite}`,
- );
- console.log(` totalTokens: ${usage.totalTokens}, computed: ${computed}`);
- }
- function assertTotalTokensEqualsComponents(usage: Usage) {
- const computed = usage.input + usage.output + usage.cacheRead + usage.cacheWrite;
- expect(usage.totalTokens).toBe(computed);
- }
- describe("totalTokens field", () => {
- // =========================================================================
- // Anthropic
- // =========================================================================
- describe.skipIf(!process.env.ANTHROPIC_API_KEY)("Anthropic (API Key)", () => {
- it(
- "claude-sonnet-4-5 - should return totalTokens equal to sum of components",
- { retry: 3, timeout: 60000 },
- async () => {
- const llm = getModel("anthropic", "claude-sonnet-4-5");
- console.log(`\nAnthropic / ${llm.id}:`);
- const { first, second } = await testTotalTokensWithCache(llm, { apiKey: process.env.ANTHROPIC_API_KEY });
- logUsage("First request", first);
- logUsage("Second request", second);
- assertTotalTokensEqualsComponents(first);
- assertTotalTokensEqualsComponents(second);
- // Anthropic should have cache activity
- const hasCache = second.cacheRead > 0 || second.cacheWrite > 0 || first.cacheWrite > 0;
- expect(hasCache).toBe(true);
- },
- );
- });
- describe("Anthropic (OAuth)", () => {
- it.skipIf(!anthropicOAuthToken)(
- "claude-sonnet-4 - should return totalTokens equal to sum of components",
- { retry: 3, timeout: 60000 },
- async () => {
- const llm = getModel("anthropic", "claude-sonnet-4-6");
- console.log(`\nAnthropic OAuth / ${llm.id}:`);
- const { first, second } = await testTotalTokensWithCache(llm, { apiKey: anthropicOAuthToken });
- logUsage("First request", first);
- logUsage("Second request", second);
- assertTotalTokensEqualsComponents(first);
- assertTotalTokensEqualsComponents(second);
- // Anthropic should have cache activity
- const hasCache = second.cacheRead > 0 || second.cacheWrite > 0 || first.cacheWrite > 0;
- expect(hasCache).toBe(true);
- },
- );
- });
- // =========================================================================
- // OpenAI
- // =========================================================================
- describe.skipIf(!process.env.OPENAI_API_KEY)("OpenAI Completions", () => {
- it(
- "gpt-4o-mini - should return totalTokens equal to sum of components",
- { retry: 3, timeout: 60000 },
- async () => {
- const { compat: _compat, ...baseModel } = getModel("openai", "gpt-4o-mini")!;
- void _compat;
- const llm: Model<"openai-completions"> = {
- ...baseModel,
- api: "openai-completions",
- };
- console.log(`\nOpenAI Completions / ${llm.id}:`);
- const { first, second } = await testTotalTokensWithCache(llm);
- logUsage("First request", first);
- logUsage("Second request", second);
- assertTotalTokensEqualsComponents(first);
- assertTotalTokensEqualsComponents(second);
- },
- );
- });
- describe.skipIf(!process.env.OPENAI_API_KEY)("OpenAI Responses", () => {
- it(
- "claude-haiku-4.5 - should return totalTokens equal to sum of components",
- { retry: 3, timeout: 60000 },
- async () => {
- const llm = getModel("openai", "gpt-4o");
- console.log(`\nOpenAI Responses / ${llm.id}:`);
- const { first, second } = await testTotalTokensWithCache(llm);
- logUsage("First request", first);
- logUsage("Second request", second);
- assertTotalTokensEqualsComponents(first);
- assertTotalTokensEqualsComponents(second);
- },
- );
- });
- describe.skipIf(!hasAzureOpenAICredentials())("Azure OpenAI Responses", () => {
- it(
- "gpt-4o-mini - should return totalTokens equal to sum of components",
- { retry: 3, timeout: 60000 },
- async () => {
- const llm = getModel("azure-openai-responses", "gpt-4o-mini");
- const azureDeploymentName = resolveAzureDeploymentName(llm.id);
- const azureOptions = azureDeploymentName ? { azureDeploymentName } : {};
- console.log(`\nAzure OpenAI Responses / ${llm.id}:`);
- const { first, second } = await testTotalTokensWithCache(llm, azureOptions);
- logUsage("First request", first);
- logUsage("Second request", second);
- assertTotalTokensEqualsComponents(first);
- assertTotalTokensEqualsComponents(second);
- },
- );
- });
- // =========================================================================
- // Google
- // =========================================================================
- describe.skipIf(!process.env.GEMINI_API_KEY)("Google", () => {
- it(
- "gemini-2.0-flash - should return totalTokens equal to sum of components",
- { retry: 3, timeout: 60000 },
- async () => {
- const llm = getModel("google", "gemini-2.0-flash");
- console.log(`\nGoogle / ${llm.id}:`);
- const { first, second } = await testTotalTokensWithCache(llm);
- logUsage("First request", first);
- logUsage("Second request", second);
- assertTotalTokensEqualsComponents(first);
- assertTotalTokensEqualsComponents(second);
- },
- );
- });
- // =========================================================================
- // xAI
- // =========================================================================
- describe.skipIf(!process.env.XAI_API_KEY)("xAI", () => {
- it(
- "grok-3-fast - should return totalTokens equal to sum of components",
- { retry: 3, timeout: 60000 },
- async () => {
- const llm = getModel("xai", "grok-3-fast");
- console.log(`\nxAI / ${llm.id}:`);
- const { first, second } = await testTotalTokensWithCache(llm, { apiKey: process.env.XAI_API_KEY });
- logUsage("First request", first);
- logUsage("Second request", second);
- assertTotalTokensEqualsComponents(first);
- assertTotalTokensEqualsComponents(second);
- },
- );
- });
- // =========================================================================
- // Groq
- // =========================================================================
- describe.skipIf(!process.env.GROQ_API_KEY)("Groq", () => {
- it(
- "openai/gpt-oss-120b - should return totalTokens equal to sum of components",
- { retry: 3, timeout: 60000 },
- async () => {
- const llm = getModel("groq", "openai/gpt-oss-120b");
- console.log(`\nGroq / ${llm.id}:`);
- const { first, second } = await testTotalTokensWithCache(llm, { apiKey: process.env.GROQ_API_KEY });
- logUsage("First request", first);
- logUsage("Second request", second);
- assertTotalTokensEqualsComponents(first);
- assertTotalTokensEqualsComponents(second);
- },
- );
- });
- // =========================================================================
- // Cerebras
- // =========================================================================
- describe.skipIf(!process.env.CEREBRAS_API_KEY)("Cerebras", () => {
- it(
- "gpt-oss-120b - should return totalTokens equal to sum of components",
- { retry: 3, timeout: 60000 },
- async () => {
- const llm = getModel("cerebras", "gpt-oss-120b");
- console.log(`\nCerebras / ${llm.id}:`);
- const { first, second } = await testTotalTokensWithCache(llm, { apiKey: process.env.CEREBRAS_API_KEY });
- logUsage("First request", first);
- logUsage("Second request", second);
- assertTotalTokensEqualsComponents(first);
- assertTotalTokensEqualsComponents(second);
- },
- );
- });
- // =========================================================================
- // Cloudflare Workers AI
- // =========================================================================
- describe.skipIf(!hasCloudflareWorkersAICredentials())("Cloudflare Workers AI", () => {
- it(
- "@cf/moonshotai/kimi-k2.6 - should return totalTokens equal to sum of components",
- { retry: 3, timeout: 60000 },
- async () => {
- const llm = getModel("cloudflare-workers-ai", "@cf/moonshotai/kimi-k2.6");
- console.log(`\nCloudflare Workers AI / ${llm.id}:`);
- const { first, second } = await testTotalTokensWithCache(llm, {
- apiKey: process.env.CLOUDFLARE_API_KEY,
- });
- logUsage("First request", first);
- logUsage("Second request", second);
- assertTotalTokensEqualsComponents(first);
- assertTotalTokensEqualsComponents(second);
- },
- );
- });
- // =========================================================================
- // Cloudflare AI Gateway
- // =========================================================================
- describe.skipIf(!hasCloudflareAiGatewayCredentials())("Cloudflare AI Gateway", () => {
- it(
- "workers-ai/@cf/moonshotai/kimi-k2.6 - should return totalTokens equal to sum of components",
- { retry: 3, timeout: 60000 },
- async () => {
- const llm = getModel("cloudflare-ai-gateway", "workers-ai/@cf/moonshotai/kimi-k2.6");
- console.log(`\nCloudflare AI Gateway / ${llm.id}:`);
- const { first, second } = await testTotalTokensWithCache(llm, {
- apiKey: process.env.CLOUDFLARE_API_KEY,
- });
- logUsage("First request", first);
- logUsage("Second request", second);
- assertTotalTokensEqualsComponents(first);
- assertTotalTokensEqualsComponents(second);
- },
- );
- });
- // =========================================================================
- // Hugging Face
- // =========================================================================
- describe.skipIf(!process.env.HF_TOKEN)("Hugging Face", () => {
- it("Kimi-K2.5 - should return totalTokens equal to sum of components", { retry: 3, timeout: 60000 }, async () => {
- const llm = getModel("huggingface", "moonshotai/Kimi-K2.5");
- console.log(`\nHugging Face / ${llm.id}:`);
- const { first, second } = await testTotalTokensWithCache(llm, { apiKey: process.env.HF_TOKEN });
- logUsage("First request", first);
- logUsage("Second request", second);
- assertTotalTokensEqualsComponents(first);
- assertTotalTokensEqualsComponents(second);
- });
- });
- // =========================================================================
- // Together AI
- // =========================================================================
- describe.skipIf(!process.env.TOGETHER_API_KEY)("Together AI", () => {
- it("Kimi-K2.6 - should return totalTokens equal to sum of components", { retry: 3, timeout: 60000 }, async () => {
- const llm = getModel("together", "moonshotai/Kimi-K2.6");
- console.log(`\nTogether AI / ${llm.id}:`);
- const { first, second } = await testTotalTokensWithCache(llm, {
- apiKey: process.env.TOGETHER_API_KEY,
- reasoningEffort: "high",
- });
- logUsage("First request", first);
- logUsage("Second request", second);
- assertTotalTokensEqualsComponents(first);
- assertTotalTokensEqualsComponents(second);
- });
- });
- // =========================================================================
- // z.ai
- // =========================================================================
- describe.skipIf(!process.env.ZAI_API_KEY)("z.ai", () => {
- it(
- "glm-4.5-air - should return totalTokens equal to sum of components",
- { retry: 3, timeout: 60000 },
- async () => {
- const llm = getModel("zai", "glm-4.5-air");
- console.log(`\nz.ai / ${llm.id}:`);
- const { first, second } = await testTotalTokensWithCache(llm, { apiKey: process.env.ZAI_API_KEY });
- logUsage("First request", first);
- logUsage("Second request", second);
- assertTotalTokensEqualsComponents(first);
- assertTotalTokensEqualsComponents(second);
- },
- );
- });
- // =========================================================================
- // Mistral
- // =========================================================================
- describe.skipIf(!process.env.MISTRAL_API_KEY)("Mistral", () => {
- it(
- "devstral-medium-latest - should return totalTokens equal to sum of components",
- { retry: 3, timeout: 60000 },
- async () => {
- const llm = getModel("mistral", "devstral-medium-latest");
- console.log(`\nMistral / ${llm.id}:`);
- const { first, second } = await testTotalTokensWithCache(llm, { apiKey: process.env.MISTRAL_API_KEY });
- logUsage("First request", first);
- logUsage("Second request", second);
- assertTotalTokensEqualsComponents(first);
- assertTotalTokensEqualsComponents(second);
- },
- );
- });
- // =========================================================================
- // MiniMax
- // =========================================================================
- describe.skipIf(!process.env.MINIMAX_API_KEY)("MiniMax", () => {
- it(
- "MiniMax-M2.7 - should return totalTokens equal to sum of components",
- { retry: 3, timeout: 60000 },
- async () => {
- const llm = getModel("minimax", "MiniMax-M2.7");
- console.log(`\nMiniMax / ${llm.id}:`);
- const { first, second } = await testTotalTokensWithCache(llm, { apiKey: process.env.MINIMAX_API_KEY });
- logUsage("First request", first);
- logUsage("Second request", second);
- assertTotalTokensEqualsComponents(first);
- assertTotalTokensEqualsComponents(second);
- },
- );
- });
- // =========================================================================
- // Xiaomi MiMo
- // =========================================================================
- describe.skipIf(!process.env.XIAOMI_API_KEY)("Xiaomi MiMo (API billing)", () => {
- it(
- "mimo-v2.5-pro - should return totalTokens equal to sum of components",
- { retry: 3, timeout: 60000 },
- async () => {
- const llm = getModel("xiaomi", "mimo-v2.5-pro");
- console.log(`\nXiaomi MiMo / ${llm.id}:`);
- const { first, second } = await testTotalTokensWithCache(llm, { apiKey: process.env.XIAOMI_API_KEY });
- logUsage("First request", first);
- logUsage("Second request", second);
- assertTotalTokensEqualsComponents(first);
- assertTotalTokensEqualsComponents(second);
- },
- );
- });
- // =========================================================================
- // Xiaomi MiMo Token Plan CN
- // =========================================================================
- describe.skipIf(!process.env.XIAOMI_TOKEN_PLAN_CN_API_KEY)("Xiaomi MiMo Token Plan (CN)", () => {
- it(
- "mimo-v2.5-pro - should return totalTokens equal to sum of components",
- { retry: 3, timeout: 60000 },
- async () => {
- const llm = getModel("xiaomi-token-plan-cn", "mimo-v2.5-pro");
- console.log(`\nXiaomi MiMo Token Plan CN / ${llm.id}:`);
- const { first, second } = await testTotalTokensWithCache(llm, {
- apiKey: process.env.XIAOMI_TOKEN_PLAN_CN_API_KEY,
- });
- logUsage("First request", first);
- logUsage("Second request", second);
- assertTotalTokensEqualsComponents(first);
- assertTotalTokensEqualsComponents(second);
- },
- );
- });
- // =========================================================================
- // Xiaomi MiMo Token Plan AMS
- // =========================================================================
- describe.skipIf(!process.env.XIAOMI_TOKEN_PLAN_AMS_API_KEY)("Xiaomi MiMo Token Plan (AMS)", () => {
- it(
- "mimo-v2.5-pro - should return totalTokens equal to sum of components",
- { retry: 3, timeout: 60000 },
- async () => {
- const llm = getModel("xiaomi-token-plan-ams", "mimo-v2.5-pro");
- console.log(`\nXiaomi MiMo Token Plan AMS / ${llm.id}:`);
- const { first, second } = await testTotalTokensWithCache(llm, {
- apiKey: process.env.XIAOMI_TOKEN_PLAN_AMS_API_KEY,
- });
- logUsage("First request", first);
- logUsage("Second request", second);
- assertTotalTokensEqualsComponents(first);
- assertTotalTokensEqualsComponents(second);
- },
- );
- });
- // =========================================================================
- // Xiaomi MiMo Token Plan SGP
- // =========================================================================
- describe.skipIf(!process.env.XIAOMI_TOKEN_PLAN_SGP_API_KEY)("Xiaomi MiMo Token Plan (SGP)", () => {
- it(
- "mimo-v2.5-pro - should return totalTokens equal to sum of components",
- { retry: 3, timeout: 60000 },
- async () => {
- const llm = getModel("xiaomi-token-plan-sgp", "mimo-v2.5-pro");
- console.log(`\nXiaomi MiMo Token Plan SGP / ${llm.id}:`);
- const { first, second } = await testTotalTokensWithCache(llm, {
- apiKey: process.env.XIAOMI_TOKEN_PLAN_SGP_API_KEY,
- });
- logUsage("First request", first);
- logUsage("Second request", second);
- assertTotalTokensEqualsComponents(first);
- assertTotalTokensEqualsComponents(second);
- },
- );
- });
- // =========================================================================
- // Kimi For Coding
- // =========================================================================
- describe.skipIf(!process.env.KIMI_API_KEY)("Kimi For Coding", () => {
- it(
- "kimi-k2-thinking - should return totalTokens equal to sum of components",
- { retry: 3, timeout: 60000 },
- async () => {
- const llm = getModel("kimi-coding", "kimi-k2-thinking");
- console.log(`\nKimi For Coding / ${llm.id}:`);
- const { first, second } = await testTotalTokensWithCache(llm, { apiKey: process.env.KIMI_API_KEY });
- logUsage("First request", first);
- logUsage("Second request", second);
- assertTotalTokensEqualsComponents(first);
- assertTotalTokensEqualsComponents(second);
- },
- );
- });
- // =========================================================================
- // Vercel AI Gateway
- // =========================================================================
- describe.skipIf(!process.env.AI_GATEWAY_API_KEY)("Vercel AI Gateway", () => {
- it(
- "google/gemini-2.5-flash - should return totalTokens equal to sum of components",
- { retry: 3, timeout: 60000 },
- async () => {
- const llm = getModel("vercel-ai-gateway", "google/gemini-2.5-flash");
- console.log(`\nVercel AI Gateway / ${llm.id}:`);
- const { first, second } = await testTotalTokensWithCache(llm, { apiKey: process.env.AI_GATEWAY_API_KEY });
- logUsage("First request", first);
- logUsage("Second request", second);
- assertTotalTokensEqualsComponents(first);
- assertTotalTokensEqualsComponents(second);
- },
- );
- });
- // =========================================================================
- // OpenRouter - Multiple backend providers
- // =========================================================================
- describe.skipIf(!process.env.OPENROUTER_API_KEY)("OpenRouter", () => {
- it(
- "anthropic/claude-sonnet-4 - should return totalTokens equal to sum of components",
- { retry: 3, timeout: 60000 },
- async () => {
- const llm = getModel("openrouter", "anthropic/claude-sonnet-4");
- console.log(`\nOpenRouter / ${llm.id}:`);
- const { first, second } = await testTotalTokensWithCache(llm, { apiKey: process.env.OPENROUTER_API_KEY });
- logUsage("First request", first);
- logUsage("Second request", second);
- assertTotalTokensEqualsComponents(first);
- assertTotalTokensEqualsComponents(second);
- },
- );
- it(
- "deepseek/deepseek-chat - should return totalTokens equal to sum of components",
- { retry: 3, timeout: 60000 },
- async () => {
- const llm = getModel("openrouter", "deepseek/deepseek-chat");
- console.log(`\nOpenRouter / ${llm.id}:`);
- const { first, second } = await testTotalTokensWithCache(llm, { apiKey: process.env.OPENROUTER_API_KEY });
- logUsage("First request", first);
- logUsage("Second request", second);
- assertTotalTokensEqualsComponents(first);
- assertTotalTokensEqualsComponents(second);
- },
- );
- it(
- "mistralai/mistral-small-3.2-24b-instruct - should return totalTokens equal to sum of components",
- { retry: 3, timeout: 60000 },
- async () => {
- const llm = getModel("openrouter", "mistralai/mistral-small-3.2-24b-instruct");
- console.log(`\nOpenRouter / ${llm.id}:`);
- const { first, second } = await testTotalTokensWithCache(llm, { apiKey: process.env.OPENROUTER_API_KEY });
- logUsage("First request", first);
- logUsage("Second request", second);
- assertTotalTokensEqualsComponents(first);
- assertTotalTokensEqualsComponents(second);
- },
- );
- it(
- "google/gemini-2.5-flash - should return totalTokens equal to sum of components",
- { retry: 3, timeout: 60000 },
- async () => {
- const llm = getModel("openrouter", "google/gemini-2.5-flash");
- console.log(`\nOpenRouter / ${llm.id}:`);
- const { first, second } = await testTotalTokensWithCache(llm, { apiKey: process.env.OPENROUTER_API_KEY });
- logUsage("First request", first);
- logUsage("Second request", second);
- assertTotalTokensEqualsComponents(first);
- assertTotalTokensEqualsComponents(second);
- },
- );
- it(
- "deepseek/deepseek-chat - should return totalTokens equal to sum of components",
- { retry: 3, timeout: 60000 },
- async () => {
- const llm = getModel("openrouter", "deepseek/deepseek-chat");
- console.log(`\nOpenRouter / ${llm.id}:`);
- const { first, second } = await testTotalTokensWithCache(llm, { apiKey: process.env.OPENROUTER_API_KEY });
- logUsage("First request", first);
- logUsage("Second request", second);
- assertTotalTokensEqualsComponents(first);
- assertTotalTokensEqualsComponents(second);
- },
- );
- });
- // =========================================================================
- // GitHub Copilot (OAuth)
- // =========================================================================
- describe("GitHub Copilot (OAuth)", () => {
- it.skipIf(!githubCopilotToken)(
- "claude-haiku-4.5 - should return totalTokens equal to sum of components",
- { retry: 3, timeout: 60000 },
- async () => {
- const llm = getModel("github-copilot", "claude-haiku-4.5");
- console.log(`\nGitHub Copilot / ${llm.id}:`);
- const { first, second } = await testTotalTokensWithCache(llm, { apiKey: githubCopilotToken });
- logUsage("First request", first);
- logUsage("Second request", second);
- assertTotalTokensEqualsComponents(first);
- assertTotalTokensEqualsComponents(second);
- },
- );
- it.skipIf(!githubCopilotToken)(
- "claude-sonnet-4 - should return totalTokens equal to sum of components",
- { retry: 3, timeout: 60000 },
- async () => {
- const llm = getModel("github-copilot", "claude-sonnet-4.6");
- console.log(`\nGitHub Copilot / ${llm.id}:`);
- const { first, second } = await testTotalTokensWithCache(llm, { apiKey: githubCopilotToken });
- logUsage("First request", first);
- logUsage("Second request", second);
- assertTotalTokensEqualsComponents(first);
- assertTotalTokensEqualsComponents(second);
- },
- );
- });
- // =========================================================================
- // =========================================================================
- // =========================================================================
- // =========================================================================
- describe.skipIf(!hasBedrockCredentials())("Amazon Bedrock", () => {
- it(
- "claude-sonnet-4-5 - should return totalTokens equal to sum of components",
- { retry: 3, timeout: 60000 },
- async () => {
- const llm = getModel("amazon-bedrock", "global.anthropic.claude-sonnet-4-5-20250929-v1:0");
- console.log(`\nAmazon Bedrock / ${llm.id}:`);
- const { first, second } = await testTotalTokensWithCache(llm);
- logUsage("First request", first);
- logUsage("Second request", second);
- assertTotalTokensEqualsComponents(first);
- assertTotalTokensEqualsComponents(second);
- },
- );
- });
- // =========================================================================
- // OpenAI Codex (OAuth)
- // =========================================================================
- describe("OpenAI Codex (OAuth)", () => {
- it.skipIf(!openaiCodexToken)(
- "gpt-5.5 - should return totalTokens equal to sum of components",
- { retry: 3, timeout: 60000 },
- async () => {
- const llm = getModel("openai-codex", "gpt-5.5");
- console.log(`\nOpenAI Codex / ${llm.id}:`);
- const { first, second } = await testTotalTokensWithCache(llm, { apiKey: openaiCodexToken });
- logUsage("First request", first);
- logUsage("Second request", second);
- assertTotalTokensEqualsComponents(first);
- assertTotalTokensEqualsComponents(second);
- },
- );
- });
- });
|