24 lines
684 B
JavaScript
24 lines
684 B
JavaScript
const IN_PER_M = 3.00;
|
|
const OUT_PER_M = 15.00;
|
|
const CACHE_READ_PER_M = 0.30;
|
|
const CACHE_WRITE_PER_M = 3.75;
|
|
|
|
export function estimateCost({ tokens_input = 0, tokens_output = 0, cache_read = 0, cache_write = 0 }) {
|
|
const usd =
|
|
(tokens_input / 1_000_000) * IN_PER_M +
|
|
(tokens_output / 1_000_000) * OUT_PER_M +
|
|
(cache_read / 1_000_000) * CACHE_READ_PER_M +
|
|
(cache_write / 1_000_000) * CACHE_WRITE_PER_M;
|
|
return usd;
|
|
}
|
|
|
|
export function fmtUsd(usd) {
|
|
if (usd < 0.01) return `$${usd.toFixed(4)}`;
|
|
return `$${usd.toFixed(3)}`;
|
|
}
|
|
|
|
export function fmtTokens(n) {
|
|
if (n >= 1000) return `${(n / 1000).toFixed(1)}K`;
|
|
return String(n);
|
|
}
|