TOKEN_REFRESH_NEEDED error incorrectly thrown for context overflow (400 prompt too long) #8524

Closed
opened 2026-02-16 18:10:11 -05:00 by yindo · 2 comments
Owner

Originally created by @OddballGreg on GitHub (Feb 4, 2026).

Originally assigned to: @rekram1-node on GitHub.

Description

When a session exceeds the model's context limit, the GitLab provider throws TOKEN_REFRESH_NEEDED instead of reporting the actual error.

Actual error (from logs):

400 {"type":"invalid_request_error","message":"prompt is too long: 218390 tokens > 200000 maximum"}

Displayed error:

GitLabError: TOKEN_REFRESH_NEEDED

Root cause: The error handling in @gitlab/gitlab-ai-provider appears to catch non-401 errors and incorrectly classify them as token issues.

Impact:

  • Users spend significant time debugging authentication issues when the real problem is context overflow
  • This also affects #9949 (OAuth token refresh support) - if that PR lands, it would attempt to refresh tokens on context overflow errors, which would fail and still not surface the real problem

Expected behavior:

  • 400 "prompt too long" errors should be reported as context overflow
  • TOKEN_REFRESH_NEEDED should only be thrown for actual 401 authentication errors
  • Ideally, suggest starting a new session or compacting context
    Environment:
  • opencode version: 1.1.51
  • Provider: gitlab/duo-chat-opus-4-5

Related: #9949, #9711

Plugins

@gitlab/opencode-gitlab-plugin

OpenCode version

1.1.51

Steps to reproduce

  1. Configure opencode with GitLab Duo as the LLM provider
  2. Start a session and have a long conversation (or use tools that return large outputs) until context approaches 200k tokens
  3. Send another message
  4. Observe GitLabError: TOKEN_REFRESH_NEEDED error instead of a context overflow message

Screenshot and/or share link

Image

Operating System

Ubunut 24.04.3 LTS (Noble Numbat)

Terminal

xterm-256color (ZSH)

Originally created by @OddballGreg on GitHub (Feb 4, 2026). Originally assigned to: @rekram1-node on GitHub. ### Description When a session exceeds the model's context limit, the GitLab provider throws TOKEN_REFRESH_NEEDED instead of reporting the actual error. Actual error (from logs): > 400 {"type":"invalid_request_error","message":"prompt is too long: 218390 tokens > 200000 maximum"} Displayed error: > GitLabError: TOKEN_REFRESH_NEEDED **Root cause:** The error handling in @gitlab/gitlab-ai-provider appears to catch non-401 errors and incorrectly classify them as token issues. **Impact:** - Users spend significant time debugging authentication issues when the real problem is context overflow - This also affects #9949 (OAuth token refresh support) - if that PR lands, it would attempt to refresh tokens on context overflow errors, which would fail and still not surface the real problem **Expected behavior:** - 400 "prompt too long" errors should be reported as context overflow - TOKEN_REFRESH_NEEDED should only be thrown for actual 401 authentication errors - Ideally, suggest starting a new session or compacting context Environment: - opencode version: 1.1.51 - Provider: gitlab/duo-chat-opus-4-5 **Related:** #9949, #9711 ### Plugins @gitlab/opencode-gitlab-plugin ### OpenCode version 1.1.51 ### Steps to reproduce 1. Configure opencode with GitLab Duo as the LLM provider 2. Start a session and have a long conversation (or use tools that return large outputs) until context approaches 200k tokens 3. Send another message 4. Observe GitLabError: TOKEN_REFRESH_NEEDED error instead of a context overflow message ### Screenshot and/or share link <img width="2527" height="1128" alt="Image" src="https://github.com/user-attachments/assets/9ae4ae83-94d1-4838-9764-05c231c75f8c" /> ### Operating System Ubunut 24.04.3 LTS (Noble Numbat) ### Terminal xterm-256color (ZSH)
yindo added the bug label 2026-02-16 18:10:11 -05:00
yindo closed this issue 2026-02-16 18:10:11 -05:00
Author
Owner

@github-actions[bot] commented on GitHub (Feb 4, 2026):

This issue might be a duplicate of existing issues. Please check:

  • #10634: Compaction overflow check doesn't account for large tool outputs until the next step, causing context overflow
  • #9427: [FEATURE]: Auto-route to RLM when context overflows
  • #9949: OAuth token refresh support (mentioned in this issue as related)
  • #9711: [BUG] GitLab OAuth token not auto-refreshed on 401 during session

The core issue here is that GitLab provider error handling incorrectly throws TOKEN_REFRESH_NEEDED for 400 context overflow errors instead of actual 401 authentication errors. This is similar to token refresh issues reported in #9711 and #9949, but specific to error classification.

Feel free to ignore if none of these address your specific case.

@github-actions[bot] commented on GitHub (Feb 4, 2026): This issue might be a duplicate of existing issues. Please check: - #10634: Compaction overflow check doesn't account for large tool outputs until the next step, causing context overflow - #9427: [FEATURE]: Auto-route to RLM when context overflows - #9949: OAuth token refresh support (mentioned in this issue as related) - #9711: [BUG] GitLab OAuth token not auto-refreshed on 401 during session The core issue here is that GitLab provider error handling incorrectly throws TOKEN_REFRESH_NEEDED for 400 context overflow errors instead of actual 401 authentication errors. This is similar to token refresh issues reported in #9711 and #9949, but specific to error classification. Feel free to ignore if none of these address your specific case.
Author
Owner

@OddballGreg commented on GitHub (Feb 4, 2026):

Related: PR #8810 adds context overflow prevention with pre-flight token estimation, which would catch this before the request is sent. That PR has been open since Jan 16 - merging it would prevent this class of misleading errors.

Would still be better not to obfuscate the true cause though.

@OddballGreg commented on GitHub (Feb 4, 2026): Related: PR #8810 adds context overflow prevention with pre-flight token estimation, which would catch this before the request is sent. That PR has been open since Jan 16 - merging it would prevent this class of misleading errors. Would still be better not to obfuscate the true cause though.
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: anomalyco/opencode#8524