OpenCode: prompt is too long, and the field that decides when

A long OpenCode session stops answering and reports:

Don't have an account yet? You need an API key from the gateway before the steps below will work — how the discount works and what you take on. Create one free → (no prepay, $1 minimum top-up)
prompt is too long

That is the Anthropic wording. Point the same session at a different provider and the failure is the same but the string changes, which is why searching for it rarely finds an answer:

ProviderWhat you actually see
Anthropicprompt is too long
OpenAIexceeds the context window
Google Geminiinput token count exceeds the maximum
DeepSeek / OpenRoutermaximum context length is X tokens
xAI (Grok)maximum prompt length is X
GitHub Copilotexceeds the limit of X
Groqreduce the length of the messages
Amazon Bedrockinput is too long for requested model
llama.cppexceeds the available context size
LM Studiogreater than the context length
MiniMaxcontext window exceeds limit
Moonshot / Kimiexceeded model token limit
Cerebras / Mistral400 (no body) or 413 (no body)
anything elsecontext length exceeded

OpenCode recognises all of them as the same ContextOverflowError. The useful thing is not the string It is knowing which side decided the session was too long.

You need a key before the code below runs. Create an account, generate a key, and copy the base URL (https://aicomp.ai/v1). Create one free →
Check current rates → Free to sign up · $1 minimum top-up · No prepayment

The field that decides when it happens

OpenCode does not wait for the provider to reject it. It compacts the session when it thinks the context is nearly full, and the number it uses comes from your config:

{
  "$schema": "https://opencode.ai/config.json",
  "provider": {
    "my-router": {
      "npm": "@ai-sdk/openai-compatible",
      "options": {
        "baseURL": "https://aicomp.ai/v1",
        "apiKey": "{env:MY_GATEWAY_KEY}"
      },
      "models": {
        "claude-sonnet-5": {
          "name": "claude-sonnet-5",
          "limit": { "context": 200000, "output": 64000 }
        }
      }
    }
  }
}

limit.context is the number OpenCode compacts against. The failure mode is declaring a larger number than the endpoint actually accepts. If you declare 200000 and the model behind the endpoint takes 128000, OpenCode keeps sending because it believes there is room, and the provider rejects the request instead. You get the overflow error rather than an automatic compact.

So the first fix is not /compact — it is lowering limit.context to the smallest context in the set of models you might route to, so OpenCode summarises before the endpoint refuses. 200000 is the safe value in the docs only when the endpoint genuinely serves it.

limit.output matters for the same reason on the other end: an output ceiling above what the model will emit produces truncated work rather than an error.

What to do when it fires

/compact summarises the session in place and keeps working. /new starts clean. Both are documented as the fix, and both are after-the-fact, the session has already grown past the point where it was cheap to summarise.

The earlier lever is excluding files. An agent session accumulates every file it read and every command result it saw, and that accumulation is what crosses the line. If the session only needed four files and read forty, the forty are the problem, not the conversation.

Why this is also a cost problem

Context overflow and cost are the same mechanism seen from two ends. Every turn re-sends the accumulated context, so a session running near its ceiling is paying the largest possible input bill on every remaining turn — and a session that overflows near the end has paid for that context several times and produced nothing.

That is the argument for compacting early rather than late: summarising at 60% of the ceiling costs one summarisation and then holds input down for every turn after it, where waiting until 95% means the expensive turns have already happened.

It is also the argument for matching the model to the session. A long session on a model with a large window costs more per turn than the same session on one with a small window and aggressive compaction But the small-window model overflows sooner. The model price index is where the two curves are comparable, because input rate and window size are listed together.

Two neighbours that look similar

Concurrency limit exceeded for user is not context. It is parallel subagents exceeding the key's concurrency allowance, fewer subagents at once, or a higher concurrency limit.

ProviderModelNotFoundError is not context either. Model IDs are exact strings, addressed as providerID/modelID; dated snapshots and older names such as claude-3-5-sonnet-20241022 are not served. The model list refreshes after /models, not automatically.

Both show up in long sessions, which is why they get mistaken for overflow.

Get API access

FAQ

FAQ

Why does the error say different things on different providers?

Because each provider phrases context overflow in its own words — Anthropic reports prompt is too long, OpenAI exceeds the context window, Gemini input token count exceeds the maximum, DeepSeek maximum context length is X tokens. OpenCode maps all of them to the same ContextOverflowError, so the fix is identical regardless of the wording.

What does `limit.context` in opencode.json actually do?

It tells OpenCode when to compact the session, before the provider rejects it. If you declare a larger number than the endpoint accepts, OpenCode never compacts and the request fails with an overflow error instead. Lower it to the smallest context among the models you route to.

Is `/compact` or `/new` the fix?

Both work, and both are after the fact The session has already grown past where summarising was cheap. The earlier lever is excluding files you did not need, and the real fix is a limit.context low enough that OpenCode compacts before the endpoint refuses.

Why do long sessions get expensive before they overflow?

Every turn re-sends the accumulated context, so a session near its ceiling pays the largest possible input cost on every remaining turn. Compacting at 60% of the ceiling costs one summarisation and holds input down after it; waiting until 95% means the expensive turns already happened.

Is `Concurrency limit exceeded for user` the same problem?

No. That is parallel subagents exceeding the key's concurrency allowance, not context size. Run fewer subagents at once, or ask for a higher concurrency limit.

Does the model list update automatically when I change config?

No. OpenCode refreshes the model list after the /models command, not automatically — a change to opencode.json will not appear in the picker until you run it.

Related

Get API access

Rate cards: Claude and GPT