Build with SwarmBuild guide
Run an AI agent on a schedule, or when something happens
An agent that only runs when you ask is half an agent. With Swarm, one call puts any of your agents on a schedule (cron with your timezone, or every few minutes), and another lets your CI or app start it with a signed webhook. Each run is an ordinary job with its own branch, report and cost. We tested both: a scheduled check that cost $0.019 a run, and a webhook that started a coding agent, which found a broken test and fixed it for $0.11.
On a schedule
Start from an agent you already have (see build your own AI agent). Say when, in words:
Run todo-triager every Monday at 9 my time and tell me what it finds.
swarm_schedule_job
{
"worker": "todo-triager",
"project": "tally",
"name": "weekly-todo-triage",
"brief": "Triage the TODO and FIXME comments in this repository. If there are none, say so in one line.",
"schedule": { "kind": "cron", "cron": "0 9 * * 1", "timezone": "Europe/Berlin" }
}To test it we used {"kind": "interval", "interval_seconds": 60} instead, the shortest Swarm allows. Runs started every minute; the one we read cost $0.019. By then the repository's TODO and FIXME had been fixed, and the run said so in one line:
There are no TODO or FIXME comments in this repository.Handy cron lines: 0 8 * * * every morning at 8, 30 18 * * 1-5 weekdays at 18:30, 0 6 1 * * the first of the month. Every run shows in swarm_list_runs with its status and cost.
When something happens: a webhook
For "whenever CI fails" or "when a form comes in", a workflow with a trigger starts one of your agents with what happened. We pointed it at our coding agent:
When CI tells you a test failed, have tally-maintainer find the cause and fix it.
swarm_set_workflow
{
"name": "fix-failed-ci",
"project": "tally",
"on": {
"event": { "source": "webhook", "id": "id" },
"start": { "worker": "tally-maintainer",
"brief": "CI reported a failure: {{event.text}}. Find the cause in this repository and fix it. Done when ./test.sh prints ok." }
},
"steps": []
}"id": "id" names the field that identifies each event, so the same event never runs twice; {{event.text}} is filled from the event's text field. Saving returns a signing key, once. Your sender signs the exact body with HMAC-SHA256 and posts it to the workflow on Swarm's own address (no public listener is opened; reach it over your private network or a tunnel):
BODY='{"id":"ci-run-4521","text":"test.sh failed on main at the latest commit: FAIL: counts for a.txt"}'
SIG=$(printf '%s' "$BODY" | openssl dgst -sha256 -mac HMAC -macopt hexkey:"$SWARM_TRIGGER_KEY" | awk '{print $NF}')
curl -s -X POST "$SWARM_API_URL/v3/workflows/$WORKFLOW_ID/events" \
-H 'Content-Type: application/json' -H "X-Swarm-Trigger-Signature: sha256=$SIG" -d "$BODY"To test it we broke a test on purpose (expecting 15 characters where there are 14), committed it, and sent that event. Swarm answered {"ok":true} and started the coding agent at once. Its report, excerpted:
### Cause of Failure
The latest commit on main ("test: expect 15 chars (deliberately wrong)") changed the expected
character count in test.sh for $tmp/a.txt from 14 to 15. [...] The CLI correctly output 14.
### Solution
- Reverted the regex pattern in test.sh back to expect 14 characters.
- CLI behavior did not change, so no changes to README.md or tally.sh were required.One commit, one file, on its own branch, waiting for review: $0.11. Add a reviewer step and a deliver step, and fixes like this can merge themselves once they pass.
Other ways to start an agent
- Watch a feed:
"source": "pull"with an HTTPSurlandevery_seconds(30 or more) starts the agent for each new item. The first poll only records what is there. - From your own code: the SDK's
workers.triggerwith a token that can start that one worker and nothing else. - Alerts: failed runs raise a signal you can send to your phone, Slack or email.
Questions
How do I run an AI agent on a schedule?
With Swarm, ask your AI to schedule one of your agents. It calls swarm_schedule_job with a cron line and your timezone (or every N seconds, at least 60). Each run is a job with its own report and cost.
Can a webhook start an AI agent?
Yes. A workflow with a webhook trigger starts one of your agents for each signed event, filling the brief from the event's fields. The signing key is shown once when you save it.
Is the webhook exposed to the internet?
No. Swarm listens on its own loopback address and opens no public listener. Your CI reaches it over a private network or a tunnel you set up.
What did the runs in this guide cost?
About $0.02 ($0.019) per scheduled check and $0.11 for the webhook-started fix, on Gemini 3.8 Flash.
Can the same event start two runs?
No. The event id field (set with "id") makes each event run once, even if the sender retries.
Updated 28 October 2026