[BUG] tool_search_tool_regex calls inflate the context count and cause a false Context limit reached

Status Open
Reported on v2.1.220
Maintainer reply None cached
Activity 0 comments · opened Aug 27, 2026

Preflight Checklist

  • [x] I have searched existing issues and this hasn't been reported yet
  • [x] This is a single bug report
  • [x] I am using the latest version of Claude Code

One related but different issue exists: https://github.com/anthropics/claude-code/issues/81620 reports another tool that also doubles its own reported usage.

What's Wrong?

Effect on the user. A session shows normal context usage, for example 10 to 30 percent full. One turn later, with no large action between the two turns, the session shows 0 percent context left. Claude Code blocks the session with the message Context limit reached. The user wrote no large message and read no large file. Only /compact or /clear unblocks the session. Both destroy the plan, the TODO state, and the active task.

Trigger. The jump follows a turn where the assistant calls the built-in tool_search_tool_regex tool several times.

Measurement. After one of those turns, Claude Code writes a token count for the turn. The count does not match the size of the request that the client sent. The gap grows on each repeat in the same session:

| Occurrence | Real request size | Count reported by Claude Code | Ratio |
|---|---|---|---|
| 1 | 315,165 | 507,924 | 1.6x |
| 2 | 346,843 | 594,654 | 1.7x |
| 3 | 360,692 | 945,762 | 2.6x |
| 4 | 386,813 | 1,736,170 | 4.5x |

The real request size comes from a record of outbound network traffic. The reported count is the sum of input_tokens, cache_creation_input_tokens, and cache_read_input_tokens in the session transcript.

At occurrence 4, the reported count passes the 1,000,000 token limit of the model. The real conversation stays below half of that limit.

Failure. The next turn fails, and all subsequent turns fail until the user compacts the session. Claude Code shows the Context limit reached banner at that moment. Claude Code also writes a synthetic assistant message into the session transcript file on disk. That message holds model: "<synthetic>", the text Prompt is too long, error: "invalid_request", isApiErrorMessage: true, and every usage field at 0. The screen never shows that text. The banner and the transcript entry are one single event, not two separate errors.

Origin of the failure. The client creates that message. The record of outbound network traffic shows no request for the failed turn. Claude Code stops the send and trusts only its own local count. The API never receives a request of that size, and the API never sends a rejection.

What Should Happen?

The reported context usage for a turn matches the size of the request that the client sends. A session with real usage far below the context limit never shows Context limit reached. When Claude Code stops a turn, the stop reflects a correct token total. The user sees a warning before the count reaches 0 percent, not a sudden jump from a normal value.

Error Messages/Logs

1. On screen. This is all that the user sees:

Context limit reached · /compact or /clear to continue
Context low (0% remaining) · Run /compact to compact & continue

2. In the background. Claude Code writes this into the session transcript file on disk, at ~/.claude/projects/<project>/<session-id>.jsonl. The screen never shows it:

{
  "message": {
    "model": "<synthetic>",
    "role": "assistant",
    "stop_reason": "stop_sequence",
    "content": [{ "type": "text", "text": "Prompt is too long" }],
    "usage": {
      "input_tokens": 0,
      "output_tokens": 0,
      "cache_creation_input_tokens": 0,
      "cache_read_input_tokens": 0
    }
  },
  "error": "invalid_request",
  "isApiErrorMessage": true
}

Part 1 and part 2 are two records of one single event, not two separate errors. The value model: "<synthetic>" marks the message as a product of the client, not a response from the API.

Steps to Reproduce

The repro takes under 2 minutes on Sonnet.

  1. Start a Claude Code session in a project with many registered tools (MCP servers, skills, agents). On-demand tool search must stay active. The test machine sets "ENABLE_TOOL_SEARCH": "true" in the env block of ~/.claude/settings.json, and the test project registers 75 tools.
  2. Send one turn. Tell the assistant to call tool_search_tool_regex 10 times in sequence, with no other action. For the first 5 calls, ask for an empty input: {}, so that each call fails with invalid_tool_input. For the last 5 calls, ask for a valid pattern.
  3. This turn completes. Its reported token count is larger than the real conversation content explains.
  4. Send the same instruction again in the same session, with the same session ID.
  5. This second turn fails at once with the synthetic Prompt is too long message above. The real conversation size stays far below the context limit of the model.

Result across 3 repeated attempts in one test session: 3 of 3 failed the same way, on the second request after the first inflated one.

Claude Model

Not sure / Multiple models

Opus with the 1M token context window, and Sonnet with the 1M token context window. Both models show the bug.

Is this a regression?

Yes, this worked in a previous version

The bug starts at version 2.1.220, on 2026-07-30. The local history shows it on 9 versions between 2.1.220 and the current 2.1.247.

Last Working Version

2.1.197. The local history holds 2,147 turns on 2.1.197 with no occurrence. The history has no session between 2026-07-04 and 2026-07-27, so versions 2.1.198 to 2.1.219 stay untested. 2.1.220 is the first version with a confirmed occurrence.

Claude Code Version

2.1.247 (Claude Code)

Platform

Anthropic API

Operating System

macOS

Terminal/Shell

Terminal.app (macOS)

Additional Information

Tool search setting. The machine sets "ENABLE_TOOL_SEARCH": "true" in the env block of ~/.claude/settings.json. A per-session record of that settings file shows the value present for 17 of the 19 affected sessions. The other 2 sessions ran without the value in that file, and the assistant still called tool_search_tool_regex in both. The setting therefore helps a reproduction, but it is not a condition of the bug.

Scale. A scan of every session file on the machine, back to 2026-07-03, found 38 occurrences of the synthetic Prompt is too long message in sessions that called tool_search_tool_regex. They cover 19 sessions, 7 project directories, and 9 Claude Code versions: 2.1.220, 2.1.223, 2.1.226, 2.1.228, 2.1.237, 2.1.239, 2.1.241, 2.1.246, and 2.1.247. Top-level sessions and subagent sessions both show it.

The same scan found 8 more synthetic Prompt is too long messages in sessions with no call to tool_search_tool_regex. Two of those come from a local background job that sends a whole transcript in one prompt, so a real oversize explains them. This report does not count those 8, and it makes no claim about them.

First reports. Three separate sessions on one day showed the jump from 10 to 30 percent, to 100 percent, between turns. No large message and no large file read explains it. All three show the same tool_search_tool_regex pattern in the transcript.

Transcripts. The table and the repro both come from full local session transcripts (JSONL). Each file holds the raw usage object per turn, the tool calls, and the exact synthetic error message. These transcripts, or extracts from them, are available on request. Name the sessions or the turns that help.

Side observation. The invalid_tool_input failures on the empty-input calls look like a separate bug. This report covers the failure with direct impact on the user: a blocked session until compact.

---

🤖 Claude Code on behalf of @danielmjrr, AI-First Senior QA Engineer and Agentic Transformation Specialist at @trilogy-group

View original on GitHub ↗