Skip to content

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]

feat(cost-optimization): sortable cache leakage columns and clearer token column name

Tin Chi Lo090fd491d4b6
diff --git a/ui/litellm-dashboard/src/app/(dashboard)/cost-optimization/_components/CacheLeakageCard.test.tsx b/ui/litellm-dashboard/src/app/(dashboard)/cost-optimization/_components/CacheLeakageCard.test.tsx
index bb40c341786..07e5e4edf50 100644
--- a/ui/litellm-dashboard/src/app/(dashboard)/cost-optimization/_components/CacheLeakageCard.test.tsx
+++ b/ui/litellm-dashboard/src/app/(dashboard)/cost-optimization/_components/CacheLeakageCard.test.tsx
@@ -91,6 +91,32 @@ describe("CacheLeakageCard", () => {
].forEach((info) => expect(getByLabelText(info)).toBeInTheDocument());
});
+ it("sorts by the clicked column, worst cache hit rate first", () => {
+ const { getAllByRole, getByText } = renderWith([
+ dayWithKeys("2026-07-12", {
+ "hash-a": key("alpha", {
+ prompt_tokens: 10000,
+ cache_read_input_tokens: 9000,
+ prompt_caching_savings_spend: 9.0,
+ }),
+ "hash-b": key("bravo", {
+ prompt_tokens: 500,
+ cache_read_input_tokens: 50,
+ prompt_caching_savings_spend: 0.05,
+ }),
+ }),
+ ]);
+ const firstDataRow = () => getAllByRole("row")[1];
+
+ expect(firstDataRow()).toHaveTextContent("alpha");
+
+ fireEvent.click(getByText("Cache hit rate"));
+ expect(firstDataRow()).toHaveTextContent("bravo");
+
+ fireEvent.click(getByText("Cache hit rate"));
+ expect(firstDataRow()).toHaveTextContent("alpha");
+ });
+
it("switches to the model view and lists only Anthropic models", () => {
const { getByText, queryByText } = renderWith([
dayWithModels("2026-07-12", {
diff --git a/ui/litellm-dashboard/src/app/(dashboard)/cost-optimization/_components/CacheLeakageCard.tsx b/ui/litellm-dashboard/src/app/(dashboard)/cost-optimization/_components/CacheLeakageCard.tsx
index ad1ff303dbc..bd0ecea9483 100644
--- a/ui/litellm-dashboard/src/app/(dashboard)/cost-optimization/_components/CacheLeakageCard.tsx
+++ b/ui/litellm-dashboard/src/app/(dashboard)/cost-optimization/_components/CacheLeakageCard.tsx
@@ -1,7 +1,7 @@
"use client";
import React, { useMemo, useState } from "react";
-import { Info } from "lucide-react";
+import { ArrowDown, ArrowUp, ArrowUpDown, Info } from "lucide-react";
import AdvancedDatePicker from "@/components/shared/advanced_date_picker";
import { Card, CardContent, CardHeader, CardTitle } from "@/components/ui/card";
@@ -9,29 +9,90 @@ import { Table, TableBody, TableCell, TableHead, TableHeader, TableRow } from "@
import { Tabs, TabsList, TabsTrigger } from "@/components/ui/tabs";
import { Tooltip, TooltipContent, TooltipProvider, TooltipTrigger } from "@/components/ui/tooltip";
import { formatNumberWithCommas } from "@/utils/dataUtils";
-import { CacheLeakageDimension, computeCacheLeakage, pct, usd } from "./costOptimizationUtils";
+import { CacheLeakageDimension, CacheLeakageRow, computeCacheLeakage, pct, usd } from "./costOptimizationUtils";
import { DailyActivityRange } from "./useDailyActivityRange";
interface CacheLeakageCardProps {
activity: DailyActivityRange;
}
-const HeadWithInfo = ({ label, info }: { label: string; info: string }) => (
- <span className="inline-flex items-center gap-1">
- {label}
- <Tooltip>
- <TooltipTrigger render={<span className="inline-flex" aria-label={info} />}>
- <Info className="h-3 w-3 text-gray-400" />
- </TooltipTrigger>
- <TooltipContent className="max-w-xs">{info}</TooltipContent>
- </Tooltip>
- </span>
+type SortColumn = "uncachedPromptTokens" | "cacheHitRatio" | "potentialSavings";
+interface SortState {
+ column: SortColumn;
+ dir: "asc" | "desc";
+}
+
+const NATURAL_DIR: Record<SortColumn, "asc" | "desc"> = {
+ uncachedPromptTokens: "desc",
+ cacheHitRatio: "asc",
+ potentialSavings: "desc",
+};
+
+const compareRows = (a: CacheLeakageRow, b: CacheLeakageRow, sort: SortState): number => {
+ const av = a[sort.column];
+ const bv = b[sort.column];
+ if (av == null && bv == null) return 0;
+ if (av == null) return 1;
+ if (bv == null) return -1;
+ return sort.dir === "asc" ? av - bv : bv - av;
+};
+
+const InfoTooltip = ({ info }: { info: string }) => (
+ <Tooltip>
+ <TooltipTrigger render={<span className="inline-flex" aria-label={info} />}>
+ <Info className="h-3 w-3 text-gray-400" />
+ </TooltipTrigger>
+ <TooltipContent className="max-w-xs">{info}</TooltipContent>
+ </Tooltip>
);
+const SortableHead = ({
+ column,
+ label,
+ info,
+ sort,
+ onSort,
+}: {
+ column: SortColumn;
+ label: string;
+ info: string;
+ sort: SortState;
+ onSort: (column: SortColumn) => void;
+}) => {
+ const active = sort.column === column;
+ const ActiveArrow = sort.dir === "asc" ? ArrowUp : ArrowDown;
+ const Arrow = active ? ActiveArrow : ArrowUpDown;
+ return (
+ <TableHead className="text-right">
+ <span className="inline-flex items-center justify-end gap-1">
+ <button
+ type="button"
+ onClick={() => onSort(column)}
+ aria-label={`Sort by ${label}`}
+ className="inline-flex items-center gap-1 font-medium hover:text-foreground"
+ >
+ {label}
+ <Arrow className={`h-3 w-3 ${active ? "text-foreground" : "text-gray-400"}`} />
+ </button>
+ <InfoTooltip info={info} />
+ </span>
+ </TableHead>
+ );
+};
+
const CacheLeakageCard: React.FC<CacheLeakageCardProps> = ({ activity }) => {
const { dateValue, onDateChange, results, loading, isFetchingMore } = activity;
const [dimension, setDimension] = useState<CacheLeakageDimension>("key");
+ const [sort, setSort] = useState<SortState>({ column: "potentialSavings", dir: "desc" });
const leakage = useMemo(() => computeCacheLeakage(results, dimension), [results, dimension]);
+ const rows = useMemo(() => [...leakage.rows].sort((a, b) => compareRows(a, b, sort)), [leakage.rows, sort]);
+
+ const onSort = (column: SortColumn) =>
+ setSort((prev) =>
+ prev.column === column
+ ? { column, dir: prev.dir === "asc" ? "desc" : "asc" }
+ : { column, dir: NATURAL_DIR[column] },
+ );
const subject = dimension === "model" ? "Models" : "Keys";
const firstColumn = dimension === "model" ? "Model" : "Key";
@@ -64,7 +125,7 @@ const CacheLeakageCard: React.FC<CacheLeakageCardProps> = ({ activity }) => {
</Tabs>
</CardHeader>
<CardContent>
- {leakage.rows.length === 0 ? (
+ {rows.length === 0 ? (
<p className="py-8 text-center text-sm text-muted-foreground">
{loading || isFetchingMore ? "Loading..." : `No ${emptyNoun} usage in this range.`}
</p>
@@ -73,28 +134,31 @@ const CacheLeakageCard: React.FC<CacheLeakageCardProps> = ({ activity }) => {
<TableHeader>
<TableRow>
<TableHead>{firstColumn}</TableHead>
- <TableHead className="text-right">
- <HeadWithInfo
- label="Uncached input"
- info="Input tokens you sent in this range that weren't served from or written to the cache"
- />
- </TableHead>
- <TableHead className="text-right">
- <HeadWithInfo
- label="Cache hit rate"
- info="Share of your input tokens that were served from the cache"
- />
- </TableHead>
- <TableHead className="text-right">
- <HeadWithInfo
- label="Potential savings"
- info="About how much you'd save if this uncached input used prompt caching. Estimated as uncached input tokens times the per-token discount your cached traffic already gets (realized cache savings ÷ cache-read tokens)."
- />
- </TableHead>
+ <SortableHead
+ column="uncachedPromptTokens"
+ label="Uncached input tokens"
+ info="Input tokens you sent in this range that weren't served from or written to the cache"
+ sort={sort}
+ onSort={onSort}
+ />
+ <SortableHead
+ column="cacheHitRatio"
+ label="Cache hit rate"
+ info="Share of your input tokens that were served from the cache"
+ sort={sort}
+ onSort={onSort}
+ />
+ <SortableHead
+ column="potentialSavings"
+ label="Potential savings"
+ info="About how much you'd save if this uncached input used prompt caching. Estimated as uncached input tokens times the per-token discount your cached traffic already gets (realized cache savings ÷ cache-read tokens)."
+ sort={sort}
+ onSort={onSort}
+ />
</TableRow>
</TableHeader>
<TableBody>
- {leakage.rows.map((row) => (
+ {rows.map((row) => (
<TableRow key={row.id}>
<TableCell className="font-medium">
{row.label}