Skip to content

Commit 4a76454

Browse files
t3dotggclaude
andcommitted
fix(usage): price new models without waiting a day for the rate table
The usage page fetches LiteLLM's rate table once every 24 hours. A model released after the last fetch shows $0.00 until the TTL runs out, and the refresh button did not help. Claude Code's 1M context variant (claude-fable-5-1[1m]) never matched the table at all. Refresh now sends refreshRates to every environment, which refetches the table inside the TTL with a one minute floor. Rate lookup strips a bracketed variant suffix so the 1M tier prices at the base rate. Co-Authored-By: Claude Fable 5.1 <[email protected]>
1 parent 70cd258 commit 4a76454

10 files changed

Lines changed: 101 additions & 12 deletions

File tree

‎apps/mobile/src/features/usage/UsageRouteScreen.tsx‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,17 +79,21 @@ export function UsageRouteScreen() {
7979
window: makeWindow(days, undefined, days === 1 ? "hour" : "day"),
8080
});
8181
};
82+
// An explicit refresh also asks every environment to refetch model pricing,
83+
// so a model released since the last daily fetch stops reading as $0.
84+
// Selecting a range never sets the flag: only the user's refresh does.
8285
const refreshWindow = () => {
8386
const nextWindow = makeWindow(windowDays, undefined, isPast24Hours ? "hour" : "day");
8487
if (
88+
window.refreshRates === true &&
8589
nextWindow.sinceDay === window.sinceDay &&
8690
nextWindow.untilDay === window.untilDay &&
8791
nextWindow.sinceTime === window.sinceTime &&
8892
nextWindow.untilTime === window.untilTime
8993
) {
9094
refresh();
9195
} else {
92-
setWindowSelection({ days: windowDays, window: nextWindow });
96+
setWindowSelection({ days: windowDays, window: { ...nextWindow, refreshRates: true } });
9397
}
9498
};
9599

‎apps/mobile/src/state/usage.ts‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@ export function useUsage(input: UsageSummaryInput): UsageView {
8484
resolution: input.resolution,
8585
sinceTime: input.sinceTime,
8686
untilTime: input.untilTime,
87+
refreshRates: input.refreshRates,
8788
}),
8889
[
8990
input.sinceDay,
@@ -92,6 +93,7 @@ export function useUsage(input: UsageSummaryInput): UsageView {
9293
input.resolution,
9394
input.sinceTime,
9495
input.untilTime,
96+
input.refreshRates,
9597
],
9698
);
9799
const atom = usageByWindowAtom(windowKey);

‎apps/server/src/usage/UsageService.test.ts‎

Lines changed: 42 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,13 @@ import { assert, describe, it } from "@effect/vitest";
88
import * as NodeServices from "@effect/platform-node/NodeServices";
99
import { HostProcessEnvironment } from "@t3tools/shared/hostProcess";
1010
import { UsageDay, type UsageSummaryInput } from "@t3tools/contracts";
11+
import * as Duration from "effect/Duration";
1112
import * as Effect from "effect/Effect";
1213
import * as Exit from "effect/Exit";
1314
import * as Fiber from "effect/Fiber";
1415
import * as Layer from "effect/Layer";
1516
import * as Scheduler from "effect/Scheduler";
17+
import * as TestClock from "effect/testing/TestClock";
1618
import { HttpClient, HttpClientResponse } from "effect/unstable/http";
1719

1820
import * as ServerConfig from "../config.ts";
@@ -65,6 +67,8 @@ const serviceLayers = (input: {
6567
readonly home: string;
6668
readonly settings: Parameters<typeof ServerSettings.layerTest>[0];
6769
readonly onRatesFetch?: () => void;
70+
/** Defaults to an unparsable document so every scan retries the fetch. */
71+
readonly ratesDocument?: unknown;
6872
}) =>
6973
ServerConfig.layerTest(process.cwd(), { prefix: input.prefix }).pipe(
7074
Layer.provideMerge(NodeServices.layer),
@@ -77,7 +81,7 @@ const serviceLayers = (input: {
7781
input.onRatesFetch?.();
7882
// Unparsable rates: every scan retries the fetch, which makes the
7983
// fetch count a boundary-level observation of how many scans ran.
80-
return HttpClientResponse.fromWeb(request, Response.json({}));
84+
return HttpClientResponse.fromWeb(request, Response.json(input.ratesDocument ?? {}));
8185
}),
8286
),
8387
),
@@ -142,6 +146,43 @@ describe("UsageService", () => {
142146
}).pipe(Effect.scoped),
143147
);
144148

