[BUG] Context prune can leave conversation in unrecoverable 400 loop

Status Open
Maintainer reply None cached
Activity 0 comments · opened Sep 13, 2026

Preflight Checklist

  • [x] I have searched existing issues and this hasn't been reported yet
  • [x] This is a single bug report (please file separate reports for different bugs)
  • [x] I am using the latest version of Claude Code

What's Wrong?

After a context prune (either automatic compaction or manual), the conversation history can end on an assistant message. When the inference endpoint rejects trailing assistant messages, every subsequent request fails with 400. Claude Code then appends the error as an assistant message, ensuring the next request also ends on assistant - creating a permanent, self-reinforcing failure loop.

Observed behavior

user       <task-notification>...
assistant  "API Error: 400 Bad Request"
user       <task-notification>...
assistant  "API Error: 400 Bad Request"
... (13+ iterations observed)

The inference endpoint log shows zero incoming requests during these failures.

Environment

  • Backend: custom inference endpoint (localhost proxy)
  • The direct Anthropic API accepts trailing assistant messages (prefill), so this only manifests with custom endpoints

What Should Happen?

Expected behavior

  1. Context prune should validate message structure before committing (ensure no trailing assistant message that could be rejected by the endpoint)
  2. Repeated identical API errors (same status, same message) should halt after N retries rather than accumulating indefinitely
  3. Background hooks should stop firing when the session is in an error state

Error Messages/Logs

Steps to Reproduce

  1. Use Claude Code with a custom inference endpoint that requires the last message to have role=user
  2. Run a long session until context compaction triggers
  3. If the compaction leaves the history ending on an assistant message, every subsequent turn returns 400
  4. Background hooks keep firing, each adding another failed assistant turn
  5. The session becomes unrecoverable - even /exit and --resume replays the same invalid history

Claude Model

Other

Is this a regression?

I don't know

Last Working Version

_No response_

Claude Code Version

v2.1.269

Platform

Anthropic API

Operating System

Windows

Terminal/Shell

WSL (Windows Subsystem for Linux)

Additional Information

_No response_

View original on GitHub ↗