The fastest, litest AI Gateway. Rust core with Python SDK. Call 100+ LLM APIs in OpenAI (or native) format with cost tracking, guardrails, load balancing, and logging [Bedrock, Azure, OpenAI, Anthropic, OpenAI, VertexAI, vLLM, Nvidia NIM]
Python ai-gatewayanthropicazure-openaibedrockgatewaylangchainlitellmllmllm-gatewayllmopsmcp-gatewayopenaiopenai-proxyrustrust-aivertex-ai
fix(ui): drop blank streamed costs instead of reading them as zero
Kerry Lu3aa9267df278
diff --git a/ui/litellm-dashboard/src/components/llm_calls/chat_completion.test.tsx b/ui/litellm-dashboard/src/components/llm_calls/chat_completion.test.tsxindex 129d51d50a2..71f36bdb6db 100644--- a/ui/litellm-dashboard/src/components/llm_calls/chat_completion.test.tsx+++ b/ui/litellm-dashboard/src/components/llm_calls/chat_completion.test.tsx@@ -472,6 +472,12 @@ describe("chat_completion prompt cache usage", () => {expect(usageData).toEqual(expect.not.objectContaining({ cost: expect.anything() }));});++ it("omits cost when the provider reports a blank value", async () => {+ const usageData = await captureUsage({ cost: " " });++ expect(usageData).toEqual(expect.not.objectContaining({ cost: expect.anything() }));+ });});describe("chat_completion response cache", () => {diff --git a/ui/litellm-dashboard/src/components/llm_calls/chat_completion.tsx b/ui/litellm-dashboard/src/components/llm_calls/chat_completion.tsxindex c12a2c3cfb4..ffa2877fbd9 100644--- a/ui/litellm-dashboard/src/components/llm_calls/chat_completion.tsx+++ b/ui/litellm-dashboard/src/components/llm_calls/chat_completion.tsx@@ -5,6 +5,7 @@ import { VectorStoreSearchResponse } from "../chat_ui/types";import { getProxyBaseUrl } from "@/components/networking";import { MCPServer, MCPToolset, type MCPEvent } from "@/components/mcp_tools/types";import { extractPromptCacheTokens } from "@/utils/promptCacheUsage";+import { parseUsageCost } from "./usage_cost";const completionAsSingleChunk = (completion: ChatCompletion): ChatCompletionChunk =>({@@ -243,12 +244,9 @@ export async function makeOpenAIChatCompletionRequest(usageData.reasoningTokens = chunkWithUsage.usage.completion_tokens_details.reasoning_tokens;}- // Extract cost from usage object if available- if (chunkWithUsage.usage.cost !== undefined && chunkWithUsage.usage.cost !== null) {- const parsedCost = Number(chunkWithUsage.usage.cost);- if (Number.isFinite(parsedCost)) {- usageData.cost = parsedCost;- }+ const parsedCost = parseUsageCost(chunkWithUsage.usage.cost);+ if (parsedCost !== undefined) {+ usageData.cost = parsedCost;}onUsageData(usageData);diff --git a/ui/litellm-dashboard/src/components/llm_calls/responses_api.test.tsx b/ui/litellm-dashboard/src/components/llm_calls/responses_api.test.tsxindex 7b13f0ac2e3..b55df89d5cc 100644--- a/ui/litellm-dashboard/src/components/llm_calls/responses_api.test.tsx+++ b/ui/litellm-dashboard/src/components/llm_calls/responses_api.test.tsx@@ -260,6 +260,35 @@ describe("responses_api", () => {expect(onUsageData).toHaveBeenCalledWith(expect.not.objectContaining({ cost: expect.anything() }), "");});+ it("should omit cost when the proxy reports a blank cost", async () => {+ async function* streamWithBlankCost() {+ yield {+ type: "response.completed",+ response: {+ id: "resp_blank_cost",+ usage: { output_tokens: 12, input_tokens: 12, total_tokens: 24, cost: " " },+ },+ };+ }+ mockResponsesCreate.mockResolvedValueOnce(streamWithBlankCost());++ const onUsageData = vi.fn();++ await makeOpenAIResponsesRequest(+ messages,+ mockUpdateTextUI,+ "gpt-4",+ "test-token",+ undefined,+ undefined,+ undefined,+ undefined,+ onUsageData,+ );++ expect(onUsageData).toHaveBeenCalledWith(expect.not.objectContaining({ cost: expect.anything() }), "");+ });+it("should replay MCP output items as events for a non-streaming response", async () => {mockResponsesCreate.mockReturnValueOnce(nonStreamingResponse({diff --git a/ui/litellm-dashboard/src/components/llm_calls/responses_api.tsx b/ui/litellm-dashboard/src/components/llm_calls/responses_api.tsxindex 63085a81b94..ce54c7c6b40 100644--- a/ui/litellm-dashboard/src/components/llm_calls/responses_api.tsx+++ b/ui/litellm-dashboard/src/components/llm_calls/responses_api.tsx@@ -4,6 +4,7 @@ import { TokenUsage } from "../chat_ui/ResponseMetrics";import { getProxyBaseUrl } from "@/components/networking";import { toast } from "@/lib/toast";import { extractPromptCacheTokens } from "@/utils/promptCacheUsage";+import { parseUsageCost } from "./usage_cost";import type { MCPEvent } from "@/components/mcp_tools/types";import { MCPServer, MCPToolset } from "@/components/mcp_tools/types";import {@@ -311,11 +312,9 @@ export async function makeOpenAIResponsesRequest(usageData.reasoningTokens = reasoningTokens;}- if (usage.cost !== undefined && usage.cost !== null) {- const parsedCost = Number(usage.cost);- if (Number.isFinite(parsedCost)) {- usageData.cost = parsedCost;- }+ const parsedCost = parseUsageCost(usage.cost);+ if (parsedCost !== undefined) {+ usageData.cost = parsedCost;}onUsageData(usageData, mcpToolUsed);diff --git a/ui/litellm-dashboard/src/components/llm_calls/usage_cost.test.ts b/ui/litellm-dashboard/src/components/llm_calls/usage_cost.test.tsnew file mode 100644index 00000000000..ee1021c0629--- /dev/null+++ b/ui/litellm-dashboard/src/components/llm_calls/usage_cost.test.ts@@ -0,0 +1,39 @@+import { describe, expect, it } from "vitest";+import { parseUsageCost } from "./usage_cost";++describe("parseUsageCost", () => {+ it("keeps finite numbers, including zero", () => {+ expect(parseUsageCost(0)).toBe(0);+ expect(parseUsageCost(0.000063)).toBe(0.000063);+ });++ it("keeps numeric strings", () => {+ expect(parseUsageCost("0.00019")).toBe(0.00019);+ expect(parseUsageCost(" 0.00019 ")).toBe(0.00019);+ });++ it("drops blank strings instead of fabricating a zero cost", () => {+ expect(parseUsageCost("")).toBeUndefined();+ expect(parseUsageCost(" ")).toBeUndefined();+ expect(parseUsageCost("\t\n")).toBeUndefined();+ });++ it("drops strings with a numeric prefix instead of truncating them", () => {+ expect(parseUsageCost("1oops")).toBeUndefined();+ expect(parseUsageCost("0.5 USD")).toBeUndefined();+ });++ it("drops non-finite numbers", () => {+ expect(parseUsageCost(Number.NaN)).toBeUndefined();+ expect(parseUsageCost(Number.POSITIVE_INFINITY)).toBeUndefined();+ });++ it("drops values that are not numbers or strings", () => {+ expect(parseUsageCost(null)).toBeUndefined();+ expect(parseUsageCost(undefined)).toBeUndefined();+ expect(parseUsageCost(true)).toBeUndefined();+ expect(parseUsageCost([])).toBeUndefined();+ expect(parseUsageCost(["0.5"])).toBeUndefined();+ expect(parseUsageCost({ total_cost: 0.5 })).toBeUndefined();+ });+});diff --git a/ui/litellm-dashboard/src/components/llm_calls/usage_cost.ts b/ui/litellm-dashboard/src/components/llm_calls/usage_cost.tsnew file mode 100644index 00000000000..79f56dcb2df--- /dev/null+++ b/ui/litellm-dashboard/src/components/llm_calls/usage_cost.ts@@ -0,0 +1,23 @@+/**+ * Providers and upstream gateways report `usage.cost` unvalidated: it arrives as a number, a numeric+ * string, an empty string, or something non-numeric. A cost that does not resolve to a finite number+ * must be dropped rather than coerced, because `NaN` survives `JSON.stringify` as `null` and crashes+ * the metrics row on the next load.+ */+export function parseUsageCost(rawCost: unknown): number | undefined {+ if (typeof rawCost === "number") {+ return Number.isFinite(rawCost) ? rawCost : undefined;+ }++ if (typeof rawCost !== "string") {+ return undefined;+ }++ const trimmed = rawCost.trim();+ if (trimmed === "") {+ return undefined;+ }++ const parsed = Number(trimmed);+ return Number.isFinite(parsed) ? parsed : undefined;+}