149+
it.live("refetches a rate table inside its TTL only when the client asks", () =>
150+
Effect.gen(function* () {
151+
const { transcript, settings, home } = yield* setup;
152+
yield* Effect.promise(() => NodeFSP.writeFile(transcript, claudeLine(1, 5)));
153+
154+
let ratesFetches = 0;
155+
const service = yield* UsageService.make.pipe(
156+
Effect.provide(
157+
serviceLayers({
158+
prefix: "usage-service-rates-refresh-test",
159+
home,
160+
settings,
161+
ratesDocument: {
162+
"claude-fable-5": { input_cost_per_token: 1e-5, output_cost_per_token: 5e-5 },
163+
},
164+
onRatesFetch: () => {
165+
ratesFetches += 1;
166+
},
167+
}),
168+
),
169+
);
170+
171+
const first = yield* service.readSummary(WINDOW);
172+
assert.strictEqual(ratesFetches, 1);
173+
assert.strictEqual(first.pricing.status, "fresh");
174+
175+
// Inside the daily TTL a plain rescan keeps the cached table.
176+
yield* TestClock.adjust(Duration.minutes(2));
177+
yield* service.readSummary(WINDOW);
178+
assert.strictEqual(ratesFetches, 1);
179+
180+
// An explicit refresh fetches again so a newly listed model gets priced.
181+
yield* service.readSummary({ ...WINDOW, refreshRates: true });
182+
assert.strictEqual(ratesFetches, 2);
183+
}).pipe(Effect.scoped, Effect.provide(TestClock.layer())),
184+
);
185+
145186
it.live("does not orphan an in-flight scan when its first caller is interrupted", () =>
146187
Effect.gen(function* () {
147188
const { settings, home } = yield* setup;

‎apps/server/src/usage/UsageService.ts‎

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,9 @@ const LITELLM_RATES_URL =
6363
/** Rates move rarely; a day-old table keeps the page working offline. */
6464
const RATES_TTL_MS = 24 * 60 * 60 * 1000;
6565

66+
/** An explicit refresh ignores the TTL, but not a table fetched this recently. */
67+
const RATES_REFRESH_FLOOR_MS = 60 * 1000;
68+
6669
/**
6770
* Files are filtered by mtime before opening. The slack covers a session whose
6871
* last write lands just before local midnight on the window's first day.
@@ -141,11 +144,15 @@ export const make = Effect.gen(function* () {
141144
/**
142145
* Loads the LiteLLM rate table, preferring a fresh copy and falling back to
143146
* the on-disk snapshot. With neither, every model reports as unpriced rather
144-
* than the page failing.
147+
* than the page failing. `force` refetches inside the TTL so a model that
148+
* LiteLLM added since the last fetch gets priced now.
145149
*/
146-
const ensureRates = Effect.fn("UsageService.ensureRates")(function* () {
150+
const ensureRates = Effect.fn("UsageService.ensureRates")(function* (force: boolean) {
147151
const now = yield* Clock.currentTimeMillis;
148-
if (ratesFetchedAtMs !== null && now - ratesFetchedAtMs < RATES_TTL_MS) return;
152+
if (ratesFetchedAtMs !== null) {
153+
const age = now - ratesFetchedAtMs;
154+
if (age < (force ? RATES_REFRESH_FLOOR_MS : RATES_TTL_MS)) return;
155+
}
149156

150157
if (ratesFetchedAtMs === null) {
151158
const fromDisk = yield* fileSystem.readFileString(ratesCachePath).pipe(
@@ -158,7 +165,7 @@ export const make = Effect.gen(function* () {
158165
rates = parsed;
159166
ratesFetchedAtMs = fromDisk.fetchedAtMs;
160167
ratesStatus = "cached";
161-
if (now - fromDisk.fetchedAtMs < RATES_TTL_MS) return;
168+
if (!force && now - fromDisk.fetchedAtMs < RATES_TTL_MS) return;
162169
}
163170
}
164171
}
@@ -425,9 +432,10 @@ export const make = Effect.gen(function* () {
425432
// Pricing only matters once records are aggregated, so the rate table
426433
// loads while transcripts stream instead of gating them: a cold rates
427434
// fetch on a slow network no longer delays the scan by its own timeout.
428-
const [, scannedDirs] = yield* Effect.all([ensureRates(), collectDirs(windowStartMs)], {
429-
concurrency: 2,
430-
});
435+
const [, scannedDirs] = yield* Effect.all(
436+
[ensureRates(input.refreshRates === true), collectDirs(windowStartMs)],
437+
{ concurrency: 2 },
438+
);
431439

432440
const aggregator = new UsageAggregator({
433441
timeZone: input.timeZone,
@@ -539,6 +547,7 @@ export const make = Effect.gen(function* () {
539547
input.resolution ?? "day",
540548
input.sinceTime ?? null,
541549
input.untilTime ?? null,
550+
input.refreshRates === true,
542551
]);
543552

544553
const readSummary = Effect.fn("UsageService.readSummary")(function* (input: UsageSummaryInput) {

‎apps/server/src/usage/usagePricing.test.ts‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,15 @@ describe("usage pricing", () => {
3131
}
3232
});
3333

34+
it("prices a bracketed context-tier variant at the base model's rate", () => {
35+
const table = parseRateTable({ "claude-fable-5-1": rate(1e-5, 2.5e-7) });
36+
37+
expect(lookupRate(table, "claude-fable-5-1[1m]")).toEqual(
38+
lookupRate(table, "claude-fable-5-1"),
39+
);
40+
expect(lookupRate(table, "anthropic/Claude-Fable-5-1[1m]")).toBeNull();
41+
});
42+
3443
it("adds a bare alias when every qualified entry has the same rate", () => {
3544
const table = parseRateTable({
3645
"provider-a/example-model": rate(1),

‎apps/server/src/usage/usagePricing.ts‎

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,16 @@ function bareModelName(key: string): string {
119119
return slash === -1 ? key : key.slice(slash + 1);
120120
}
121121

122+
/**
123+
* Drops a bracketed variant suffix such as `claude-fable-5-1[1m]`, which
124+
* Claude Code writes for the 1M context tier. The rate table only knows the
125+
* base name, and we price at the base tier anyway.
126+
*/
127+
function stripVariantSuffix(key: string): string {
128+
const bracket = key.indexOf("[");
129+
return bracket === -1 ? key : key.slice(0, bracket);
130+
}
131+
122132
/**
123133
* Models we never price, regardless of the table.
124134
*
@@ -136,7 +146,7 @@ const UNPRICEABLE_MODELS = new Set([
136146
]);
137147

138148
export function lookupRate(table: RateTable, model: string): ModelRate | null {
139-
const key = normalizeRateKey(model);
149+
const key = stripVariantSuffix(normalizeRateKey(model));
140150
const bareName = bareModelName(key);
141151
if (bareName.length === 0 || UNPRICEABLE_MODELS.has(bareName)) return null;
142152
return table.get(key) ?? null;

‎apps/web/src/components/usage/UsagePage.tsx‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,17 +92,21 @@ export function UsagePage() {
9292
window: makeWindow(days, undefined, days === 1 ? "hour" : "day"),
9393
});
9494
};
95+
// An explicit refresh also asks every environment to refetch model pricing,
96+
// so a model released since the last daily fetch stops reading as $0.
97+
// Selecting a range never sets the flag: only the user's refresh does.
9598
const refreshWindow = () => {
9699
const nextWindow = makeWindow(windowDays, undefined, isPast24Hours ? "hour" : "day");
97100
if (
101+
window.refreshRates === true &&
98102
nextWindow.sinceDay === window.sinceDay &&
99103
nextWindow.untilDay === window.untilDay &&
100104
nextWindow.sinceTime === window.sinceTime &&
101105
nextWindow.untilTime === window.untilTime
102106
) {
103107
refresh();
104108
} else {
105-
setWindowSelection({ days: windowDays, window: nextWindow });
109+
setWindowSelection({ days: windowDays, window: { ...nextWindow, refreshRates: true } });
106110
}
107111
};
108112
const windowLabel =

‎apps/web/src/state/usage.ts‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@ export function useUsage(input: UsageSummaryInput): UsageView {
8181
resolution: input.resolution,
8282
sinceTime: input.sinceTime,
8383
untilTime: input.untilTime,
84+
refreshRates: input.refreshRates,
8485
}),
8586
[
8687
input.sinceDay,
@@ -89,6 +90,7 @@ export function useUsage(input: UsageSummaryInput): UsageView {
8990
input.resolution,
9091
input.sinceTime,
9192
input.untilTime,
93+
input.refreshRates,
9294
],
9395
);
9496
const atom = usageByWindowAtom(windowKey);

‎docs/user/usage.md‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,6 @@ completed-turn record will not appear.
1010

1111
Use **Past 24h** for an hourly chart covering the exact rolling 24-hour period. The **7 days**,
1212
**30 days**, and **90 days** ranges use daily resolution. Cost and token toggles update both the
13-
headline and chart, and refreshing rescans every connected environment.
13+
headline and chart. Refreshing rescans every connected environment and refetches model pricing on
14+
each of them, so a newly released model that showed $0.00 gets a price without waiting for the daily
15+
pricing update.

‎packages/contracts/src/usage.ts‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,12 @@ export const UsageSummaryInput = Schema.Struct({
185185
sinceTime: Schema.optional(TrimmedNonEmptyString),
186186
/** Exclusive UTC instant for an hourly rolling window. */
187187
untilTime: Schema.optional(TrimmedNonEmptyString),
188+
/**
189+
* Refetch the model rate table even if the cached copy is inside its TTL.
190+
* Sent by an explicit refresh so a newly released model gets priced without
191+
* waiting a day.
192+
*/
193+
refreshRates: Schema.optional(Schema.Boolean),
188194
});
189195
export type UsageSummaryInput = typeof UsageSummaryInput.Type;
190196

0 commit comments

Comments
 (0)