Skip to content

🪢 Open source agent evals & observability: Trace, evaluate, and improve LLM applications with one open platform.

4.1 KB
Raw Blame
import type { GatewayAnalyticsWidget } from "@/src/features/ai-gateway/components/GatewayAnalyticsPage/types/gatewayAnalyticsWidget";

const BREAKDOWN_ROW_LIMIT = 20;

/** Per-user breakdowns leave out service keys, which have no bound owner. */
const KEY_OWNER_PRESENT_FILTER: GatewayAnalyticsWidget["filters"][number] = {
  column: "aiGatewayKeyOwnerUserId",
  operator: "is not null",
  value: "",
  type: "null",
};

export const GATEWAY_ANALYTICS_WIDGETS: readonly GatewayAnalyticsWidget[] = [
  {
    id: "total-cost",
    name: "Total cost",
    description: "Cost of all gateway requests",
    view: "observations",
    dimensions: [],
    metrics: [{ measure: "totalCost", agg: "sum" }],
    filters: [],
    chartType: "NUMBER",
    chartConfig: { type: "NUMBER" },
    size: "small",
  },
  {
    id: "requests",
    name: "Requests",
    description: "Observations recorded by the gateway",
    view: "observations",
    dimensions: [],
    metrics: [{ measure: "count", agg: "count" }],
    filters: [],
    chartType: "NUMBER",
    chartConfig: { type: "NUMBER" },
    size: "small",
  },
  {
    id: "total-tokens",
    name: "Tokens",
    description: "Input and output tokens",
    view: "observations",
    dimensions: [],
    metrics: [{ measure: "totalTokens", agg: "sum" }],
    filters: [],
    chartType: "NUMBER",
    chartConfig: { type: "NUMBER" },
    size: "small",
  },
  {
    id: "cost-over-time",
    name: "Cost over time",
    description: "Cost of all gateway requests over time",
    view: "observations",
    dimensions: [],
    metrics: [{ measure: "totalCost", agg: "sum" }],
    filters: [],
    chartType: "BAR_TIME_SERIES",
    chartConfig: { type: "BAR_TIME_SERIES" },
    size: "full",
  },
  {
    id: "cost-by-user",
    name: "Cost by user",
    description: `Top ${BREAKDOWN_ROW_LIMIT} users by cost; service keys are excluded`,
    view: "observations",
    dimensions: [{ field: "aiGatewayKeyOwnerUserId" }],
    metrics: [{ measure: "totalCost", agg: "sum" }],
    filters: [KEY_OWNER_PRESENT_FILTER],
    chartType: "HORIZONTAL_BAR",
    chartConfig: { type: "HORIZONTAL_BAR", row_limit: BREAKDOWN_ROW_LIMIT },
    dimensionLabels: "users",
    size: "medium",
  },
  {
    id: "requests-per-user",
    name: "Requests per user",
    description: `Top ${BREAKDOWN_ROW_LIMIT} users by requests; service keys are excluded`,
    view: "observations",
    dimensions: [{ field: "aiGatewayKeyOwnerUserId" }],
    metrics: [{ measure: "count", agg: "count" }],
    filters: [KEY_OWNER_PRESENT_FILTER],
    chartType: "HORIZONTAL_BAR",
    chartConfig: { type: "HORIZONTAL_BAR", row_limit: BREAKDOWN_ROW_LIMIT },
    dimensionLabels: "users",
    size: "medium",
  },
  {
    id: "cost-by-api-key",
    name: "Cost by gateway key",
    description: `Top ${BREAKDOWN_ROW_LIMIT} gateway keys by cost`,
    view: "observations",
    dimensions: [{ field: "aiGatewayKeyId" }],
    metrics: [{ measure: "totalCost", agg: "sum" }],
    filters: [],
    chartType: "HORIZONTAL_BAR",
    chartConfig: { type: "HORIZONTAL_BAR", row_limit: BREAKDOWN_ROW_LIMIT },
    dimensionLabels: "apiKeys",
    size: "medium",
  },
  {
    id: "cost-by-model",
    name: "Cost by model",
    description: `Top ${BREAKDOWN_ROW_LIMIT} models by cost`,
    view: "observations",
    dimensions: [{ field: "providedModelName" }],
    metrics: [{ measure: "totalCost", agg: "sum" }],
    filters: [],
    chartType: "HORIZONTAL_BAR",
    chartConfig: { type: "HORIZONTAL_BAR", row_limit: BREAKDOWN_ROW_LIMIT },
    size: "medium",
  },
];

/** Not rendered: a per-model breakdown that finds usage without a model price. */
export const GATEWAY_UNPRICED_USAGE_QUERY: GatewayAnalyticsWidget = {
  id: "unpriced-usage",
  name: "Unpriced usage",
  description: "Requests and tokens of models without a price",
  view: "observations",
  dimensions: [{ field: "providedModelName" }],
  metrics: [
    { measure: "count", agg: "count" },
    { measure: "totalTokens", agg: "sum" },
    { measure: "totalCost", agg: "sum" },
  ],
  filters: [],
  chartType: "HORIZONTAL_BAR",
  chartConfig: { type: "HORIZONTAL_BAR", row_limit: 1000 },
  size: "full",
};