Claude Code: Fix For /context Bug With Thinking Enabled

Alex Johnson
-
Claude Code: Fix For /context Bug With Thinking Enabled

Experiencing issues with Claude Code's /context command when extended thinking is enabled? You're not alone! This article dives into a peculiar bug where the /context command fails on the first attempt but mysteriously succeeds on the second try. We'll explore the root cause, offer temporary workarounds, and emphasize the importance of reporting such issues for a more robust and reliable Claude Code experience.

The Issue: A Deep Dive

When extended thinking is enabled in Claude Code, the /context command sometimes throws an error on its initial run. This error, specifically a 400 Bad Request, points to a formatting issue in the message structure sent to the Anthropic API. The API expects the last assistant message to begin with a <thinking> block before any text or tool use when extended thinking is active. Claude Code, however, seems to be sending text first, leading to the error. Retrying the command often works because the subsequent conversation history alters the message structure, aligning it with the API's expectations.

The error message clearly states: messages.373.content.0.type: Expected 'thinking' or 'redacted_thinking', but found 'text'. This indicates that the API is receiving a 'text' type when it anticipates a 'thinking' block at the beginning of the assistant's message. This discrepancy arises due to how Claude Code formats the message when extended thinking is enabled. The retry success hinges on the conversation history being slightly different, which influences how Claude Code constructs the subsequent API call, inadvertently formatting it correctly.

Essentially, the bug lies in Claude Code's handling of the thinking state when invoking the /context command. It fails to consistently adhere to the API's requirement of initiating the last assistant message with a <thinking> block. This inconsistency is what triggers the error on the first attempt. Understanding this nuance is crucial for both developers and users of Claude Code, as it sheds light on the underlying cause of the issue and provides a basis for potential solutions and workarounds. By addressing this formatting discrepancy, Claude Code can ensure seamless compatibility with the Anthropic API when extended thinking is enabled, ultimately enhancing the user experience and fostering greater reliability.

Understanding the Error Message

The error message you might encounter is quite telling:

Error: 400 {"type":"error","error":{"type":"invalid_request_error","message":"messages.373.content.0.type: Expected `thinking` or `redacted_thinking`, but found `text`. When `thinking` is enabled, a final `assistant` message must start with a thinking block (preceeding the lastmost set of `tool_use` and `tool_result` blocks). We recommend you include thinking blocks from previous turns. To avoid this requirement, disable `thinking`. Please consult our documentation at https://docs.claude.com/en/docs/build-with-claude/extended-thinking"},"request_id":"req_011CTgunMYhfTRB5CtNiStG4"}

This message explicitly states that when thinking is enabled, the final assistant message must start with a thinking block. The error arises because Claude Code is sending text before the required thinking block on the first attempt.

Proposed Solutions and Workarounds

Fortunately, there are a few ways to tackle this issue, ranging from quick fixes to more permanent solutions.

Option 1: Disable Extended Thinking (Quick Fix)

The simplest solution is to disable extended thinking altogether. While this might not be ideal for everyone, it immediately resolves the error. You can disable it in Claude Code settings. This is a quick and easy workaround if you don't heavily rely on the extended thinking feature.

Option 2: Retry the Command (Workaround)

As you've already discovered, retrying the /context command often works. The reason? The conversation history changes after the first failed attempt, altering how Claude Code constructs the API call. This change, for some reason, leads to the message being formatted correctly on the second try.

Option 3: Report the Bug (Proper Fix)

The most effective solution in the long run is to report the bug to the Claude Code developers. By reporting the bug, you contribute to the overall improvement of Claude Code. You can report it on the Claude Code GitHub repository: https://github.com/anthropics/claude-code/issues. This allows the developers to investigate the issue, identify the root cause, and implement a permanent fix in a future release. Reporting also helps other users who might be experiencing the same problem.

Why Retrying Works: A Closer Look

The success of retrying the /context command is tied to the dynamic nature of the conversation history. On the initial attempt, your last message might be the assistant's response, consisting of text without a thinking block. However, after the first attempt fails, the error message itself becomes part of the conversation history. This addition alters the subsequent API call made by Claude Code. It's this slight change in context that somehow prompts Claude Code to format the message correctly, including the necessary thinking block, and allowing the second attempt to succeed.

Steps to Reproduce the Bug

Want to see the bug in action? Here's how to reproduce it:

  1. Enable extended thinking in your Claude settings.
  2. Call the /context command using the Sonnet 4.5 model.

Key Information

  • Claude Model: Sonnet 4.5 (the issue was not present in Sonnet 4.2)
  • Claude Code Version: 2.0.2
  • Platform: Anthropic API
  • Operating System: macOS (but likely affects other platforms as well)
  • Terminal/Shell: Terminal.app (macOS)

Regression Analysis

This bug is a regression, meaning it worked in a previous version of Claude Code. Specifically, it was working in Sonnet 4.2 but is now broken in Sonnet 4.5.

Conclusion: A Bug, Not a Feature

In conclusion, the /context command failing on the first try with thinking enabled is indeed a bug in Claude Code. It's not a connection error or a problem on your end. While the retry workaround is a temporary solution, reporting the bug is crucial for a proper fix. By understanding the root cause and contributing to the bug reporting process, you help make Claude Code a more reliable and user-friendly tool. Remember to always keep your Claude Code version updated to benefit from the latest bug fixes and improvements. For more information on Claude and its features, visit the Anthropic website.

You may also like