Agents

Scripts

Keep a page current with a script or a scheduled agent on your Mac.

Each example here is a page, plus a way to keep it current on the Mac that runs Legato: a script that writes it with legato set, run by launchd or cron, or a prompt for an agent with Legato's MCP tools. Both write the same page file, with the same checks; see Pages and Cards.

If the data comes from one HTTP API that answers JSON, you may not need a script at all: a page can name sources that Legato fetches on a schedule itself. Scripts and agents are for the rest: data that needs a few calls, some arithmetic, a command-line tool, or an agent's judgement.

The legato tool in scripts

legato ships inside the app, at /Applications/Legato.app/Contents/Helpers/legato. Scripts run from launchd or cron get a short PATH, so call it by that full path. For your own shell, a link on your PATH works too:

bash
mkdir -p ~/bin
ln -s /Applications/Legato.app/Contents/Helpers/legato ~/bin/legato

legato set <id> reads the whole page from a file or stdin, writes it, and waits for Legato to load it. It prints JSON and exits with status 0, or prints an {"error": …} and exits with status 1, so a script can tell a refused page from a saved one. If Legato isn't running, set opens it. See The legato command for every command.

Free disk space

The smallest useful example: a shell script that puts the startup disk's free space on a page called disk.

sh
#!/bin/sh
# Free space on the startup disk, as the Legato page "disk".
legato=/Applications/Legato.app/Contents/Helpers/legato
free=$(df -g / | awk 'NR==2 {print $4}')
"$legato" set disk <<EOF
{
  "title": "Disk",
  "icon": "internaldrive",
  "cards": [
    { "id": "free", "type": "stat", "label": "Free on the startup disk", "value": $free, "unit": "GB" }
  ]
}
EOF

Save it as ~/bin/disk-page.sh, run chmod +x ~/bin/disk-page.sh, and run it once to see the page. To run it every hour, use a launch agent like the one below, with /bin/sh and the script's path as its ProgramArguments.

Every run sends the whole page. When nothing changed, the version stays the same and changed is false, but the page's update time moves on, so its footer shows the job is still running.

Anthropic API spend

This month's spend on the Anthropic API against a budget you set, from Anthropic's documented cost report in the Admin API. Download api-spend.py: Python 3.9 or later, standard library only.

The page

The script writes the page ai-spend:

  • A pacing bar for the month: the spend so far, your budget as target, and elapsed, the fraction of the month gone. The bar's tick marks where you'd be if exactly on pace, and the bar turns orange when you're ahead of it.
  • A sparkline of each day's spend, from the 2nd of the month on (a sparkline needs two points).
  • A text note when spend runs ahead of pace, with where the month ends at that rate. Once spend reaches the budget, the note's tone turns critical and the page asks for one macOS notification, with an id for the month (over-budget-2026-10), so it notifies once a month however often the job runs. See The notify field.

On the 7th of October at noon, with a $150 budget:

json
{
  "title": "API spend",
  "icon": "sparkles",
  "cards": [
    { "id": "month", "type": "pacing", "label": "October", "value": 88.75, "target": 150, "elapsed": 0.2097, "unit": "USD" },
    { "id": "daily", "type": "sparkline", "label": "Daily spend", "points": [12.51, 30, 0, 20, 25, 0, 1.25], "unit": "USD" },
    { "id": "note", "type": "text", "tone": "warning", "body": "Ahead of pace: at this rate the month ends near $423.27, $273.27 over budget." }
  ]
}

Months and days are UTC, as in the cost report, so in the Americas a new month starts the evening before. Spend usually shows up in the report within five minutes. The report leaves out Priority Tier, which Anthropic bills differently.

What you need

  • An Anthropic Admin API key (sk-ant-admin01-…). Admin keys exist only for organizations: an individual Claude Console account has no Admin API, so set up an organization first. A member with the admin role creates the key on the Console's Admin keys page. An ordinary workspace API key won't work. A Console admin key can do everything the Admin API does, members and API keys included, even though this script only reads the cost report, so guard it like a password and give it an expiry.
  • Legato in /Applications, in its trial or with a license. The script runs /Applications/Legato.app/Contents/Helpers/legato; set LEGATO_CLI to use a legato somewhere else.

Run it

Try it with --dry-run first, which prints the page and writes nothing:

bash
python3 -I api-spend.py --budget 500 --dry-run

Then write it:

bash
python3 -I api-spend.py --budget 500
ai-spend: version 5d41402abc4b2a76, changed, 88.75 of 500.00 USD

--page writes a different page id, for example a page per organization. On a failure it prints what Anthropic answered, or the code and message legato set refused the page with, and exits with status 1. If the page was saved but Legato hasn't shown it yet (it isn't running, or it has no trial or license), the script prints legato's note on stderr and still succeeds.

