69 lines
6.6 KiB
JavaScript
69 lines
6.6 KiB
JavaScript
const object = value => !!value && typeof value === 'object' && !Array.isArray(value);
|
|
const valid = value => Number.isSafeInteger(value) && value >= 0;
|
|
const at = (value, path) => path.split('.').reduce((part, key) => object(part) ? part[key] : undefined, value);
|
|
const present = value => value !== undefined && value !== null;
|
|
const metric = (value, source, status = 'reported') => ({ tokens: valid(value) ? value : null,
|
|
status: !present(value) ? 'unknown' : valid(value) ? status : 'invalid', sources: Array.isArray(source) ? source : [source] });
|
|
|
|
// These are token categories from the received protocol fields, not a bill or
|
|
// proof that any particular provider served the connection. Missing is not zero.
|
|
export function tokenAccountingView(usage) {
|
|
if (!object(usage)) return null;
|
|
if (usage.tokenAccounting?.schemaVersion === 1 && object(usage.tokenAccounting.metrics)) return usage.tokenAccounting;
|
|
const protocol = usage.protocol ?? 'chat-completions', raw = object(usage.raw) ? usage.raw : null;
|
|
const metrics = { input: metric(usage.prompt_tokens, 'normalized.prompt_tokens'), output: metric(usage.completion_tokens, 'normalized.completion_tokens'),
|
|
total: metric(usage.total_tokens, 'normalized.total_tokens'), cacheRead: metric(undefined, 'unreported'), cacheWrite: metric(undefined, 'unreported'),
|
|
uncached: metric(undefined, 'unreported'), reasoning: metric(undefined, 'unreported') };
|
|
const issues = [], lifetimes = []; let scheme = 'read-write-uncached';
|
|
const read = path => metric(at(raw, path), 'usage.' + path);
|
|
const first = paths => read(paths.find(path => present(at(raw, path))) ?? paths[0]);
|
|
let lifetimePaths = [];
|
|
if (!raw) {
|
|
metrics.cacheRead = metric(usage.prompt_tokens_details?.cached_tokens, 'normalized.prompt_tokens_details.cached_tokens');
|
|
metrics.cacheWrite = metric(usage.prompt_tokens_details?.cache_creation_tokens, 'normalized.prompt_tokens_details.cache_creation_tokens');
|
|
metrics.reasoning = metric(usage.completion_tokens_details?.reasoning_tokens, 'normalized.completion_tokens_details.reasoning_tokens');
|
|
} else if (protocol === 'anthropic-messages') {
|
|
metrics.output = read('output_tokens'); metrics.cacheRead = read('cache_read_input_tokens'); metrics.cacheWrite = read('cache_creation_input_tokens'); metrics.uncached = read('input_tokens');
|
|
const parts = [metrics.uncached, metrics.cacheRead, metrics.cacheWrite];
|
|
const input = parts.every(value => valid(value.tokens)) ? parts.reduce((sum, value) => sum + value.tokens, 0) : undefined;
|
|
metrics.input = metric(input, parts.flatMap(value => value.sources), 'derived');
|
|
lifetimePaths = ['cache_creation.ephemeral_5m_input_tokens', 'cache_creation.ephemeral_1h_input_tokens'];
|
|
} else {
|
|
const responses = protocol === 'responses', details = responses ? 'input_tokens_details' : 'prompt_tokens_details';
|
|
metrics.input = read(responses ? 'input_tokens' : 'prompt_tokens'); metrics.output = read(responses ? 'output_tokens' : 'completion_tokens');
|
|
metrics.cacheRead = first(responses ? [details + '.cached_tokens'] : [details + '.cached_tokens', 'prompt_cache_hit_tokens', 'cached_tokens']);
|
|
metrics.cacheWrite = read(details + '.cache_write_tokens');
|
|
metrics.uncached = first(responses ? [details + '.uncached_tokens'] : [details + '.uncached_tokens', 'prompt_cache_miss_tokens']);
|
|
metrics.reasoning = read((responses ? 'output_tokens_details' : 'completion_tokens_details') + '.reasoning_tokens');
|
|
lifetimePaths = [details + '.cache_write_tokens_details.ttl_30m_tokens', details + '.cache_write_tokens_details.ttl_12h_tokens'];
|
|
if (!responses && metrics.cacheWrite.status === 'unknown' && metrics.uncached.sources[0] === 'usage.prompt_cache_miss_tokens') scheme = 'read-and-miss';
|
|
if (!responses && new Set([at(raw, details + '.cached_tokens'), raw.prompt_cache_hit_tokens, raw.cached_tokens].filter(valid)).size > 1) issues.push('cache_read_alias_mismatch');
|
|
if (!responses && valid(at(raw, details + '.uncached_tokens')) && valid(raw.prompt_cache_miss_tokens)
|
|
&& at(raw, details + '.uncached_tokens') !== raw.prompt_cache_miss_tokens) issues.push('uncached_alias_mismatch');
|
|
if (metrics.uncached.status === 'unknown' && [metrics.input.tokens, metrics.cacheRead.tokens, metrics.cacheWrite.tokens].every(valid)) {
|
|
const remaining = metrics.input.tokens - metrics.cacheRead.tokens - metrics.cacheWrite.tokens;
|
|
if (valid(remaining)) metrics.uncached = { tokens: remaining, status: 'derived', sources: [...metrics.input.sources, ...metrics.cacheRead.sources, ...metrics.cacheWrite.sources], formula: 'input - cacheRead - cacheWrite' };
|
|
else issues.push('cache_categories_exceed_input');
|
|
}
|
|
}
|
|
if (raw) {
|
|
if (present(raw.total_tokens)) metrics.total = read('total_tokens');
|
|
else metrics.total = metric(valid(metrics.input.tokens) && valid(metrics.output.tokens) ? metrics.input.tokens + metrics.output.tokens : undefined,
|
|
[...metrics.input.sources, ...metrics.output.sources], 'derived');
|
|
for (const key of ['cacheRead', 'cacheWrite', 'uncached']) if (valid(metrics[key].tokens) && valid(metrics.input.tokens) && metrics[key].tokens > metrics.input.tokens) issues.push(key + '_exceeds_input');
|
|
if (valid(metrics.reasoning.tokens) && valid(metrics.output.tokens) && metrics.reasoning.tokens > metrics.output.tokens) issues.push('reasoning_exceeds_output');
|
|
if (lifetimePaths.some(path => present(at(raw, path)))) for (const path of lifetimePaths) lifetimes.push({ name: path.split('.').at(-1), ...read(path) });
|
|
}
|
|
for (const [key, value] of Object.entries(metrics)) if (value.status === 'invalid') issues.push('invalid_' + key);
|
|
for (const value of lifetimes) if (value.status === 'invalid') issues.push('invalid_write_lifetime');
|
|
const check = (values, total) => !valid(total) || !values.every(valid) ? 'partial' : values.reduce((sum, value) => sum + value, 0) === total ? 'consistent' : 'inconsistent';
|
|
const checks = { inputPartition: check((scheme === 'read-and-miss' ? ['cacheRead', 'uncached'] : ['cacheRead', 'cacheWrite', 'uncached']).map(key => metrics[key].tokens), metrics.input.tokens),
|
|
total: check([metrics.input.tokens, metrics.output.tokens], metrics.total.tokens), cacheWriteLifetimes: lifetimes.length ? check(lifetimes.map(value => value.tokens), metrics.cacheWrite.tokens) : 'partial' };
|
|
for (const [key, value] of Object.entries(checks)) if (value === 'inconsistent') issues.push(key + '_mismatch');
|
|
return { schemaVersion: 1, protocol, projection: raw ? 'original-usage' : 'legacy-normalized', scheme, metrics, lifetimes, checks, issues };
|
|
}
|
|
|
|
export function withTokenAccounting(usage) {
|
|
return usage ? { ...usage, tokenAccounting: tokenAccountingView(usage) } : usage;
|
|
}
|