Skip to content

How to Run Claude Code AI Agents on Autopilot Safely

Problem

I woke up this morning and checked my autonomous AI agent “Katie” that had been running for months, creating open source projects and committing code every 15 minutes. The progress report from last night was missing. I checked the project directory and my heart sank—ALL .md files were totally gone. No more memory files, no more soul, no more tools, no more build files, no more project files.

Months of autonomous work wiped out in a single session.

Trial and Error

I had been running my AI agent on autopilot for months with great results. Katie was building fun projects, committing code regularly, and seemed to be working perfectly. I thought I could just let her run continuously in an endless conversation.

But I made several critical mistakes:

  1. No session management: I let the conversation run endlessly without state refresh
  2. No filesystem protection: Katie had full write access to all directories
  3. No automated backups: I relied on hope instead of automation
  4. No pre-session preparation: Each new session started with accumulated context baggage

The incident taught me that autonomous AI agents need structured guardrails. Here’s what I learned about running them safely.

Solution: Scheduled Sessions with State Refresh

The core insight from the autonomous agent community is that endless conversations accumulate bad patterns. Instead of running one long session, I now use scheduled build sessions with automatic state refresh.

Session Management Workflow

I run 4 primary build sessions per day (6am, 12pm, 6pm, 12am), each followed by refinement sessions. The key is preparing the agent with a fresh state before each session:

crontab
# Primary build sessions: 6am, 12pm, 6pm, 12am
0 6,12,18,0 * * * ~/.claude/scripts/start-build-session.sh
# Pre-session prep (15 min before build)
45 5,11,17,23 * * * ~/.claude/scripts/prep-session.sh
# Daily memory compression
0 0 * * * ~/.claude/scripts/compress-memory.sh
# Nightly backups (2am)
0 2 * * * tar -czf ~/backups/claude-$(date +\%Y\%m\%d).tar.gz ~/.claude ~/projects

The pre-session script does three things in sequence:

prep-session.sh
#!/bin/bash
# Step 1: Start fresh with /new
curl -X POST localhost:8080/new
# Step 2: Wait 5 minutes for state to initialize
sleep 300
# Step 3: Ask agent to review memory files
curl -X POST localhost:8080/message -d "Review your memory files"
# Step 4: Wait another 5 minutes
sleep 300
# Step 5: Ask agent to review build prompts and key files
curl -X POST localhost:8080/message -d "Review all build prompts and key files"

Why this works: The /new command clears accumulated context that might contain bad patterns. Then the memory review re-loads only the intentional, curated information the agent needs to know about itself and its projects.

Filesystem Protection

Even with proper session management, I still needed a safety net. One bad decision during a session could erase everything. I implemented predicate-authority filesystem protection:

Filesystem Guard Configuration
# Allow read access to all project files
ALLOW: ~/.claude/** (read)
ALLOW: ~/projects/** (read)
# Require approval for writes to critical files
REQUIRE_APPROVAL: ~/.claude/memory/** (write, delete)
REQUIRE_APPROVAL: ~/.claude/build-files/** (write, delete)
REQUIRE_APPROVAL: ~/projects/** (delete)
# Block bulk delete operations
BLOCK: rm -rf /**
BLOCK: find . -delete

This way, if Katie tries to delete memory files or wipe a project, the operation gets blocked pending my approval.

Automated Backup Strategy

The final layer of protection is automated backups. I use a simple nightly backup script:

backup-claude.sh
#!/bin/bash
BACKUP_DIR=~/backups
DATE_STAMP=$(date +%Y%m%d)
# Create backup directory
mkdir -p "$BACKUP_DIR"
# Backup Claude config and memory
tar -czf "$BACKUP_DIR/claude-$DATE_STAMP.tar.gz" \
~/.claude \
~/projects
# Keep only last 30 days of backups
find "$BACKUP_DIR" -name "claude-*.tar.gz" -mtime +30 -delete
echo "Backup complete: claude-$DATE_STAMP.tar.gz"

Why This Matters

Autonomous agents can produce incredible output—Katie created multiple open source projects, maintained consistent coding patterns, and worked 24/7 without burnout. But one bad decision erased months of work.

The solution combines three principles:

  1. Prevention: Session management with state refresh prevents pattern drift
  2. Protection: Filesystem guards catch destructive operations before they execute
  3. Recovery: Automated backups ensure you can always restore from disaster

The setup cost is minimal compared to the risk. A few cron jobs and a backup script take maybe 30 minutes to configure. Losing months of autonomous work takes seconds.

Common Mistakes to Avoid

I learned these the hard way:

  1. Running endless sessions: Context accumulation leads to unpredictable behavior
  2. No filesystem guards: Root-level permissions are too dangerous
  3. Manual backups: If it’s not automated, it won’t happen consistently
  4. No monitoring: Check agent output regularly; silence often means failure

Summary

In this post, I shared how to run Claude Code AI agents autonomously with proper safety measures. The approach uses scheduled build sessions with state refresh, filesystem protection proxies, and automated backups. The key insight is that autonomous agents need structured session management, not endless conversations, to operate safely over extended periods.

Final Words + More Resources

My intention with this article was to help others share my knowledge and experience. If you want to contact me, you can contact by email: Email me

Here are also the most important links from this article along with some further resources that will help you in this scope:

Oh, and if you found these resources useful, don’t forget to support me by starring the repo on GitHub!

Comments