DEV Community

Atajo Studio
Atajo Studio

Posted on

Building your first scheduled agent worker, step by step

A scheduled worker is an agent that wakes up on a timer, does one bounded piece of work, records what happened, and goes back to sleep. Done right, it is the cheapest way to get useful work done while you are not looking.

Done wrong, it fails silently for weeks. Here is how to build one that does not.

Step 1: define the job narrowly

Pick one job with a clear output. "Check new support emails and draft replies" is a job. "Help with the business" is not.

Write down:

  • the input it reads
  • the output it produces
  • what "done" means for one run

Step 2: give it a state file

The worker needs a place to read where things stand and write what it did. Keep one to three tasks for the day, each with a definition of done.

Step 3: choose a fixed time

Pick an hour when you are not using the same account for anything else. At night works well. A fixed time also makes problems easy to spot: if there is no trace of last night's run, it did not run.

Step 4: add the stop rules

Three rules cover most cases:

  1. Do only the task marked for today.
  2. Stop at the first of: done, a time limit, or three failed attempts.
  3. If nothing changed since the last run, do nothing and say so.

Step 5: make failure loud

This is the step people skip. A scheduled job can fail in ways that leave no error on your screen: a permission blocked, a quota exhausted, a path that no longer exists.

  • Write one line to a log on every run, success or not.
  • Write alerts to a file you will read at the start of your day.
  • Make the first thing a new session does be to check that log.

If a run is missing from the log, treat it as a failure.

Step 6: use the cheapest model that works

Routine work belongs on a cheap model. Reserve the expensive one for tasks explicitly marked as needing it.

Step 7: review on a schedule

Choose a day each week to read the log. Ask three questions: did it run every time, did it do something useful, and is it worth what it costs? If the answer is no for a few weeks, turn it off.

A final check before you trust it

  • Can you tell, in ten seconds, whether last night's run happened?
  • If it breaks, will anything tell you?
  • Is there a limit on how much it can do or spend in one run?

If you can answer yes to all three, you have a worker you can leave alone.

Top comments (0)