@code-yeongyu/senpi
Version:
Coding agent CLI with read, bash, edit, write tools and session management
42 lines • 1.99 kB
JavaScript
import { calculateCost, costCardAt } from "@earendil-works/pi-ai";
/**
* Price of sending `tokens` of prompt with no warm cache: a cache write where the model prices one (Anthropic),
* else plain input (providers whose cache needs no write). Uses the model's own cost card, tiers included.
*/
function coldPromptUsd(model, tokens, atMs) {
const writes = costCardAt(model.cost, atMs).cacheWrite > 0;
const usage = {
input: writes ? 0 : tokens,
output: 0,
cacheRead: 0,
cacheWrite: writes ? tokens : 0,
totalTokens: tokens,
cost: { input: 0, output: 0, cacheRead: 0, cacheWrite: 0, total: 0 },
};
return calculateCost(model, usage, atMs).total;
}
export function estimateHandoff(model, contextTokens, freshTokens, atMs) {
const nextReplyUsd = coldPromptUsd(model, contextTokens, atMs);
if (!(nextReplyUsd > 0))
return undefined;
const fresh = Math.min(freshTokens, contextTokens);
const freshUsd = coldPromptUsd(model, fresh, atMs);
return { contextTokens, freshTokens: fresh, nextReplyUsd, freshUsd, savingUsd: nextReplyUsd - freshUsd };
}
export function formatUsd(value) {
if (value < 0.01)
return "under $0.01";
return `$${value.toFixed(2)}`;
}
function formatTokens(tokens) {
return tokens >= 1_000_000 ? `${(tokens / 1_000_000).toFixed(1)}M` : `${Math.round(tokens / 1000)}k`;
}
export function hintText(estimate, subscription) {
// The numbers lead: a one-line banner that clips its tail must still show them.
const prices = subscription ? " at API prices" : "";
const plan = subscription ? " On your plan it counts toward your usage limits." : "";
return (`The next reply would cost about ${formatUsd(estimate.nextReplyUsd)}${prices}; ` +
`a fresh session about ${formatUsd(estimate.freshUsd)}. ` +
`This session is ${formatTokens(estimate.contextTokens)} tokens and its cache has expired.${plan}`);
}
//# sourceMappingURL=estimate.js.map