Never stop coding. Free MIT AI gateway: one endpoint, 375 providers (150+ free), 1,400+ models Kimi, Claude, GPT, Gemini, GLM, DeepSeek, MiniMax. Works with Claude Code, Codex, Cursor, OpenCode, Cline & Copilot. Quota-aware auto-fallback, RTK+Caveman compression saves 15-95% tokens, MCP/A2A, Desktop/PWA. Built by 900+ contributors
TypeScript a2aai-agentsai-gatewayanthropicclaudeclaude-codeclinecodexcopilotcursordeepseekfree-aigeminikimillm-gatewaymcpopenaiopenai-proxyqwentoken-saver
feat(inspector): minimal cost estimate table for LlmDetailsTab (R5-11)
diegosouzapwe32abf580225
diff --git a/src/mitm/inspector/llmMetadataExtractor.ts b/src/mitm/inspector/llmMetadataExtractor.tsindex 97209513db..3a0c46c60c 100644--- a/src/mitm/inspector/llmMetadataExtractor.ts+++ b/src/mitm/inspector/llmMetadataExtractor.ts@@ -4,10 +4,11 @@* is host- and path-based; token counts come from the upstream `usage` block.** Replaces the stub `extractLlmMetadata` left in `kindDetector.ts` by F1.- * Cost estimation is deferred to a future audit pass; we return `null`.+ * Cost estimation uses the minimal pricing table in `pricing.ts` (R5-11).*/import { detectKind } from "./kindDetector.ts";+import { estimateCost } from "./pricing.ts";import { mergeStream, parseSseStream } from "./sseMerger.ts";import type { InterceptedRequest, LlmMetadata } from "./types.ts";@@ -177,6 +178,6 @@ export function extractLlmMetadata(req: InterceptedRequest): LlmMetadata | nulltokensOut,streamed,mappedTo,- costEstimateUsd: null, // cost table out of scope for F4+ costEstimateUsd: estimateCost(model, tokensIn, tokensOut),};}diff --git a/src/mitm/inspector/pricing.ts b/src/mitm/inspector/pricing.tsnew file mode 100644index 0000000000..5f55e2978b--- /dev/null+++ b/src/mitm/inspector/pricing.ts@@ -0,0 +1,57 @@+/**+ * Minimal pricing table for cost estimation in Traffic Inspector LlmDetailsTab.+ * Values in USD per 1M tokens. Approximate — providers update prices without notice;+ * users with strict cost requirements should override via env or DB settings.+ *+ * Lookup is a case-insensitive substring match so versioned model IDs+ * (e.g. "claude-3-5-sonnet-20240620") resolve against the canonical short key.+ */++export interface ModelPricing {+ inputPerMTok: number;+ outputPerMTok: number;+}++export const PRICING_TABLE: Record<string, ModelPricing> = {+ "gpt-4o-mini": { inputPerMTok: 0.15, outputPerMTok: 0.60 },+ "gpt-4o": { inputPerMTok: 2.50, outputPerMTok: 10.00 },+ "claude-3-5-sonnet": { inputPerMTok: 3.00, outputPerMTok: 15.00 },+ "claude-3-5-haiku": { inputPerMTok: 0.80, outputPerMTok: 4.00 },+ "claude-3-opus": { inputPerMTok: 15.00, outputPerMTok: 75.00 },+ "gemini-2.0-flash": { inputPerMTok: 0.10, outputPerMTok: 0.40 },+ "gemini-1.5-flash": { inputPerMTok: 0.075, outputPerMTok: 0.30 },+ "gemini-1.5-pro": { inputPerMTok: 1.25, outputPerMTok: 5.00 },+ "deepseek-reasoner": { inputPerMTok: 0.55, outputPerMTok: 2.19 },+ "deepseek-chat": { inputPerMTok: 0.27, outputPerMTok: 1.10 },+};++/**+ * Lookup pricing for a model id — case-insensitive substring match against+ * the canonical keys (longest-specific-first ordering matters: "gpt-4o-mini"+ * is listed before "gpt-4o" so it matches first). Returns null if no match.+ */+export function lookupPricing(model: string | null): ModelPricing | null {+ if (!model) return null;+ const m = model.toLowerCase();+ for (const [key, price] of Object.entries(PRICING_TABLE)) {+ if (m.includes(key)) return price;+ }+ return null;+}++/**+ * Estimate cost in USD given input/output token counts. Returns null when+ * both token counts are null or the model has no entry in PRICING_TABLE.+ */+export function estimateCost(+ model: string | null,+ tokensIn: number | null,+ tokensOut: number | null,+): number | null {+ if (tokensIn == null && tokensOut == null) return null;+ const price = lookupPricing(model);+ if (!price) return null;+ const inCost = ((tokensIn ?? 0) / 1_000_000) * price.inputPerMTok;+ const outCost = ((tokensOut ?? 0) / 1_000_000) * price.outputPerMTok;+ return Number((inCost + outCost).toFixed(6));+}diff --git a/tests/unit/inspector-llm-metadata.test.ts b/tests/unit/inspector-llm-metadata.test.tsindex babb1f14c5..a68b2c602b 100644--- a/tests/unit/inspector-llm-metadata.test.ts+++ b/tests/unit/inspector-llm-metadata.test.ts@@ -87,6 +87,48 @@ test("extracts tokensIn/tokensOut from prompt_tokens/completion_tokens", () => {assert.equal(meta.tokensOut, 25);});+test("computes costEstimateUsd for gpt-4o with token counts", () => {+ const req = makeReq({+ requestBody: JSON.stringify({ model: "gpt-4o", messages: [] }),+ responseBody: JSON.stringify({+ usage: { prompt_tokens: 1_000_000, completion_tokens: 100_000 },+ }),+ });+ const meta = extractLlmMetadata(req);+ assert.ok(meta);+ assert.equal(meta.tokensIn, 1_000_000);+ assert.equal(meta.tokensOut, 100_000);+ // 1M*2.50/1M + 100k*10.00/1M = 2.50 + 1.00 = 3.50+ assert.equal(meta.costEstimateUsd, 3.50);+});++test("computes costEstimateUsd for claude-3-5-sonnet with token counts", () => {+ const req = makeReq({+ host: "api.anthropic.com",+ path: "/v1/messages",+ requestBody: JSON.stringify({ model: "claude-3-5-sonnet-20240620", messages: [] }),+ responseBody: JSON.stringify({+ usage: { input_tokens: 500_000, output_tokens: 200_000 },+ }),+ });+ const meta = extractLlmMetadata(req);+ assert.ok(meta);+ // 500k*3.00/1M + 200k*15.00/1M = 1.50 + 3.00 = 4.50+ assert.equal(meta.costEstimateUsd, 4.50);+});++test("costEstimateUsd is null for unknown model", () => {+ const req = makeReq({+ requestBody: JSON.stringify({ model: "unknown-model-xyz", messages: [] }),+ responseBody: JSON.stringify({+ usage: { prompt_tokens: 100, completion_tokens: 50 },+ }),+ });+ const meta = extractLlmMetadata(req);+ assert.ok(meta);+ assert.equal(meta.costEstimateUsd, null);+});+test("extracts tokensIn/tokensOut from input_tokens/output_tokens (Anthropic)", () => {const req = makeReq({host: "api.anthropic.com",diff --git a/tests/unit/inspector-pricing.test.ts b/tests/unit/inspector-pricing.test.tsnew file mode 100644index 0000000000..0e0f818a36--- /dev/null+++ b/tests/unit/inspector-pricing.test.ts@@ -0,0 +1,63 @@+import test from "node:test";+import assert from "node:assert/strict";+import { lookupPricing, estimateCost } from "../../src/mitm/inspector/pricing.ts";++test("lookupPricing - exact key gpt-4o", () => {+ const p = lookupPricing("gpt-4o");+ assert.ok(p);+ assert.equal(p.inputPerMTok, 2.50);+ assert.equal(p.outputPerMTok, 10.00);+});++test("lookupPricing - versioned claude id matches prefix", () => {+ const p = lookupPricing("claude-3-5-sonnet-20240620");+ assert.ok(p);+ assert.equal(p.inputPerMTok, 3.00);+ assert.equal(p.outputPerMTok, 15.00);+});++test("lookupPricing - unknown model returns null", () => {+ assert.equal(lookupPricing("unknown-model-xyz"), null);+});++test("lookupPricing - null model returns null", () => {+ assert.equal(lookupPricing(null), null);+});++test("lookupPricing - gpt-4o-mini matches before gpt-4o (ordering)", () => {+ const p = lookupPricing("gpt-4o-mini");+ assert.ok(p);+ assert.equal(p.inputPerMTok, 0.15);+});++test("estimateCost - gpt-4o with 1M input + 100k output = 3.50", () => {+ const cost = estimateCost("gpt-4o", 1_000_000, 100_000);+ assert.ok(cost !== null);+ // 1M * 2.50/1M + 100k * 10.00/1M = 2.50 + 1.00 = 3.50+ assert.equal(cost, 3.50);+});++test("estimateCost - null model returns null", () => {+ assert.equal(estimateCost(null, 100, 100), null);+});++test("estimateCost - both token counts null returns null", () => {+ assert.equal(estimateCost("gpt-4o", null, null), null);+});++test("estimateCost - zero tokens returns 0", () => {+ const cost = estimateCost("gpt-4o", 0, 0);+ assert.ok(cost !== null);+ assert.equal(cost, 0);+});++test("estimateCost - only tokensIn provided (tokensOut null)", () => {+ // 1000 input tokens at gpt-4o-mini $0.15/1M => 0.00015+ const cost = estimateCost("gpt-4o-mini", 1000, null);+ assert.ok(cost !== null);+ assert.equal(cost, 0.00015);+});++test("estimateCost - unknown model returns null even with tokens", () => {+ assert.equal(estimateCost("llama-99-unknown", 5000, 2000), null);+});