Skip to content

Instantly share code, notes, and snippets.

@brumar
Created June 5, 2025 13:49
Show Gist options
  • Select an option

  • Save brumar/4dd067342bd92031c21f7c0afae7ae32 to your computer and use it in GitHub Desktop.

Select an option

Save brumar/4dd067342bd92031c21f7c0afae7ae32 to your computer and use it in GitHub Desktop.
shell script to get claude code a bit more "--max-turns" conscious
#!/bin/bash
# insist in your CLAUDE.md to get claude code call this as much as possible
# If CLAUDE.md indicates existing commands, add || ./hook.sh <<args>> at each.
# Usage: ./hook.sh <log_dir> <max_turns> [threshold]
# Example: ./hook.sh .jbsays/logs/ 30 5
log_dir="$1"
max_turns="$2"
threshold="${3:-5}" # Default to 5 if not provided
# Validate input
if [ -z "$log_dir" ] || [ -z "$max_turns" ]; then
echo "Usage: $0 <log_dir> <max_turns> [threshold]"
exit 1
fi
if ! [[ "$max_turns" =~ ^[0-9]+$ ]] || ! [[ "$threshold" =~ ^[0-9]+$ ]]; then
echo "Error: max_turns and threshold must be positive integers"
exit 1
fi
if [ ! -d "$log_dir" ]; then
echo "Error: Directory '$log_dir' does not exist"
exit 1
fi
# Find the most recent .log file in the directory
latest_log=$(ls -t "$log_dir"/*.log 2>/dev/null | head -n 1)
# Exit if no .log file is found
if [ -z "$latest_log" ]; then
echo "Turn 0/$max_turns - No log file found yet"
exit 0
fi
# Count unique message IDs (current turn number)
current_turn=$(grep -o '"id":"msg_[^"]*"' "$latest_log" | sed 's/"id"://g' | sed 's/"//g' | sort -u | wc -l)
# Calculate remaining turns
remaining_turns=$((max_turns - current_turn))
# Display turn information
echo "Turn $current_turn/$max_turns"
# Check if we're within threshold
if [ "$remaining_turns" -le "$threshold" ] && [ "$remaining_turns" -gt 0 ]; then
echo "⚠️ Only $remaining_turns turns remain! Time to commit your progress and update the markdown files so that the next agent benefits from your experience."
elif [ "$remaining_turns" -le 0 ]; then
echo "🛑 Maximum turns reached! This is the last turn - commit any final changes immediately."
else
echo "$remaining_turns turns remaining"
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment