Skip to content

Auto compaction is not triggered by code: context_length_exceeded in API call errors #17746

Closed

Description

@ghost91-

Description

When the inference provider responds with something like

{
    "error": {
        "message": "You passed 99073 input tokens and requested 32000 output tokens. However, the model's context length is only 131072 tokens, resulting in a maximum input length of 99072 tokens. Please reduce the length of the input prompt. (parameter=input_tokens, value=99073)",
        "type": "invalid_request_error",
        "param": null,
        "code": "context_length_exceeded"
    }
}

with HTTP status code 400, auto compaction is not triggered.

At the moment, context overflow detection is only based on matching certain message patterns and checking for status code 413.

However, to make it more robust, we should also check against the code context_length_exceeded. It's fairly standardized because that's what OpenAi returns in this case (they use HTTP status 400, btw.).
This code is already being checked in stream errors, so it only makes sense to check for it in API call errors, too.

Plugins

No response

OpenCode version

1.2.26

Steps to reproduce

  1. Configure a provider that returns a message like above on context overflow. If you do not have access to such a provider, you can fake it by using a proxy that adapts the response accordingly.
  2. Send a large message that will exceed the context limit to the provider.
  3. Observe that you see an error, but auto compaction is not triggered.

Screenshot and/or share link

No response

Operating System

Arch Linux (irrelevant)

Terminal

Alacrtitty (irrelevant)

Activity

  1. added
    coreAnything pertaining to core functionality of the application (opencode server stuff)
    on Mar 16, 2026
  2. github-actions commented on Mar 16, 2026

    @github-actions
    Contributor

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

    Your issue is more specific with a clear root cause analysis (HTTP 400 + code: context_length_exceeded not being checked in non-stream errors), so it may still warrant its own tracking even if related.

  3. ghost91- commented on Mar 16, 2026

    @ghost91-
    ContributorAuthor
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 workingcoreAnything pertaining to core functionality of the application (opencode server stuff)

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions