Any way to restart Claude Code without losing the context?

Summary

An operator attempted to upload a 13.5 MB image to a Claude Code session, which triggered a persistent API error loop. The input exceeded the API’s strict 5 MB payload limit. Because the client session retained the invalid request in its context window, every subsequent interaction—regardless of the new prompt—replayed the full error history, including the oversized payload, causing every request to fail identically.

  • Key Takeaway: Context persistence is a double-edged sword; it preserves useful history but also locks you into a fatal execution loop.
  • Error Type: 400 invalid_request_error due to image exceeds 5 MB maximum.
  • Behavior: The session context includes the error response, creating a recursive failure state.

Root Cause

The root cause is a failure to sanitize input size before API transmission, compounded by a lack of error state isolation in the session management.

  1. Input Validation Failure: The user submitted an image file (13,569,456 bytes) without client-side size checks.
  2. API Constraint Violation: The Claude API enforces a hard limit of 5,242,880 bytes for image payloads in the messages.content block.
  3. Context Contamination: Upon receiving the 400 error, the chat interface appended the error response to the conversation history.
  4. Retry Loop: Every subsequent prompt re-serialized the entire context (including the error and the bad input) and sent it to the API, resulting in an immediate rejection with no new processing.

Why This Happens in Real Systems

This scenario is a classic example of poor error boundary handling in conversational AI architectures.

  • Stateless API, Stateful Client: The API is stateless; it processes the full context sent in the request. The client (Claude Code) manages the “state” by accumulating messages.
  • No Auto-Correction: The system does not automatically discard invalid messages that caused previous errors. It assumes the user wants the full history preserved.
  • Garbage In, Garbage Forever: If a single message in the history is malformed or exceeds limits, the entire context becomes unprocessable until that specific message is removed.

Real-World Impact

  • Total Session Stall: The user cannot proceed with the session. All new prompts result in the same API rejection.
  • Loss of Work: To recover, the user must manually copy valid text and restart a new session, losing the previous conversation context.
  • Cognitive Friction: The user is forced to diagnose API-level constraints (5MB limits) rather than focusing on the task at hand.

Example or Code

While we cannot see the internal client code, the error payload is the primary evidence of the request structure.

{
  "type": "error",
  "error": {
    "type": "invalid_request_error",
    "message": "messages.19.content.1.image.source.base64: image exceeds 5 MB maximum: 13569456 bytes > 5242880 bytes"
  },
  "request_id": "req_011CXFSj4rfoEe5kyVm6vTfH"
}

How Senior Engineers Fix It

Senior engineers approach this by adding defensive layers and state isolation.

  1. Implement a “Dead Letter” Queue: The client should catch 4xx validation errors before appending the user prompt or the error to the local history.
  2. Client-Side Validation: Enforce strict file size limits (e.g., if file.size > 5_000_000 throw Error) before attempting to upload or encode base64 strings.
  3. Session Truncation/Correction: Provide a mechanism to “edit” or “rollback” the last message. If the AI detects a context loop (e.g., 3 identical errors in a row), it should suggest truncating the context window to the last valid exchange.
  4. Proxy/Intermediary: For high-reliability systems, place a gateway between the user and the API that sanitizes payloads, stripping out oversized attachments automatically.

Why Juniors Miss It

Junior engineers often focus on the happy path (successful upload) and overlook destructive state (looping errors).

  • Underestimating Context Costs: They often view the context window as infinite or free, not realizing that a single bad message renders the whole window unusable.
  • Lack of Error Propagation Knowledge: They may not understand that the API response is being fed back into the input stream of the next request.
  • UI/UX Focus: Juniors prioritize the ease of file drag-and-drop (making it easy to upload large files) over the backend constraints (5MB limits), assuming the backend will handle it.