feat: log token usage

This commit is contained in:
Naomi Carrigan 2025-02-10 21:07:52 -08:00
parent a347ff8eb6
commit 8c72375761
Signed by: naomi
SSH Key Fingerprint: SHA256:rca1iUI2OhAM6n4FIUaFcZcicmri0jgocqKiTTAfrt8
8 changed files with 62 additions and 0 deletions

View File

@ -41,6 +41,14 @@ client.on(Events.InteractionCreate, (interaction) => {
}
});
client.on(Events.EntitlementCreate, (entitlement) => {
void logger.log("info", `User ${entitlement.userId} has subscribed!`);
});
client.on(Events.EntitlementDelete, (entitlement) => {
void logger.log("info", `User ${entitlement.userId} has unsubscribed... :c`);
});
client.on(Events.ClientReady, () => {
void logger.log("debug", "Bot is ready.");
});

View File

@ -6,6 +6,7 @@
import { MessageFlags, type ChatInputCommandInteraction } from "discord.js";
import { personality } from "../config/personality.js";
import { ai } from "../utils/ai.js";
import { calculateCost } from "../utils/calculateCost.js";
import { isSubscribed } from "../utils/isSubscribed.js";
import type { ImageBlockParam } from "@anthropic-ai/sdk/resources/index.js";
@ -109,4 +110,7 @@ export const alt = async(
response?.text
?? "I'm sorry, I don't have an answer for that. Please try again later.",
);
const { usage } = messages;
await calculateCost(usage, interaction.user.username, "alt-text");
};

View File

@ -6,6 +6,7 @@
import { MessageFlags, type ChatInputCommandInteraction } from "discord.js";
import { personality } from "../config/personality.js";
import { ai } from "../utils/ai.js";
import { calculateCost } from "../utils/calculateCost.js";
import { isSubscribed } from "../utils/isSubscribed.js";
/**
@ -40,4 +41,7 @@ export const evaluate = async(
response?.text
?? "I'm sorry, I don't have an answer for that. Please try again later.",
);
const { usage } = messages;
await calculateCost(usage, interaction.user.username, "evaluate");
};

View File

@ -6,6 +6,7 @@
import { MessageFlags, type ChatInputCommandInteraction } from "discord.js";
import { personality } from "../config/personality.js";
import { ai } from "../utils/ai.js";
import { calculateCost } from "../utils/calculateCost.js";
import { isSubscribed } from "../utils/isSubscribed.js";
/**
@ -41,4 +42,7 @@ export const mood = async(
response?.text
?? "I'm sorry, I don't have an answer for that. Please try again later.",
);
const { usage } = messages;
await calculateCost(usage, interaction.user.username, "mood");
};

View File

@ -6,6 +6,7 @@
import { MessageFlags, type ChatInputCommandInteraction } from "discord.js";
import { personality } from "../config/personality.js";
import { ai } from "../utils/ai.js";
import { calculateCost } from "../utils/calculateCost.js";
import { isSubscribed } from "../utils/isSubscribed.js";
/**
@ -41,4 +42,7 @@ export const proofread = async(
response?.text
?? "I'm sorry, I don't have an answer for that. Please try again later.",
);
const { usage } = messages;
await calculateCost(usage, interaction.user.username, "proofread");
};

View File

@ -6,6 +6,7 @@
import { MessageFlags, type ChatInputCommandInteraction } from "discord.js";
import { personality } from "../config/personality.js";
import { ai } from "../utils/ai.js";
import { calculateCost } from "../utils/calculateCost.js";
import { isSubscribed } from "../utils/isSubscribed.js";
/**
@ -41,4 +42,7 @@ export const query = async(
response?.text
?? "I'm sorry, I don't have an answer for that. Please try again later.",
);
const { usage } = messages;
await calculateCost(usage, interaction.user.username, "query");
};

View File

@ -6,6 +6,7 @@
import { MessageFlags, type ChatInputCommandInteraction } from "discord.js";
import { personality } from "../config/personality.js";
import { ai } from "../utils/ai.js";
import { calculateCost } from "../utils/calculateCost.js";
import { isSubscribed } from "../utils/isSubscribed.js";
/**
@ -41,4 +42,7 @@ export const summarise = async(
response?.text
?? "I'm sorry, I don't have an answer for that. Please try again later.",
);
const { usage } = messages;
await calculateCost(usage, interaction.user.username, "summarise");
};

View File

@ -0,0 +1,30 @@
/**
* @copyright nhcarrigan
* @license Naomi's Public License
* @author Naomi Carrigan
*/
import { logger } from "./logger.js";
import type { Usage } from "@anthropic-ai/sdk/resources/index.js";
/**
* Calculates the cost of a command run by a user, and sends to
* our logging service.
* @param usage -- The usage payload from Anthropic.
* @param uuid -- The Discord ID of the user who ran the command.
* @param command -- The command that was run.
*/
export const calculateCost = async(
usage: Usage,
uuid: string,
command: string,
): Promise<void> => {
const inputCost = usage.input_tokens * (3 / 1_000_000);
const outputCost = usage.output_tokens * (15 / 1_000_000);
const totalCost = inputCost + outputCost;
await logger.log(
"info",
`User ${uuid} ran \`${command}\` which accepted ${usage.input_tokens.toString()} and generated ${usage.output_tokens.toString()}.
Total cost: ${totalCost.toLocaleString("en-GB", { currency: "USD", style: "currency" })}`,
);
};