Skip to content

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

@OddballGreg

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)

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