Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 9 additions & 2 deletions src/cost/pricing-fetcher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@ import { mkdir } from "node:fs/promises";
import { homedir } from "node:os";
import { dirname, join } from "node:path";

import { getLogger } from "@intx/log";
import { type } from "arktype";
import { SETTINGS_DIR_NAME } from "../branding.js";
import { LOG_NAMESPACE_ROOT, SETTINGS_DIR_NAME } from "../branding.js";

export interface ModelPricing {
inputPricePerToken: number;
Expand Down Expand Up @@ -47,6 +48,8 @@ const DEFAULT_ENDPOINT = "https://models.dev/api.json";
const DEFAULT_REFRESH_INTERVAL_MS = 24 * 60 * 60 * 1000;
const DEFAULT_FETCH_TIMEOUT_MS = 5000;

const log = getLogger([LOG_NAMESPACE_ROOT, "cost", "pricing"]);

const modelPricingValidator = type({
inputPricePerToken: "number",
outputPricePerToken: "number",
Expand Down Expand Up @@ -288,7 +291,11 @@ export async function loadPricing(
const pricing = await fetchPricing(options);
await writePricingCache(pricing, cachePath);
return pricing;
} catch {
} catch (err) {
// One debug line keeps a failed refresh observable while stale cache carries on.
log.debug("pricing refresh failed, keeping cached pricing: {error}", {
error: err instanceof Error ? err.message : String(err),
});
return readPricingCache(cachePath);
}
}
Expand Down
38 changes: 38 additions & 0 deletions src/pricing-fetcher.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { defined } from "../testkit/defined.js";
import { describe, test, expect } from "bun:test";
import { isAbsolute, join } from "node:path";
import { configureSync, getConfig, resetSync } from "@intx/log";
import {
parseModelsDevContextWindows,
parseModelsDevPricing,
Expand Down Expand Up @@ -259,6 +260,43 @@ describe("loadPricing", () => {

expect(result).toBeNull();
});

test("emits a single debug line when refresh fails", async () => {
const records: { level: string; text: string }[] = [];
const previous = getConfig();
configureSync({
reset: true,
sinks: {
test: (record) => {
records.push({
level: record.level,
text: record.message.map(String).join(""),
});
},
},
loggers: [{ category: [], lowestLevel: "debug", sinks: ["test"] }],
});
try {
const failingFetch = (async (): Promise<Response> => {
throw new Error("network error");
}) as unknown as typeof fetch;
const result = await loadPricing({
fetchImpl: failingFetch,
cachePath: "/tmp/nonexistent-pricing-cache-debug.json",
});
expect(result).toBeNull();
const hits = records.filter(
(record) =>
record.level === "debug" &&
record.text.includes("pricing refresh failed"),
);
expect(hits).toHaveLength(1);
expect(hits[0]?.text).toContain("network error");
} finally {
if (previous === null) resetSync();
else configureSync({ ...previous, reset: true });
}
});
});

// ---------------------------------------------------------------------------
Expand Down
Loading