Fix for Claude Code API Error 400 - Invalid signature in thinking block.
When using Claude Code with extended thinking mode, you may encounter this error:
API Error: 400 {"type":"error","error":{"type":"invalid_request_error","message":"messages.X.content.0: Invalid `signature` in `thinking`block"}}
This happens when thinking blocks in session files become corrupted.
This tool removes corrupted thinking and redacted_thinking blocks from Claude Code session files, allowing you to resume your conversation.
# Clone the repository
git clone https://github.com/a1exlism/claude-code-cmd-fix-thinking-error.git
cd claude-code-cmd-fix-thinking-error
# Run install script
./install.shThe install script will optionally configure Claude Code hooks for automatic session checking.
# Copy the fix script
cp fix_claude_thinking_error.py ~/.claude/
# Copy the command file (optional, for /fix-thinking-error command)
mkdir -p ~/.claude/commands
cp commands/fix-thinking-error.md ~/.claude/commands/When you encounter the 400 error, use this workflow to fix it without leaving Claude Code:
# Step 1: Clear current corrupted session
/clear
# Step 2: Fix the session file
/fix-thinking-error
# Step 3: Resume the fixed session
/resume
# Then select the session you want to restoreNote:
/clearclears the current conversation context, allowing/fix-thinking-errorto run without triggering the 400 error.
# Fix latest session (default)
python3 ~/.claude/fix_claude_thinking_error.py
# List all session files
python3 ~/.claude/fix_claude_thinking_error.py --list
# Fix all session files
python3 ~/.claude/fix_claude_thinking_error.py --all
# Fix specific file
python3 ~/.claude/fix_claude_thinking_error.py --file /path/to/session.jsonl
# Fix by index (from --list)
python3 ~/.claude/fix_claude_thinking_error.py --index 2
# Skip backup (use with caution)
python3 ~/.claude/fix_claude_thinking_error.py --no-backup/fix-thinking-error # Fix latest session (default)
/fix-thinking-error --list # List all session files
/fix-thinking-error --all # Fix all session filesNote: Using
/fix-thinking-errorinside Claude Code may generate new thinking blocks. If the error persists after fixing, run the script again from an external terminal.
Claude Code hooks are user-defined shell commands that run automatically at different points in the lifecycle. You can configure a hook to automatically check for corrupted thinking blocks when resuming a session.
Add the following to ~/.claude/settings.json:
{
"hooks": {
"SessionStart": [
{
"matcher": "resume",
"hooks": [
{
"type": "command",
"command": "python3 ~/.claude/fix_claude_thinking_error.py --hook-mode --cwd \"$CLAUDE_PROJECT_DIR\""
}
]
}
]
}
}This hook runs on SessionStart (specifically when resuming) and checks session files for corrupted thinking blocks.
| Option | Description |
|---|---|
--list / -l |
List all session files |
--file <path> / -f |
Fix specific file |
--index <n> / -i |
Fix/restore file by index |
--all / -a |
Fix all session files |
--no-backup |
Skip backup creation (use with caution) |
--auto-fix |
Skip confirmation when using --all |
--cwd <path> / -c |
Filter sessions by project directory |
--check |
Check for corrupted thinking blocks |
--hook-mode |
Run as hook, output JSON format |
--list-backups / -lb |
List all backup files |
--restore / -r |
Restore session from backup |
--delete / -d |
Delete backup after restore |
When running multiple Claude Code instances, use --cwd to filter sessions by project:
# Fix only current project's sessions
python3 ~/.claude/fix_claude_thinking_error.py --cwd .
# List sessions for a specific project
python3 ~/.claude/fix_claude_thinking_error.py --cwd /path/to/project --list
# Fix all sessions (all projects)
python3 ~/.claude/fix_claude_thinking_error.pyThe
/fix-thinking-errorcommand automatically uses--cwdto filter to the current project. Use--globalto fix all projects.
python3 ~/.claude/fix_claude_thinking_error.py --list-backups# Restore latest backup
python3 ~/.claude/fix_claude_thinking_error.py --restore
# Restore specific backup by index
python3 ~/.claude/fix_claude_thinking_error.py --restore --index 2
# Restore and delete backup file
python3 ~/.claude/fix_claude_thinking_error.py --restore --deleteIMPORTANT: You MUST reload the session after fixing. The fix only modifies the file on disk, not the current session in memory.
# In Claude Code, reload the session
/resume
# Then select the current session to reload it
⚠️ DO NOT continue using the current session after fixing. The error will persist until you/resumeto reload the fixed session data.
- Locates Claude Code session files in
~/.claude/projects/ - Creates a timestamped backup (e.g.,
session.jsonl.bak.20250109_120000) - Removes
thinkingandredacted_thinkingcontent blocks from messages - Writes the cleaned session file
claude-thinking-fix/
├── README.md # This file
├── install.sh # Installation script
├── fix_claude_thinking_error.py # Main fix script
└── commands/
└── fix-thinking-error.md # Claude Code command file
- Python 3.6+
- No external dependencies (uses only standard library)
| Platform | Status | Notes |
|---|---|---|
| Linux | ✅ Supported | Native support |
| macOS | ✅ Supported | Native support |
| WSL | ✅ Supported | Recommended for Windows users |
| Windows | ❌ Not tested | Contributions welcome |
MIT License