Keep the key out of source control

Don't write the admin key into the script, a plist, a crontab or a repository. Put it in a file only you can read, outside any repository:

bash
mkdir -p ~/.config/legato
cat > ~/.config/legato/api-spend.env <<'EOF'
export ANTHROPIC_ADMIN_KEY='sk-ant-admin01-…'
EOF
chmod 600 ~/.config/legato/api-spend.env

The jobs below read that file each time they run. If you keep the script in a dotfiles repository, keep the .env file out of it. Legato itself needs no key: legato writes to your own Pages folder.

Run it every hour

The report updates within minutes and Anthropic allows polling as often as once a minute, so you can run this job hourly. The page's footer says how long ago it was written, so a page that reads "updated 2 days ago" means the job has stopped.

With launchd. Save the script as ~/bin/api-spend.py, then this as ~/Library/LaunchAgents/com.legatobar.api-spend.plist, with your own home folder and budget:

xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
  <key>Label</key><string>com.legatobar.api-spend</string>
  <key>ProgramArguments</key>
  <array>
    <string>/bin/sh</string>
    <string>-c</string>
    <string>. "$HOME/.config/legato/api-spend.env" &amp;&amp; exec /usr/bin/python3 -I "$HOME/bin/api-spend.py" --budget 500</string>
  </array>
  <key>StartInterval</key><integer>3600</integer>
  <key>RunAtLoad</key><true/>
  <key>StandardErrorPath</key><string>/Users/you/Library/Logs/api-spend.log</string>
</dict>
</plist>

Load it, and it runs now and every hour after, catching up after the Mac wakes:

bash
launchctl bootstrap gui/$(id -u) ~/Library/LaunchAgents/com.legatobar.api-spend.plist

The key stays in the .env file, not in the plist. /usr/bin/python3 needs the Xcode Command Line Tools; use the path which python3 prints if yours comes from elsewhere. To stop the job, run launchctl bootout gui/$(id -u)/com.legatobar.api-spend.

With cron. cron on the same Mac works too. Run crontab -e and add a line:

7 * * * * . "$HOME/.config/legato/api-spend.env" && python3 -I "$HOME/bin/api-spend.py" --budget 500 >> "$HOME/api-spend.log" 2>&1

Unlike launchd, cron doesn't catch up on a run it missed while the Mac slept.

Or let an agent keep it

An agent on this Mac with Legato's MCP tools and a way to call the Admin API, such as Claude Code with shell access and ANTHROPIC_ADMIN_KEY in its environment, can do the same job. Give it something like this, in a scheduled task, a claude -p job, or by hand:

You keep my Legato "ai-spend" page current. Call Anthropic's cost report: GET https://api.anthropic.com/v1/organizations/cost_report with starting_at set to midnight UTC on the 1st of this month, bucket_width=1d and limit=31, headers x-api-key: $ANTHROPIC_ADMIN_KEY and anthropic-version: 2023-06-01; follow next_page while has_more is true. Each bucket is one UTC day, and each result's amount is a decimal string in cents, so divide by 100. Then call set_page with page_id "ai-spend": title "API spend", icon "sparkles", a pacing card with id "month", label the month's name, value the month's total in dollars, target 500, elapsed the fraction of the month gone and unit "USD"; a sparkline with id "daily" of each day's spend so far if there are at least two days; and, only if value ÷ target is more than elapsed, a text note with id "note" and tone "warning" saying where the month ends at this rate. Use only that documented endpoint. Always send the whole page. If you get an error, fix what its message names and try again.

The script is the cheaper way to run it every hour, since it uses no model at all. The prompt suits an agent that already runs on a schedule and can add the page to its rounds. A scheduled agent shows how to run one from launchd.

A scheduled agent

Any job an agent can do by hand, it can do on a schedule. With Claude Code connected to Legato (see Connect an agent, with --scope user so every folder has it), claude -p runs one prompt and exits, and --allowedTools lets it call set_page without asking:

bash
cd ~/src/app && claude -p "List the open pull requests waiting on my review with gh, and keep my Legato page \"reviews\" current: a list of them, newest first, each linking to its pull request. Always send the whole page." \
  --allowedTools "mcp__legato__set_page" "Bash(gh pr list:*)"

Put that line in a launch agent's ProgramArguments (through /bin/sh -c, as in Run it every hour) to run it every morning or every hour. launchd's PATH is short, so use the path which claude prints instead of plain claude. Each run starts fresh, so put everything the agent needs in the prompt: which page id, which cards, and where the data comes from. Allow only the tools the job needs: here, writing pages and listing pull requests.