/** * @copyright nhcarrigan * @license Naomi's Public License * @author Naomi Carrigan */ import type { BetaUsage } from "@anthropic-ai/sdk/resources/beta.js"; /** * Calculates the cost of an AI response. * @param usage - The usage payload from Anthropic. * @returns A description of the cost of the AI response. */ export const getAiCost = (usage: BetaUsage): string => { const { input_tokens: inputTokens, output_tokens: outputTokens } = usage; const costPerInputToken = 5 / 1_000_000; const costPerOutputToken = 25 / 1_000_000; const inputCost = inputTokens * costPerInputToken; const outputCost = outputTokens * costPerOutputToken; const totalCost = inputCost + outputCost; return `Input cost: ${inputCost.toLocaleString("en-GB", { currency: "USD", style: "currency", })} Output cost: ${outputCost.toLocaleString("en-GB", { currency: "USD", style: "currency", })} Total cost: ${totalCost.toLocaleString("en-GB", { currency: "USD", style: "currency", })}`; };