initial cost tracking

Signed-off-by: Eason Goodale <easong@openai.com>
This commit is contained in:
Eason Goodale
2025-04-18 03:10:54 -07:00
parent 0d6a98f9af
commit cdc0897a25
11 changed files with 421 additions and 4 deletions

View File

@@ -0,0 +1,28 @@
import { describe, expect, it } from "vitest";
import type { ResponseItem } from "openai/resources/responses/responses.mjs";
import { calculateContextPercentRemaining } from "../src/components/chat/terminal-chat-utils.js";
function makeUserMessage(id: string, text: string): ResponseItem {
return {
id,
type: "message",
role: "user",
content: [{ type: "input_text", text }],
} as ResponseItem;
}
describe("calculateContextPercentRemaining", () => {
it("includes extra context characters in calculation", () => {
const msgText = "a".repeat(40); // 40 chars → 10 tokens
const items = [makeUserMessage("1", msgText)];
const model = "gpt-4-16k";
const base = calculateContextPercentRemaining(items, model);
const withExtra = calculateContextPercentRemaining(items, model, 8); // +8 chars → +2 tokens
expect(withExtra).toBeLessThan(base);
});
});

View File

@@ -0,0 +1,50 @@
import { describe, expect, test } from "vitest";
import { estimateCostUSD } from "../src/utils/estimate-cost.js";
import { SessionCostTracker } from "../src/utils/session-cost.js";
import type { ResponseItem } from "openai/resources/responses/responses.mjs";
// Helper to craft a minimal ResponseItem for tests
function makeMessage(
id: string,
role: "user" | "assistant",
text: string,
): ResponseItem {
return {
id,
type: "message",
role,
content: [{ type: role === "user" ? "input_text" : "output_text", text }],
} as ResponseItem;
}
describe("estimateCostUSD", () => {
test("returns a proportional, positive estimate for known models", () => {
const items: Array<ResponseItem> = [
makeMessage("1", "user", "hello world"),
makeMessage("2", "assistant", "hi there"),
];
const cost = estimateCostUSD(items, "gpt-3.5-turbo");
expect(cost).not.toBeNull();
expect(cost!).toBeGreaterThan(0);
// Adding another token should increase the estimate
const cost2 = estimateCostUSD(
items.concat([makeMessage("3", "user", "extra")]),
"gpt-3.5-turbo",
);
expect(cost2!).toBeGreaterThan(cost!);
});
});
describe("SessionCostTracker", () => {
test("accumulates items and reports tokens & cost", () => {
const tracker = new SessionCostTracker("gpt-3.5-turbo");
tracker.addItems([makeMessage("1", "user", "foo")]);
tracker.addItems([makeMessage("2", "assistant", "bar baz")]);
expect(tracker.getTokensUsed()).toBeGreaterThan(0);
expect(tracker.getCostUSD()!).toBeGreaterThan(0);
});
});

View File

@@ -0,0 +1,79 @@
import { afterEach, describe, expect, it, vi } from "vitest";
import type { ResponseItem } from "openai/resources/responses/responses.mjs";
import {
ensureSessionTracker,
getSessionTracker,
printAndResetSessionSummary,
} from "../src/utils/session-cost.js";
function makeMessage(id: string, role: "user" | "assistant", text: string): ResponseItem {
return {
id,
type: "message",
role,
content: [{ type: role === "user" ? "input_text" : "output_text", text }],
} as ResponseItem;
}
describe("printAndResetSessionSummary", () => {
afterEach(() => {
vi.restoreAllMocks();
});
it("/clear resets tracker so successive conversations start fresh", () => {
const spy = vi.spyOn(console, "log").mockImplementation(() => {});
const perSessionTokens: Array<number> = [];
for (let i = 1; i <= 3; i++) {
const tracker = ensureSessionTracker("gpt-3.5-turbo");
tracker.addTokens(i * 10); // 10, 20, 30
perSessionTokens.push(tracker.getTokensUsed());
// Simulate user typing /clear which prints & resets
printAndResetSessionSummary();
expect(getSessionTracker()).toBeNull();
}
expect(perSessionTokens).toEqual([10, 20, 30]);
spy.mockRestore();
});
it("prints a summary and resets the global tracker", () => {
const spy = vi.spyOn(console, "log").mockImplementation(() => {});
const tracker = ensureSessionTracker("gpt-3.5-turbo");
tracker.addItems([
makeMessage("1", "user", "hello"),
makeMessage("2", "assistant", "hi"),
]);
printAndResetSessionSummary();
expect(spy).toHaveBeenCalled();
expect(getSessionTracker()).toBeNull();
});
it("prefers exact token counts added via addTokens() over heuristic", () => {
const tracker = ensureSessionTracker("gpt-3.5-turbo");
// Add a long message (heuristic would count >1 token)
tracker.addItems([
makeMessage("x", "user", "a".repeat(400)), // ~100 tokens
]);
const heuristicTokens = tracker.getTokensUsed();
expect(heuristicTokens).toBeGreaterThan(50);
// Now inject an exact low token count and ensure it overrides
tracker.addTokens(10);
expect(tracker.getTokensUsed()).toBe(heuristicTokens + (10 - heuristicTokens));
const cost = tracker.getCostUSD();
expect(cost).not.toBeNull();
});
});