[FEATURE] Add --keep flag to /clear command to preserve last N messages

Resolved 💬 4 comments Opened Dec 24, 2025 by douglance Closed Dec 24, 2025

Problem Statement

When working on long sessions, /clear removes all context including recent relevant exchanges. /compact only summarizes but doesn't reduce context noise. Users need a middle ground - the ability to reset context while keeping the last few messages that are immediately relevant to their current task.

Proposed Solution

Add an optional --keep=N parameter to /clear:

/clear --keep=5    # Clear all but last 5 messages
/clear -k 5        # Short form

Each message (user or assistant) counts as 1, preserving complete message structure including tool uses.

Alternative Solutions

  • Using /compact (doesn't reduce noise, only compresses)
  • Manual copy-paste of recent context (error-prone, tedious)
  • Starting new session and re-explaining context (wastes tokens)

Use Case Example

  1. Working on a complex debugging session for 30 minutes
  2. Context has grown large with many tangential tool calls
  3. Finally isolated the bug in the last 2-3 exchanges
  4. Want to clear old noise but keep the recent debugging context
  5. /clear --keep=5 preserves last 5 messages, clears everything else
  6. Continue working with clean context and relevant recent history

Implementation Considerations

  • Message Counting: Count individual messages (each user or assistant = 1)
  • Tool Results: Preserve complete message structure including tool use blocks
  • System Messages: Only user/assistant messages count toward N
  • Transcript Handling: Create new transcript with preserved messages
  • Hooks Compatibility: transcript_path should point to new transcript

View original on GitHub ↗

This issue has 4 comments on GitHub. Read the full discussion on GitHub ↗