Skip to content

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

Closed
gignit/opencode
#3

Description

@OddballGreg

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 fix(provider): add OAuth token refresh support for GitLab provider #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)

Activity

  1. github-actions commented on Feb 4, 2026

    @github-actions
    Contributor

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

    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.

  2. OddballGreg commented on Feb 4, 2026

    @OddballGreg
    Author

    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 up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

bugSomething isn't working

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions