GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — fix(server): stop overpricing cached Claude tokens by SunkenInTime · Pull Request #8806 · pingdotgg/t3code

fix(server): stop overpricing cached Claude tokens #8806

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

Sign up for GitHub

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
maria-rcks merged 1 commit into pingdotgg:main from SunkenInTime:t3code/fix-claude-usage-pricing
Aug 30, 2026

Uh oh!

There was an error while loading. Please reload this page.

Merged

fix(server): stop overpricing cached Claude tokens #8806

Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
1 commit
Select commit
63bf1f8
fix(server): stop overpricing cached Claude tokens
SunkenInTime Aug 30, 2026
File filter
Filter by extension

Clear filters
Conversations
Failed to load comments.
Loading

Uh oh!

There was an error while loading. Please reload this page.

Jump to
Jump to file
Failed to load files.
Loading

Uh oh!

There was an error while loading. Please reload this page.

Diff view
Diff view
fix(server): stop overpricing cached Claude tokens
  • Loading branch information

Uh oh!

There was an error while loading. Please reload this page.

@SunkenInTime
SunkenInTime committed Aug 30, 2026
commit 63bf1f86ac251765f50bafd1c508f9a64cf639d2

There are no files selected for viewing

55 changes: 55 additions & 0 deletions 55 apps/server/src/usage/usagePricing.test.ts
Show comments
View file
Open in desktop
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
import { describe, expect, it } from "@effect/vitest";

import { lookupRate, normalizeModelName, parseRateTable } from "./usagePricing.ts";

const rate = (input: number, cacheRead?: number) => ({
input_cost_per_token: input,
output_cost_per_token: input * 5,
...(cacheRead === undefined ? {} : { cache_read_input_token_cost: cacheRead }),
});

