Context length exceeded / prompt is too long: how to fix it

"Context length exceeded" and "prompt is too long" mean the text you sent, including history, tool results and instructions, is larger than the model's context window. Shorten or summarize the history, start a fresh session, or send less per request. In Claude Code, run /compact, or /clear if compaction fails.

What the error actually means

A model can only read a fixed amount of text at once. That limit is the context window, counted in tokens. When the input alone is larger than the window, the provider refuses the request instead of silently cutting it. Anthropic documents this as a 400 invalid_request_error with the message "prompt is too long". Other providers use similar wording, such as "maximum context length exceeded".

Everything in the request counts: the system prompt, every earlier message, tool definitions, tool results, images, and documents. In an agent, tool results (a big file read, a long log) are usually the biggest part. The reply the model writes also counts toward the window.

Fix it in Claude Code

Claude Code shows messages such as "Prompt is too long" or "Context limit reached". Its error reference recommends these actions, in this order:

  1. Run /context to see what is using space.
  2. Run /compact to summarize the conversation. You can add a focus, for example /compact keep the test output and code changes.
  3. If compaction itself fails, or the conversation is a single huge exchange, run /clear and start fresh. Use /rename first if you want to find the session later.
  4. Switch to a model with a larger window with /model, if one is available to you.

Fix it when you call an API

If you build your own agent, count tokens before you send. Anthropic offers a token counting endpoint for this. Then reduce what you send:

Input plus output can also hit the limit

On Claude 4.5 models and newer, a request is accepted when input plus max_tokens is larger than the window, and generation stops with stop_reason model_context_window_exceeded when it reaches the limit. On older models the API returns a validation error instead. If you see an error even though your input looks small, lower max_tokens.

For local models (Ollama, llama.cpp), the same problem shows up as an n_ctx or context size error. The fix is to raise the context size you start the model with, if your memory allows, or to send less.

Why bigger windows do not make it go away

Many current Claude models have a 1M-token window, and others have 200k. A bigger window delays the error but costs more per request, and Anthropic notes that accuracy and recall can degrade as context grows. The lasting fix is to keep less in context: write what matters to a file or memory, and let each agent read only what its step needs.

Where a node helps, and where it does not

A NodePress node does not make a model's window larger. What it changes is where facts live. Agents in a node share a thread and a memory graph, so a decision or finding can be stored once and read back by name in a fresh session, instead of being carried forward in a growing chat. That can keep each session small enough to stay under the limit. It is a way to avoid the error, not a way to bypass it.

Do it with a NodePress node

  1. Create a node on your own computer and give it a clear name.
  2. Paste the one line from the node page into your agent so it joins over the NodePress MCP server.
  3. Tell the agent to write key decisions and file summaries to the node's memory as it works.
  4. When a session gets long, start a fresh one. The new session reads the memory and the thread instead of a replayed chat.

If one single input is simply too large, such as a huge document that must be read whole, memory will not help. Split it, summarize it in stages, or use a model with a larger window.

Questions people ask

What does "prompt is too long" mean?

The request, including all history, tool results and instructions, is larger than the model's context window. The API returns a 400 invalid_request_error.

How do I fix "prompt is too long" in Claude Code?

Run /compact. If compaction fails, run /clear and start a new session. Use /context to see what is taking space.

Why does it happen in a new session?

Something large is loaded at the start: a big CLAUDE.md, many MCP tool definitions, or a huge pasted file. Check /context and trim it.

Is context length the same as a rate limit?

No. Context length is the size of one request. A rate limit is how much you send over time. See the rate limit page for 429 errors.

Does a 1M-token window fix it?

It raises the ceiling, but requests cost more and recall can degrade as context grows. Keeping less in context is the lasting fix.

Related

Sources

NodePress gives your AI agents one shared place on your own computer. Free to start, no sign-up. Make your first node.