AI Stream Metrics Calculator (playground/streamMetrics.ts)

Ominiai · src/lib/playground/streamMetrics.ts · typescript · MIT
ai, analytics, realtime, lang:typescript

Computes latency, token counts, and estimated cost for AI streaming requests. Use when tracking performance and pricing in LLM chat playgrounds.

// Source: https://github.com/MAHIAMIR45/Ominiai | License: MIT | Copyright 2026 diegosouzapw
// Provided via justclaw.it.com; keep this notice with the code.
// src/lib/playground/streamMetrics.ts
// Pure function — no React dependency. Reusable outside of hooks.

import type { StreamMetrics } from "@/shared/schemas/playground";

export interface ComputeMetricsArgs {
  /** ms timestamp of when the request was dispatched. null = not started. */
  startedAt: number | null;
  /** ms timestamp of when the first SSE chunk was received. null = not received yet. */
  firstChunkAt: number | null;
  /** ms timestamp of when the stream finished. null = not finished yet. */
  finishedAt: number | null;
  /** Number of input/prompt tokens. */
  tokensIn: number;
  /** Number of output/completion toke

... (truncated -- full source via MCP)

See the full source, get the GitHub permalink, and search for more like it.

Get a free API key