Files
ai-kit/test/prices.test.js
CatoandClaude 43b629d307 feat(prices): what the paid calls cost, from the vendors' own pages (1.30.0) (#118)
PRICE_LIST names each paid model's USD per 1M tokens and the page it was read
from (2026-11-15). summariseUsage tallies gain costUSD for the server's paid
answered calls and unpriced for those it cannot price, never counted as zero.


Claude-Session: https://claude.ai/code/session_019MDtmtUQb9BM11ycpApwGq

Co-authored-by: Claude <noreply@anthropic.com>
2026-11-15 10:53:47 +01:00

59 lines
2.4 KiB
JavaScript

/**
* What a paid call cost (src/prices.ts), and the summary's money (usage-log).
* The owner's ask, 2026-11-15: see how much each provider has been used for.
*/
import { test } from "node:test";
import assert from "node:assert/strict";
import { PRICE_LIST, priceOf, costOf, summariseUsage } from "@bitbaum/ai-kit";
test("every price names the vendor page it was read from, and when", () => {
for (const p of PRICE_LIST) {
assert.match(p.source, /^https:\/\//, `${p.provider}/${p.model}`);
assert.match(p.read, /^\d{4}-\d{2}-\d{2}$/);
assert.ok(p.input >= 0 && p.output >= 0);
}
});
test("a price is found by the bare id and by a vendor-prefixed id", () => {
assert.equal(priceOf("openai", "text-embedding-3-small")?.input, 0.02);
assert.equal(priceOf("xai", "x-ai/grok-4.7")?.output, 6);
assert.equal(priceOf("groq", "openai/gpt-oss-120b"), null, "a free vendor has no price here");
});
test("cost is tokens times the published price; an unknown model is null, not zero", () => {
assert.equal(costOf("openai", "text-embedding-3-small", 1_000_000), 0.02);
assert.equal(costOf("xai", "grok-4.7", 1000, 1000), (1000 * 2 + 1000 * 6) / 1_000_000);
assert.equal(costOf("openai", "gpt-9-imaginary", 100, 100), null);
});
test("the summary adds up paid calls, counts what it could not price, and ignores free and own-key calls", () => {
const base = { t: 1, app: "ORANGECAT", outcome: "served", byok: false };
const s = summariseUsage([
{
...base,
provider: "openai",
model: "text-embedding-3-small",
paid: true,
inputTokens: 500_000,
},
{ ...base, provider: "openai", model: "text-embedding-3-small", paid: true },
{ ...base, provider: "openai", model: "mystery", paid: true, inputTokens: 10 },
{ ...base, provider: "groq", model: "openai/gpt-oss-20b", paid: false, inputTokens: 9999 },
{ ...base, provider: "xai", model: "grok-4.7", paid: false, byok: true, inputTokens: 9999 },
{
...base,
provider: "openai",
model: "text-embedding-3-small",
paid: true,
outcome: "failed",
inputTokens: 1e6,
},
]);
const openai = s.providers.find((p) => p.provider === "openai");
assert.equal(openai.costUSD, 0.01);
assert.equal(openai.unpriced, 2, "no tokens, and no price: both said, neither counted as zero");
assert.equal(s.providers.find((p) => p.provider === "groq").costUSD, 0);
assert.equal(s.total.costUSD, 0.01);
assert.equal(s.apps[0].costUSD, 0.01);
});