Stop Losing Context When Claude Code Compacts - Run It Inside tmux with Logging
Stop Losing Context When Claude Code Compacts - Run It Inside tmux with Logging
Claude Code periodically compacts its context to stay within token limits. When it does this, your terminal scrollback gets cleared. That conversation where Claude explained exactly why it made a particular design decision? Gone. The error output that was crucial for debugging? Gone.
The Problem
Context compaction is necessary - Claude cannot hold unlimited history. But the side effect of clearing terminal scrollback means you lose information that the terminal should have preserved. This is especially painful when:
- You need to reference an earlier error message
- Claude explained a complex decision and you want to review the reasoning
- You are running a long session and need to check what happened an hour ago
- Multiple agents are running and you want to compare their outputs
The tmux Fix
Running Claude Code inside tmux with logging enabled captures everything to a file, regardless of what Claude does to the terminal:
# Start a new tmux session
tmux new-session -s claude
# Enable logging for the current pane
# Press Ctrl-b then : to open tmux command mode, then type:
pipe-pane -o 'cat >> ~/claude-logs/session-$(date +%Y%m%d-%H%M%S).log'
Or add this to your .tmux.conf for automatic logging:
bind-key P pipe-pane -o 'cat >> ~/claude-logs/tmux-#{session_name}-#{window_index}-#{pane_index}.log'
Now every character that appears in the terminal gets written to a log file. Context compaction clears the screen, but the log file retains everything.
Practical Setup
Create a logging directory and a simple alias:
mkdir -p ~/claude-logs
# Add to your .zshrc or .bashrc
alias claude-logged='tmux new-session -s claude \; pipe-pane -o "cat >> ~/claude-logs/claude-$(date +%Y%m%d-%H%M%S).log" \; send-keys "claude" Enter'
Now claude-logged starts Claude Code in a tmux session with automatic logging.
Bonus: Reviewing Past Sessions
Your log files become a searchable history of every Claude Code session. Need to find that one-liner Claude generated last Tuesday? Grep through your logs:
grep -r "the thing you remember" ~/claude-logs/
This is especially valuable for teams where multiple people use Claude Code. Share log files to review how Claude approached different problems.
Fazm is an open source macOS AI agent. Open source on GitHub.