@sema-agent/core
Version:
Stateless, task-oriented AI agent core
31 lines (30 loc) • 1.44 kB
JavaScript
import { computeCostMicroUsd } from "../pricing.js";
export function cacheFamilyOf(model) {
const override = model.params?.promptCacheFamily;
if (override === "input-excludes-cached" || override === "anthropic")
return "input-excludes-cached";
if (override === "input-includes-cached" || override === "openai")
return "input-includes-cached";
return model.api === "anthropic-messages" || model.api === "bedrock-converse-stream"
? "input-excludes-cached"
: "input-includes-cached";
}
export function promptTokensOf(family, u) {
return family === "input-excludes-cached" ? u.input + u.cacheRead + u.cacheWrite : u.input;
}
export function uncachedInputTokensOf(family, u) {
return Math.max(0, promptTokensOf(family, u) - u.cacheRead - u.cacheWrite);
}
export function usageCostMicroUsd(family, u, pricing) {
const normalized = { input: u.input || 0, cacheRead: u.cacheRead || 0, cacheWrite: u.cacheWrite || 0 };
const totalInputTokens = promptTokensOf(family, normalized);
const uncachedInputTokens = uncachedInputTokensOf(family, normalized);
const costMicroUsd = computeCostMicroUsd({
totalInputTokens,
cacheReadTokens: normalized.cacheRead,
cacheWriteTokens: normalized.cacheWrite,
cacheWriteTokensLong: 0,
outputTokens: u.output || 0,
}, pricing);
return { totalInputTokens, uncachedInputTokens, costMicroUsd };
}