Skip to content

Commit b79687d

Browse files
committed
fix(agents): resolve context window for models with slash in ID (e.g. OpenRouter proxy models)
Remove the `!ref.model.includes("/")` guard in `resolveContextTokensForModel()` which blocked qualified cache key lookup for models like `anthropic/claude-sonnet-4` accessed through proxy providers. Two call sites fixed: 1. Explicit provider path (params.provider && ref) -- safe because provider is explicitly given 2. Last resort implicit provider path (!params.provider && ref) -- bare key already failed, trying qualified key has no downside Fixes: max context showing DEFAULT_CONTEXT_TOKENS (200K) instead of actual model's context window for proxy models
1 parent eb1b640 commit b79687d

1 file changed

Lines changed: 10 additions & 8 deletions

File tree

src/agents/context.ts

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -389,7 +389,7 @@ export function resolveContextTokensForModel(params: {
389389
}
390390
// Only do the config direct scan when the caller explicitly passed a
391391
// provider. When provider is inferred from a slash in the model string
392-
// (e.g. "google/gemini-2.5-pro" ref.provider = "google"), the model ID
392+
// (e.g. "google/gemini-2.5-pro" ??ref.provider = "google"), the model ID
393393
// may belong to a DIFFERENT provider (e.g. an OpenRouter session). Scanning
394394
// cfg.models.providers.google in that case would return Google's configured
395395
// window and misreport context limits for the OpenRouter session.
@@ -409,16 +409,16 @@ export function resolveContextTokensForModel(params: {
409409
// When provider is explicitly given and the model ID is bare (no slash),
410410
// try the provider-qualified cache key BEFORE the bare key. Discovery
411411
// entries are stored under qualified IDs (e.g. "google-gemini-cli/
412-
// gemini-3.1-pro-preview 1M"), while the bare key may hold a cross-
412+
// gemini-3.1-pro-preview ??1M"), while the bare key may hold a cross-
413413
// provider minimum (128k). Returning the qualified entry gives the correct
414414
// provider-specific window for /status and session context-token persistence.
415415
//
416416
// Guard: only when params.provider is explicit (not inferred from a slash in
417417
// the model string). For model-only callers (e.g. status.ts log-usage
418418
// fallback with model="google/gemini-2.5-pro"), the inferred provider would
419419
// construct "google/gemini-2.5-pro" as the qualified key which accidentally
420-
// matches OpenRouter's raw discovery entry the bare lookup is correct there.
421-
if (params.provider && ref && !ref.model.includes("/")) {
420+
// matches OpenRouter's raw discovery entry ??the bare lookup is correct there.
421+
if (params.provider && ref) {
422422
const qualifiedResult = lookupContextTokens(
423423
`${normalizeProviderId(ref.provider)}/${ref.model}`,
424424
{
@@ -441,10 +441,12 @@ export function resolveContextTokensForModel(params: {
441441
return bareResult;
442442
}
443443

444-
// When provider is implicit, try qualified as a last resort so inferred
445-
// provider/model pairs (e.g. model="google-gemini-cli/gemini-3.1-pro")
446-
// still find discovery entries stored under that qualified ID.
447-
if (!params.provider && ref && !ref.model.includes("/")) {
444+
// Last resort: try qualified key even when ref.model contains a slash.
445+
// The bare lookup already failed, so there is no downside to trying the
446+
// full qualified key. This covers models like "anthropic/claude-sonnet-4"
447+
// accessed through a proxy provider whose discovery cache stores the
448+
// full qualified ID.
449+
if (!params.provider && ref) {
448450
const qualifiedResult = lookupContextTokens(
449451
`${normalizeProviderId(ref.provider)}/${ref.model}`,
450452
{

0 commit comments

Comments
 (0)