Skip to content

Commit aed96bb

Browse files
committed
perf(status): skip unused status pricing lookups
1 parent 71a6260 commit aed96bb

3 files changed

Lines changed: 41 additions & 11 deletions

File tree

src/agents/context.lookup.test.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -358,6 +358,25 @@ describe("lookupContextTokens", () => {
358358
expect(result).toBe(200_000);
359359
});
360360

361+
it("resolveContextTokensForModel treats explicit config as authoritative for read-only misses", async () => {
362+
const loadConfig = vi.fn(() => {
363+
throw new Error("runtime config should not be loaded");
364+
});
365+
mockContextModuleDeps(loadConfig);
366+
const resolveContextTokensForModel = await importResolveContextTokensForModel();
367+
368+
const result = resolveContextTokensForModel({
369+
cfg: { agents: { defaults: {} } } as never,
370+
provider: "openai",
371+
model: "unknown-test-model",
372+
fallbackContextTokens: 123_000,
373+
allowAsyncLoad: false,
374+
});
375+
376+
expect(result).toBe(123_000);
377+
expect(loadConfig).not.toHaveBeenCalled();
378+
});
379+
361380
it("resolveContextTokensForModel: config direct scan prevents OpenRouter qualified key collision for Google provider", async () => {
362381
// When provider is explicitly "google" and cfg has a Google contextWindow
363382
// override, the config direct scan returns it before any cache lookup —

src/agents/context.ts

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -269,11 +269,14 @@ function ensureContextWindowCacheLoaded(): Promise<void> {
269269

270270
export function lookupContextTokens(
271271
modelId?: string,
272-
options?: { allowAsyncLoad?: boolean },
272+
options?: { allowAsyncLoad?: boolean; skipRuntimeConfigLoad?: boolean },
273273
): number | undefined {
274274
if (!modelId) {
275275
return undefined;
276276
}
277+
if (options?.skipRuntimeConfigLoad) {
278+
return lookupCachedContextTokens(modelId);
279+
}
277280
if (options?.allowAsyncLoad === false) {
278281
// Read-only callers still need synchronous config-backed overrides, but they
279282
// should not start background model discovery or models.json writes.
@@ -515,7 +518,10 @@ export function resolveContextTokensForModel(params: {
515518
if (params.provider && ref && !ref.model.includes("/")) {
516519
const qualifiedResult = lookupContextTokens(
517520
`${normalizeProviderId(ref.provider)}/${ref.model}`,
518-
{ allowAsyncLoad: params.allowAsyncLoad },
521+
{
522+
allowAsyncLoad: params.allowAsyncLoad,
523+
skipRuntimeConfigLoad: Boolean(params.cfg),
524+
},
519525
);
520526
if (qualifiedResult !== undefined) {
521527
return qualifiedResult;
@@ -526,6 +532,7 @@ export function resolveContextTokensForModel(params: {
526532
// (e.g. "google/gemini-2.5-pro") this IS the raw discovery cache key.
527533
const bareResult = lookupContextTokens(params.model, {
528534
allowAsyncLoad: params.allowAsyncLoad,
535+
skipRuntimeConfigLoad: Boolean(params.cfg),
529536
});
530537
if (bareResult !== undefined) {
531538
return bareResult;
@@ -537,7 +544,10 @@ export function resolveContextTokensForModel(params: {
537544
if (!params.provider && ref && !ref.model.includes("/")) {
538545
const qualifiedResult = lookupContextTokens(
539546
`${normalizeProviderId(ref.provider)}/${ref.model}`,
540-
{ allowAsyncLoad: params.allowAsyncLoad },
547+
{
548+
allowAsyncLoad: params.allowAsyncLoad,
549+
skipRuntimeConfigLoad: Boolean(params.cfg),
550+
},
541551
);
542552
if (qualifiedResult !== undefined) {
543553
return qualifiedResult;

src/status/status-message.ts

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -869,15 +869,16 @@ export function buildStatusMessage(args: StatusArgs): string {
869869
? activeAuthMode
870870
: (selectedAuthMode ?? activeAuthMode);
871871
const showCost = effectiveCostAuthMode === "api-key" || effectiveCostAuthMode === "mixed";
872-
const costConfig = showCost
873-
? resolveModelCostConfig({
874-
provider: activeProvider,
875-
model: activeModel,
876-
config: args.config,
877-
allowPluginNormalization: false,
878-
})
879-
: undefined;
880872
const hasUsage = typeof inputTokens === "number" || typeof outputTokens === "number";
873+
const costConfig =
874+
showCost && hasUsage
875+
? resolveModelCostConfig({
876+
provider: activeProvider,
877+
model: activeModel,
878+
config: args.config,
879+
allowPluginNormalization: false,
880+
})
881+
: undefined;
881882
const cost =
882883
showCost && hasUsage
883884
? estimateUsageCost({

0 commit comments

Comments
 (0)