Skip to content

fix(ai): fetch GitHub Copilot context window limits at runtime#2527

Closed
dpearson2699 wants to merge 1 commit into
earendil-works:mainfrom
dpearson2699:fix/copilot-context-window-limits
Closed

fix(ai): fetch GitHub Copilot context window limits at runtime#2527
dpearson2699 wants to merge 1 commit into
earendil-works:mainfrom
dpearson2699:fix/copilot-context-window-limits

Conversation

@dpearson2699
Copy link
Copy Markdown

Fixes #2526

Problem

GitHub Copilot models have incorrect contextWindow values. Two issues:

  1. Wrong 1M override: generate-models.ts applied a 1M context window to Claude 4.6 models for the github-copilot provider. The Copilot API enforces 200K, not 1M. Result: the coding agent never compacts, eventually hitting prompt-too-long errors.

  2. Stale models.dev data: The github-copilot section of models.dev/api.json reports incorrect context windows for several Claude models (e.g., claude-opus-4.6 listed as 144K, actual Copilot limit is 200K). Verified by querying GET /models on the Copilot API.

Changes

packages/ai/scripts/generate-models.ts

  • Removed github-copilot from the 1M context window override (only anthropic, opencode, opencode-go get 1M for Claude 4.6 models)

packages/ai/src/utils/oauth/github-copilot.ts

  • Added fetchCopilotModelLimits(token, enterpriseDomain?) — queries GET /models on the Copilot API, parses capabilities.limits.max_context_window_tokens and max_output_tokens per model
  • Extended CopilotCredentials type with modelLimits field
  • Called fetchCopilotModelLimits() during loginGitHubCopilot() and refreshToken(), storing results in credentials
  • Updated modifyModels() to apply fetched contextWindow/maxTokens from stored limits

packages/ai/src/models.generated.ts

  • Regenerated with models.dev defaults (no Copilot-specific overrides). Pre-login values are conservative; runtime fetch corrects them after OAuth

Design rationale

Static hardcoded limits would need manual updates whenever GitHub adds or changes models. Fetching at runtime after OAuth login ensures limits are always accurate without maintenance burden. The fetch runs during login and token refresh (tokens expire every ~30 min), so new models or changed limits are picked up automatically. Failure to fetch is non-fatal — models.dev defaults are conservative (lower than real), so compaction triggers early but never overflows.

The models.dev API reports incorrect context window values for several
GitHub Copilot models (e.g., claude-opus-4.6 reported as 144K instead of
the actual 200K enforced by the Copilot API). The previous 1M override
for Claude 4.6 models was also incorrect for the Copilot provider.

Changes:
- Remove github-copilot from the 1M context window override in
  generate-models.ts (only anthropic/opencode providers get 1M)
- Add fetchCopilotModelLimits() that queries the Copilot /models API
  for real max_context_window_tokens and max_output_tokens per model
- Call fetchCopilotModelLimits() during login and token refresh, storing
  limits in CopilotCredentials.modelLimits
- Apply fetched limits in modifyModels() so contextWindow/maxTokens
  reflect the actual Copilot-enforced values after authentication
- Regenerate models.generated.ts with models.dev defaults (no Copilot
  overrides); runtime fetch corrects values after OAuth login
@github-actions
Copy link
Copy Markdown
Contributor

Hi @dpearson2699, thanks for your interest in contributing!

We ask new contributors to open an issue first before submitting a PR. This helps us discuss the approach and avoid wasted effort.

Next steps:

  1. Open an issue describing what you want to change and why (keep it concise, write in your human voice, AI slop will be closed)
  2. Once a maintainer approves with lgtm, you'll be added to the approved contributors list
  3. Then you can submit your PR

This PR will be closed automatically. See https://github.com/badlogic/pi-mono/blob/main/CONTRIBUTING.md for more details.

@hoesler
Copy link
Copy Markdown

hoesler commented May 21, 2026

FWIW: I asked pi to build an extension for this: https://github.com/hoesler/agent-stuff/blob/main/pi/extensions/copilot-model-limits/index.ts

@badlogic
Copy link
Copy Markdown
Collaborator

Closing because the PR author is not listed in .github/APPROVED_CONTRIBUTORS.

@badlogic badlogic closed this May 28, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

GitHub Copilot models have incorrect contextWindow values (stale models.dev data + wrong 1M override)

3 participants