describe("usage pricing", () => {
it("keeps the existing model-name normalization contract", () => {
expect(normalizeModelName(" Anthropic/Claude-Opus-5 ")).toBe("claude-opus-5");
});

it("keeps the canonical Fable rate separate from DeepInfra in either order", () => {
const canonical = ["claude-fable-5", rate(1e-5, 1e-6)] as const;
const deepInfra = ["deepinfra/anthropic/claude-fable-5", rate(1e-5)] as const;

for (const entries of [
[canonical, deepInfra],
[deepInfra, canonical],
]) {
const table = parseRateTable(Object.fromEntries(entries));

expect(lookupRate(table, "claude-fable-5")?.cacheReadCostPerToken).toBe(1e-6);
expect(lookupRate(table, "deepinfra/anthropic/claude-fable-5")?.cacheReadCostPerToken).toBe(
1e-5,
);
expect(lookupRate(table, "other/claude-fable-5")).toBeNull();
}
});

it("adds a bare alias when every qualified entry has the same rate", () => {
const table = parseRateTable({
"provider-a/example-model": rate(1),
"provider-b/example-model": rate(1),
});

expect(lookupRate(table, "example-model")).toEqual(
lookupRate(table, "provider-a/example-model"),
);
});

it("leaves an ambiguous bare name unpriced", () => {
const table = parseRateTable({
"provider-a/example-model": rate(1),
"provider-b/example-model": rate(3),
});

expect(lookupRate(table, "provider-a/example-model")?.inputCostPerToken).toBe(1);
expect(lookupRate(table, "provider-b/example-model")?.inputCostPerToken).toBe(3);
expect(lookupRate(table, "example-model")).toBeNull();
});
});
58 changes: 48 additions & 10 deletions 58 apps/server/src/usage/usagePricing.ts
Show comments
View file
Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,9 @@ function finiteNumber(value: unknown): number | null {
* Entries without both an input and an output rate are dropped: a half-priced
* model would silently under-report cost, which is worse than reporting the
* model as unpriced.
*
* Entries keep their full normalized key; a bare name is aliased only when no
* canonical entry exists and every qualified entry has the same rate.
*/
export function parseRateTable(document: unknown): RateTable {
const table = new Map<string, ModelRate>();
Expand All @@ -56,7 +59,9 @@ export function parseRateTable(document: unknown): RateTable {
const output = finiteNumber(entry.output_cost_per_token);
if (input === null || output === null) continue;

table.set(normalizeModelName(name), {
const key = normalizeRateKey(name);
if (key.length === 0) continue;
table.set(key, {
inputCostPerToken: input,
outputCostPerToken: output,
// Anthropic bills cache reads at a discount and cache writes at a
Expand All @@ -66,20 +71,52 @@ export function parseRateTable(document: unknown): RateTable {
cacheCreationCostPerToken: finiteNumber(entry.cache_creation_input_token_cost) ?? input,
});
}

// `null` marks a bare name claimed at conflicting rates: no alias for it.
const aliasCandidates = new Map<string, ModelRate | null>();
for (const [key, rate] of table) {
const alias = bareModelName(key);
if (alias.length === 0 || alias === key || table.has(alias)) continue;
const held = aliasCandidates.get(alias);
if (held === undefined) {
aliasCandidates.set(alias, rate);
} else if (held !== null && !sameRate(held, rate)) {
aliasCandidates.set(alias, null);
}
}
for (const [alias, rate] of aliasCandidates) {
if (rate !== null) table.set(alias, rate);
}

return table;
}

function sameRate(a: ModelRate, b: ModelRate): boolean {
return (
a.inputCostPerToken === b.inputCostPerToken &&
a.outputCostPerToken === b.outputCostPerToken &&
a.cacheReadCostPerToken === b.cacheReadCostPerToken &&
a.cacheCreationCostPerToken === b.cacheCreationCostPerToken
);
}

function normalizeRateKey(model: string): string {
return model.trim().toLowerCase();
}

/**
* Canonicalises a model name for lookup.
*
* Strips a `provider/` prefix (LiteLLM publishes both `claude-opus-5` and
* `anthropic/claude-opus-5`) and lowercases, since transcripts are inconsistent
* about casing.
* Strips a `provider/` prefix and lowercases, since transcripts are
* inconsistent about casing.
*/
export function normalizeModelName(model: string): string {
const trimmed = model.trim().toLowerCase();
const slash = trimmed.lastIndexOf("/");
return slash === -1 ? trimmed : trimmed.slice(slash + 1);
return bareModelName(normalizeRateKey(model));
}

function bareModelName(key: string): string {
const slash = key.lastIndexOf("/");
return slash === -1 ? key : key.slice(slash + 1);
}

/**
Expand All @@ -99,9 +136,10 @@ const UNPRICEABLE_MODELS = new Set([
]);

export function lookupRate(table: RateTable, model: string): ModelRate | null {
const normalized = normalizeModelName(model);
if (normalized.length === 0 || UNPRICEABLE_MODELS.has(normalized)) return null;
return table.get(normalized) ?? null;
const key = normalizeRateKey(model);
const bareName = bareModelName(key);
if (bareName.length === 0 || UNPRICEABLE_MODELS.has(bareName)) return null;
return table.get(key) ?? null;
}

export interface PricedUsage {
Expand Down
2 changes: 1 addition & 1 deletion 2 apps/server/src/usage/usageScanCache.test.ts
Show comments
View file
Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ describe("scan cache round trip", () => {

it("rejects the whole cache when an intern table holds a non-string", () => {
// models: [1] would pass the undefined guard, put a number in a record's
// model, and crash normalizeModelName at aggregate time.
// model, and crash lookupRate at aggregate time.
const encoded = encodeScanCache(cacheWith([["/a.jsonl", 100, [record()]]]));
const poisoned = { ...encoded, models: [1] };

Expand Down
2 changes: 1 addition & 1 deletion 2 apps/server/src/usage/usageScanCache.ts
Show comments
View file
Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ export function decodeScanCache(document: unknown): ScanCache {

// The intern tables must be all strings: a numeric entry would pass the
// undefined guard below, land in a record's model, and crash the aggregate
// at normalizeModelName. A corrupt table rejects the whole cache.
// at lookupRate. A corrupt table rejects the whole cache.
if (!root.models.every((value) => typeof value === "string")) return cache;
if (!root.sessions.every((value) => typeof value === "string")) return cache;
const models = root.models as readonly string[];
Expand Down
Loading
Oops, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

Add this suggestion to a batch that can be applied as a single commit. This suggestion is invalid because no changes were made to the code. Suggestions cannot be applied while the pull request is closed. Suggestions cannot be applied while viewing a subset of changes. Only one suggestion per line can be applied in a batch. Add this suggestion to a batch that can be applied as a single commit. Applying suggestions on deleted lines is not supported. You must change the existing code in this line in order to create a valid suggestion. Outdated suggestions cannot be applied. This suggestion has been applied or marked resolved. Suggestions cannot be applied from pending reviews. Suggestions cannot be applied on multi-line comments. Suggestions cannot be applied while the pull request is queued to merge. Suggestion cannot be applied right now. Please check back later.
You are visitor 01402809 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.