Skip to content

Auto-compaction enabled by default, but context_length_exceeded errors still occur in agent workflows #8089

Closed as not planned

Description

@diegonix

Description

Auto-compaction enabled by default, but context_length_exceeded errors still occur in agent workflows

Description

According to the opencode documentation, auto-compaction is enabled by default:

https://opencode.ai/docs/config/#compaction

Example default configuration:

{
  "$schema": "https://opencode.ai/config.json",
  "compaction": {
    "auto": true,
    "prune": true
  }
}

However, I am still encountering frequent context_length_exceeded errors during agent execution, which suggests that auto-compaction may not be triggering as expected.

Error message

{
  "type": "error",
  "sequence_number": 2,
  "error": {
    "type": "invalid_request_error",
    "code": "context_length_exceeded",
    "message": "Your input exceeds the context window of this model. Please adjust your input and try again.",
    "param": "input"
  }
}

Expected behavior

With compaction.auto = true and compaction.prune = true, I would expect:

  • Context compaction to occur automatically before the model context window is exceeded.
  • Agent and sub-agent workflows to be protected from context_length_exceeded errors in long-running or multi-step executions.

Environment

  • opencode version: 1.1.14
  • Model: GPT-5.2 (OpenAI)
  • Authentication: OpenAI OAuth via /connect
  • Usage pattern: Build agent invoking multiple custom sub-agents

Questions / Clarification

  • Is auto-compaction currently supported for agent and sub-agent contexts, or only for top-level interactions?
  • Are there known limitations when using multiple agents that could cause compaction not to trigger?
  • Is additional configuration required to ensure compaction works correctly in agent-based workflows?

Any guidance or confirmation of expected behavior would be greatly appreciated. Thanks for the great project!

Plugins

No response

OpenCode version

1.1.4

Steps to reproduce

  • The error appears to occur inside an agent execution, not during a single prompt.
  • I am using the build agent, which in turn invokes multiple custom sub-agents.
  • Over time, the accumulated context across agent/sub-agent interactions seems to exceed the model’s context window.
  • Auto-compaction does not appear to reduce or prune context early enough to prevent this error.

Screenshot and/or share link

No response

Operating System

Ubuntu 24.04

Terminal

Ghostty

Activity

  1. github-actions commented on Jan 12, 2026

    @github-actions
    Contributor

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

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

  2. virgildotcodes commented on Jan 15, 2026

    @virgildotcodes

    I've also been having this occur pretty frequently (2x a day?) as I've been testing Opencode more extensively the last few days. I'm using Codex 5.2 XHigh, and this typically happens on long running tasks.

    Not sure if this is at all helpful, but Codex things the huge git diff output was the culprit:

    User: So what were you doing when we got this error and I had to compact your context?{"type":"error","sequence_number":2,"error":{"type":"invalid_request_error","code":"context_length_exceeded","message":"Your input exceeds the context window of this model. Please adjust your input and try again.","param":"input"}}
    
    Opencode: Right when the context overflow happened, I was in the review step after you said “Continue.” I ran git status -sb and git diff to check uncommitted changes, and the diff for resources/js/features/wallet/providers/web3-provider.tsx was huge and got truncated. I was about to use the explore agent to inspect that file and then proceed with formatting/tests—no new edits were made at that moment.
    
    User: So what do you think caused you to exceed the context length?
    
    Opencode: Most likely the full git diff output (especially the large web3-provider.tsx diff) plus the very long environment/AGENTS instructions pushed the input over the model’s context window. The tool returned a truncated diff, which is a common sign that the response was too large and likely triggered the overflow.
    
  3. rekram1-node commented on Jan 15, 2026

    @rekram1-node
    Collaborator

    Yeah there is a bug specific to some openai models, should be fixed v soon

  4. lee-b commented on Jan 20, 2026

    @lee-b

    Yeah there is a bug specific to some openai models, should be fixed v soon

    It seems to be a problem with the openai-compatible API, rather than specific models. I've seen it on multiple local models (from different AI labs) with vllm and if I recall correctly, llama.cpp too.

  5. rekram1-node commented on Jan 20, 2026

    @rekram1-node
    Collaborator

    If you are using local models be sure you have the context limits configured correctly @lee-b

  6. TomLucidor commented on Jan 21, 2026

    @TomLucidor

    This kinda issue I saw also with Claude Code a while ago, maybe auto-compact can be done much earlier (based on the "dumb zone")? Also as a side note I feel like OpenCode does NOT check the metadata of the model to know how long things should be, especially if I switch to a 131K models instead of a 262K model.

  7. carljuneau commented on Jan 21, 2026

    @carljuneau

    I can replicate the issue.

    Get it every 15 min or so:

    {"type":"error","sequence_number":2,"error":{"type":"invalid_request_error","code":"context_length_exceeded","message":"Your input exceeds the context window of this model. Please adjust your input and try again.","param":"input"}}

    GPT-5.2 Codex on Windows (Powershell). Glad to hear it should be should be fixed soon.

  8. roryeckel commented on Feb 4, 2026

    @roryeckel

    I am getting the same error. For some reason, OpenCode thinks the GPT-5.2-Codex context length is 400k but it's actually the ~270k for Codex Oauth sign in.

    Image

    Just FYI, I do have Oh My OpenCode installed. I don't know if that is going to change anything.

    I was able to work around it by defining openai/gpt-5.2-codex inside my opencode.json.
    Ref: #8465 (comment)

  9. 9 remaining items

  10. diegonix commented on Feb 9, 2026

    @diegonix
    Author

    I stopped the model when the following happened and wrote "continue" and it proceeded without error

    Image

    Yes, the problem is when model is thinking and the context fills out.
    When you stop and send a simple command, the compaction is triggered (something like that).
    I was able to reproduce, by doing: say "hi" to me. This forces the model to answer a couple of tokens without thinking, and context is updated.

  11. PangeranSilaen commented on Feb 10, 2026

    @PangeranSilaen

    I ran into the same issue with GPT-5.3 Codex.

    Even though the advertised context window is ~400K, I was consistently seeing
    context_length_exceeded errors once usage reached ~270K (especially in agent/sub-agent workflows).

    A practical workaround that solved it for me was adding guidance in AGENTS.md
    to enforce a soft token ceiling:

    • don’t let Codex GPT-5.2 / 5.3 sessions exceed ~240K tokens
    • once approaching that range, trigger compaction early (or stop gracefully)
      instead of waiting until the hard limit is hit

    This also helped reduce overly verbose explanations while keeping code output normal.
    Since applying this, I haven’t encountered the error again.

    Might be worth documenting as a recommended threshold for multi-agent workflows.

  12. bradleat commented on Feb 10, 2026

    @bradleat

    Yeah, I'd say this is the issue that I run into the most. There is a version of this error that I run into for Claude as well and it happens like once every hours I am running a model. So several times a day. It's very annoying!

  13. prob32 commented on Feb 12, 2026

    @prob32

    I'm also running into this issue although with a very different enviroment;

    Docker desktop open-code with OMO
    LM Studio running Qwen3

    It's actually running decently but the context window I have is 32k tokens so I run into this very freqeuntly, i've tried adding context window parameters to my models when connecting to open code but it hasn't shown in UI

  14. prob32 commented on Feb 12, 2026

    @prob32

    In the primary agent, such as the Build, most of the time context management is able to handle content removal and maintain the session for a considerable amount of time.

    The biggest problem is in the sub-agents, which seem to lack this capability, and in 100% of cases hit the problem.

    Yes i've only run into this with sub agents as well - which is worse with OMO!

  15. Jamie-Clayton commented on Feb 13, 2026

    @Jamie-Clayton

    OpenCode v1.1.63

    I'm getting the same error just doing simple things with the default Agent, suggesting this is not a Agent specific issue, but an underlying process workflow issue.

    Image
  16. lee-b commented on Feb 27, 2026

    @lee-b

    This seems to work for me, if I religiously set context, input, and output for all models, on any context length change to the model being used. It would be much better if it could just grab the context length from the /models endpoint, set input=max_tokens, and output = context_len - max_tokens, but it works. Have to be careful that input is long enough for any compaction summaries that the model might output, though, which can vary by model.

  17. diegonix commented on Feb 27, 2026

    @diegonix
    Author

    This seems to work for me, if I religiously set context, input, and output for all models, on any context length change to the model being used. It would be much better if it could just grab the context length from the /models endpoint, set input=max_tokens, and output = context_len - max_tokens, but it works. Have to be careful that input is long enough for any compaction summaries that the model might output, though, which can vary by model.

    How have you been doing this @lee-b ? It wasn't clear to me.

  18. lee-b commented on Feb 28, 2026

    @lee-b

    This seems to work for me, if I religiously set context, input, and output for all models, on any context length change to the model being used.

    How have you been doing this @lee-b ? It wasn't clear to me.

    In ~/.config/opencode/opencode.json, create a provider section like this:

      "provider": {
        "my-server": {
          "name": "my-server",
          "npm": "@ai-sdk/openai-compatible",
          "options": {
            "apiKey": "sk-some-key",
            "baseURL": "http://my.server:myport/v1",
            "includeUsage": false
          },
          "models": {
            "my-model": {
              "limit": {
                "context": 65536,
                "input": 47014,
                "output": 18432
              },
              "modalities": {
                "input": [
                  "text",
                  "image"
                ],
                "output": [
                  "text"
                ]
              },
              "reasoning": false,
              "tool_call": false,
              "variants": {
                "reasoning": {},
                "standard": {}
              }
            }
          }
        }
      }
    

    Change baseURL and apiKey to suit your server, of course, and my-model to match the model name that your server lists if you curl ../v1/models on it.

    Then it've just between tweaking the context to suit whichever model I have configured on the server, and subtracting input from that to set the output value too.

    input should really come from the model docs, if the docs tell you -- if it's too small, the model may be cut off before it does a tool call, interrupting the agency/autonomy.

    Remove , "image" from modalities if your model can't do vision.

    I'm not sure about the reasoning/tool_call/variants part, but this basic provider config section works for me, with multiple models (qwen3.5, minimax, step-3.5-flash, etc.).

  19. diegonix commented on Mar 6, 2026

    @diegonix
    Author

    This seems to work for me, if I religiously set context, input, and output for all models, on any context length change to the model being used.

    How have you been doing this @lee-b ? It wasn't clear to me.

    In ~/.config/opencode/opencode.json, create a provider section like this:

      "provider": {
        "my-server": {
          "name": "my-server",
          "npm": "@ai-sdk/openai-compatible",
          "options": {
            "apiKey": "sk-some-key",
            "baseURL": "http://my.server:myport/v1",
            "includeUsage": false
          },
          "models": {
            "my-model": {
              "limit": {
                "context": 65536,
                "input": 47014,
                "output": 18432
              },
              "modalities": {
                "input": [
                  "text",
                  "image"
                ],
                "output": [
                  "text"
                ]
              },
              "reasoning": false,
              "tool_call": false,
              "variants": {
                "reasoning": {},
                "standard": {}
              }
            }
          }
        }
      }
    

    Change baseURL and apiKey to suit your server, of course, and my-model to match the model name that your server lists if you curl ../v1/models on it.

    Then it've just between tweaking the context to suit whichever model I have configured on the server, and subtracting input from that to set the output value too.

    input should really come from the model docs, if the docs tell you -- if it's too small, the model may be cut off before it does a tool call, interrupting the agency/autonomy.

    Remove , "image" from modalities if your model can't do vision.

    I'm not sure about the reasoning/tool_call/variants part, but this basic provider config section works for me, with multiple models (qwen3.5, minimax, step-3.5-flash, etc.).

    @lee-b I tested your approach and it worked very well. Of course, it still doesn't completely eliminate the problem, but it greatly improved usability. My example:

    ...
      "provider": {    
        "openai": {
          "models": {
            "gpt-5.2": {
              "limit": {
                "context": 400000,
                "input": 250000,
                "output": 128000
              },
              "fast": {
                "disabled": true
              },
              "include": ["reasoning.encrypted_content"]
            },
            "gpt-5.2-codex": {
              "limit": {
                "context": 400000,
                "input": 250000,
                "output": 128000
              },
              "fast": {
                "disabled": true
              },
              "include": ["reasoning.encrypted_content"]
            },
    ...
  20. removed
    bugSomething isn't working
    on May 3, 2026
  21. github-actions commented on Jul 6, 2026

    @github-actions
    Contributor

    To stay organized issues are automatically closed after 60 days of no activity. If the issue is still relevant please open a new one.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions