ai
Version:
AI SDK by Vercel - build apps like ChatGPT, Claude, Gemini, and more with a single interface for any model using the Vercel AI Gateway or go direct to OpenAI, Anthropic, Google, or any other model provider.
18 lines (16 loc) • 481 B
text/typescript
/**
* Calculates a token rate in tokens per second.
*
* Returns 0 when the token count is unknown, the duration is unknown or 0, or
* the computed rate cannot be represented as a finite JSON-safe number.
*/
export function calculateTokensPerSecond({
tokens,
durationMs,
}: {
tokens: number | undefined;
durationMs: number | undefined;
}): number {
const tokenRate = (1000 * (tokens ?? 0)) / (durationMs ?? 0);
return Number.isFinite(tokenRate) ? tokenRate : 0;
}