Skip to content

Instantly share code, notes, and snippets.

@florinel-chis
Last active June 18, 2025 09:39
Show Gist options
  • Save florinel-chis/957f88600258fb87b673f977dcd82a96 to your computer and use it in GitHub Desktop.
Save florinel-chis/957f88600258fb87b673f977dcd82a96 to your computer and use it in GitHub Desktop.
bash script to run claude in a loop to implement a project - based on a tasklist.
#!/bin/bash
# Claude Task Loop - Model Usage Aware Version
# Handles model switches and optimizes usage
# Configuration
MAX_ITERATIONS=50
DELAY_BETWEEN_RUNS=5
LOG_FILE="claude_tasks.log"
echo "=== Claude Task Automation ===" | tee "$LOG_FILE"
echo "Starting at $(date)" | tee -a "$LOG_FILE"
iteration=0
model_switch_detected=0
# Count initial pending tasks
initial_pending=$(grep -c "❌ Pending" tasks.md 2>/dev/null || echo 0)
echo "Initial pending tasks: $initial_pending" | tee -a "$LOG_FILE"
# Function to detect model switch
check_for_model_switch() {
local output="$1"
if echo "$output" | grep -qi "limit reached.*using.*Sonnet"; then
echo "⚠️ Model switched from Opus 4 to Sonnet 4" | tee -a "$LOG_FILE"
model_switch_detected=1
return 0
fi
return 1
}
# Main loop
while [ $iteration -lt $MAX_ITERATIONS ]; do
iteration=$((iteration + 1))
# Check if there are pending tasks
if ! grep -q "❌ Pending" tasks.md; then
echo "✅ All tasks completed!" | tee -a "$LOG_FILE"
break
fi
# Count current pending tasks
pending_count=$(grep -c "❌ Pending" tasks.md)
echo "" | tee -a "$LOG_FILE"
echo "=== Iteration $iteration | Pending tasks: $pending_count ===" | tee -a "$LOG_FILE"
# Show which task we're working on
next_task=$(grep "❌ Pending" tasks.md | head -1 | cut -d'|' -f2-3 | xargs)
echo "Working on: $next_task" | tee -a "$LOG_FILE"
# Choose prompt based on whether we've switched models
if [ $model_switch_detected -eq 1 ]; then
# Shorter prompt for Sonnet 4 (might have different limits)
PROMPT="Continue with next '❌ Pending' task from @tasks.md. Implement, test, update to '✅ Done'. Check @tasklist.md for details."
else
# Full prompt for Opus 4
PROMPT="Read @tasks.md which contains a table of tasks. Find the first task with status '❌ Pending' and:
1. Read the task details from the table (Task ID, Task name, Description, and Functional Test requirements)
2. Implement the task according to the description
3. Run all the functional tests listed in the 'Functional Test' column
4. If all tests pass and the task is complete, update the Status column from '❌ Pending' to '✅ Done'
5. If there's a blocker, update the Status to '⚠️ Blocked' and add a note explaining why
6. Preserve the table formatting when updating the status
7. commit the changes done in the project. check the log and past changes if you need to understand the changes done in the project.
8. update documentation if needed, after each set of changes. use the available documentation files in the project for implementing and testing the tasks.
9. If you need more information about the tasks, refer to the @tasklist.md file and documentation from docs folder.
10. Write the progress to the log file: progress.log, verbose output.
Focus on completing one task at a time. After updating the status, check if you can continue with the next pending task. Additional information about the tasks are found in @tasklist.md"
fi
# Run Claude
output=$(claude --continue --permission-mode bypassPermissions -p "$PROMPT" 2>&1)
echo "$output" | tee -a "$LOG_FILE"
# Check for model switch
check_for_model_switch "$output"
# Check how many tasks were completed
new_pending=$(grep -c "❌ Pending" tasks.md)
completed=$((pending_count - new_pending))
if [ $completed -gt 0 ]; then
echo "✓ Completed $completed task(s)" | tee -a "$LOG_FILE"
fi
# Wait before next task
if grep -q "❌ Pending" tasks.md && [ $iteration -lt $MAX_ITERATIONS ]; then
echo "Waiting $DELAY_BETWEEN_RUNS seconds..." | tee -a "$LOG_FILE"
sleep $DELAY_BETWEEN_RUNS
fi
done
# Final summary
final_pending=$(grep -c "❌ Pending" tasks.md 2>/dev/null || echo 0)
total_completed=$((initial_pending - final_pending))
echo "" | tee -a "$LOG_FILE"
echo "=== Summary ===" | tee -a "$LOG_FILE"
echo "Total iterations: $iteration" | tee -a "$LOG_FILE"
echo "Tasks completed: $total_completed" | tee -a "$LOG_FILE"
echo "Tasks remaining: $final_pending" | tee -a "$LOG_FILE"
if [ $model_switch_detected -eq 1 ]; then
echo "Note: Switched from Opus 4 to Sonnet 4 during execution" | tee -a "$LOG_FILE"
fi
echo "Completed at $(date)" | tee -a "$LOG_FILE"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment