You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Auto-compaction enabled by default, but context_length_exceeded errors still occur in agent workflows #8089
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.
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.
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.
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.
{"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.
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.
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)
I stopped the model when the following happened and wrote "continue" and it proceeded without error
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.
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.
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!
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
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!
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.
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.
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.
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.).
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:
Activity
github-actions commentedon Jan 12, 2026
This issue might be a duplicate of existing issues. Please check:
Feel free to ignore if none of these address your specific case.
virgildotcodes commentedon Jan 15, 2026
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:
rekram1-node commentedon Jan 15, 2026
Yeah there is a bug specific to some openai models, should be fixed v soon
feat(opencode): add context overflow prevention with configurable thr…
lee-b commentedon Jan 20, 2026
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.
rekram1-node commentedon Jan 20, 2026
If you are using local models be sure you have the context limits configured correctly @lee-b
TomLucidor commentedon Jan 21, 2026
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.
carljuneau commentedon Jan 21, 2026
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.
roryeckel commentedon Feb 4, 2026
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.
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 remaining items
diegonix commentedon Feb 9, 2026
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.PangeranSilaen commentedon Feb 10, 2026
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_exceedederrors once usage reached ~270K (especially in agent/sub-agent workflows).A practical workaround that solved it for me was adding guidance in
AGENTS.mdto enforce a soft token ceiling:
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.
bradleat commentedon Feb 10, 2026
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!
test(compaction): add failing tests for missing headroom when limit.i…
prob32 commentedon Feb 12, 2026
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
prob32 commentedon Feb 12, 2026
Yes i've only run into this with sub agents as well - which is worse with OMO!
Jamie-Clayton commentedon Feb 13, 2026
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.
lee-b commentedon Feb 27, 2026
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.
diegonix commentedon Feb 27, 2026
How have you been doing this @lee-b ? It wasn't clear to me.
lee-b commentedon Feb 28, 2026
In
~/.config/opencode/opencode.json, create a provider section like this:Change
baseURLandapiKeyto suit your server, of course, andmy-modelto match the model name that your server lists if youcurl ../v1/modelson it.Then it've just between tweaking the
contextto suit whichever model I have configured on the server, and subtractinginputfrom that to set theoutputvalue too.inputshould 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"frommodalitiesif your model can't do vision.I'm not sure about the
reasoning/tool_call/variantspart, but this basicproviderconfig section works for me, with multiple models (qwen3.5, minimax, step-3.5-flash, etc.).diegonix commentedon Mar 6, 2026
@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:
github-actions commentedon Jul 6, 2026
To stay organized issues are automatically closed after 60 days of no activity. If the issue is still relevant please open a new one.