Cyber Week: 25% off for life on new servers with .Ends in 6d 15hSee plans

Run Claude Code unattended on an agent box

Let Claude Code work on its own: a script that runs claude -p in headless mode with only the tools you allow, a schedule that starts it, a log you can read, and how to keep the cost in check.

Last updated November 22, 2026

Claude Code is interactive by default: you type, it works, it asks before it runs things. On an agent box you can also let it run on its own, for example every night: "run the tests, fix what fails, write down what you changed". This guide sets that up with Claude Code's headless mode (claude -p), a small script and a schedule.

What you need

  • An AI agent box with the Claude Code template (see the quickstart). The Starter size is enough for most projects.
  • An Anthropic API key saved as ANTHROPIC_API_KEY under API keys on the Agent tab. Unattended runs need a key or a signed-in Claude subscription: if you would rather use your subscription, run claude once in the Terminal and sign in there.
  • Your project in your home folder, for example /home/container/workspace/my-project (clone it with git clone in the Terminal).

1. Try it by hand first

Open the Terminal on the Agent tab and run:

cd ~/workspace/my-project
claude -p "List the three biggest risks in this codebase in one short paragraph each."

-p (or --print) runs one prompt, prints the answer and exits, without the interactive screen. If this works, the key and the project are fine.

2. Write the script

Create ~/bin/claude-nightly.sh (nano ~/bin/claude-nightly.sh in the Terminal, or the Files tab). ~/bin is already on your PATH.

#!/bin/bash
# One unattended Claude Code run, logged to ~/logs/claude-nightly.log
. /etc/profile.d/agent-box.sh   # loads the keys you saved on the Agent tab
cd "$HOME/workspace/my-project" || exit 1
mkdir -p "$HOME/logs"
{
  echo "=== $(date -u '+%Y-%m-%d %H:%M UTC')"
  claude -p "Run the tests with npm test. If any fail, fix the code, run them again, and add a short note about what you changed to NOTES.md." \
    --allowedTools "Read,Edit,Bash(npm test *)" \
    --max-turns 20 \
    --output-format text
  echo "=== exit code $?"
} >> "$HOME/logs/claude-nightly.log" 2>&1

Then make it executable: chmod +x ~/bin/claude-nightly.sh, and run it once: ~/bin/claude-nightly.sh, then tail -n 40 ~/logs/claude-nightly.log.

What the options do:

  • --allowedTools lists what Claude may do without asking. Here it may read and edit files and run npm test, nothing else. A headless run can't ask you, so anything not on the list is refused. Keep the list as short as the job allows.
  • --max-turns stops a run that goes in circles.
  • --output-format text writes plain text to the log. Use json if another script reads the result.

The first line matters: commands that a schedule or the console starts don't load your saved keys by themselves, so the script loads /etc/profile.d/agent-box.sh first.

3. Run it on a schedule

Option A: the Schedules tab (runs at set times)

  1. Open the Schedules tab of your box and press Create Schedule.
  2. Enter a Schedule name, for example Nightly Claude, and pick the time under Schedule timing (Daily, Weekly, a Quick Options preset, or Custom for a cron expression such as 0 3 * * *).
  3. Leave Active on, and switch on Only when online: the box has to be running for the task to reach it.
  4. Press Save schedule, then Add Task on the new schedule.
  5. Set Action to Send Command, type bin/claude-nightly.sh as the Task Payload and press Save task.

At the set time the command goes to your box's console, which runs it as a shell command in your home folder. Use the Run Now button on the schedule to test it straight away, then check the log. The box must stay running for schedules to fire, and a running box is billed per second (never more than its monthly cap).

Option B: a loop in start.sh (runs every few hours, starts with the box)

Your box runs ~/start.sh every time it starts, and starts it again if it stops (see when your agent crashes). For a job every 6 hours, put this in ~/start.sh:

#!/bin/bash
while true; do
  "$HOME/bin/claude-nightly.sh"
  sleep 6h
done

Then type agent restart in the Console. agent status tells you whether it is running.

4. Keep the cost in check

  • Claude Code bills your Anthropic account for every run, separately from your box. Set a monthly spend limit in the Anthropic Console, and keep --max-turns low.
  • Give each run one clear, small job. Long open-ended prompts use far more tokens.
  • Read ~/logs/claude-nightly.log now and then. Rotate it when it grows: mv ~/logs/claude-nightly.log ~/logs/claude-nightly.old.
  • Commit before the run (or let the prompt tell Claude to work on a branch), so any change is easy to review with git diff and undo.
  • Make a backup on the Backups tab before you let it loose on something important.

Related