How AI Agents Run Themselves: Build a Scheduled Agentic System A developer published a from-scratch Python tutorial for building a scheduled agentic system in roughly 150 lines, using a plain model-tool loop, cron scheduling, and a four-table SQLite database (jobs, runs, findings, memory) instead of an agent framework. The design separates human-defined cron jobs from agent-defined scheduling via a schedule_job tool, and confines the agent's network access to a small set of wrapped tool functions that act as its safety boundary. A working, minimal agent in about 150 lines. No framework, no magic. Just a loop, a few tools, a scheduler and a database. Most "AI agent" tutorials stop at a chat box that calls one function. That is a demo, not a system. A real agent does work while you sleep: it wakes up on a schedule, looks at the world, decides what to do, writes down what it learned, and tells you the result. This article builds exactly that, from scratch, in Python. By the end you will have: Strip away the vocabulary and an agent is four things in a loop: The model does not execute anything. It asks for a tool, your code runs it, and the result goes back into the conversation. That is the whole trick. Everything else memory, scheduling, dashboards is ordinary software wrapped around that loop. A chat agent only works when a human types. A scheduled agent works on its own. Cron is the simplest way to get there: a line like 0 6 1 means "every Monday at 06:00". When it fires, you start one run of the agent with a stored goal. There are two ways to schedule: | Pattern | Who decides the schedule | Use it for | |---|---|---| | Human-defined | You write the job and its cron string | Weekly reports, daily monitoring | | Agent-defined | The agent calls a schedule job tool | "Check this competitor again in 3 days" | The second pattern is where agents start to feel alive, and it is also where they can hurt you. We will add it with guardrails near the end. Build the data layer before the agent. It is what turns a clever script into a system you can trust. Four tables are enough: jobs : the goal and the cron string. runs : one row per execution, with status and summary. findings : every tool call and its raw result, stored as JSON. memory : small key/value notes the agent writes for its future self. Findings are a log , memory is a notebook . The log is for you when something breaks. The notebook is for the agent so next week's run does not repeat last week's work. python db.py import sqlite3 DB = sqlite3.connect "agent.db", check same thread=False DB.row factory = sqlite3.Row DB.executescript """ CREATE TABLE IF NOT EXISTS jobs id INTEGER PRIMARY KEY, name TEXT UNIQUE, goal TEXT NOT NULL, cron TEXT NOT NULL, enabled INTEGER DEFAULT 1 ; CREATE TABLE IF NOT EXISTS runs id INTEGER PRIMARY KEY, job id INTEGER, started REAL, status TEXT, summary TEXT ; CREATE TABLE IF NOT EXISTS findings id INTEGER PRIMARY KEY, run id INTEGER, tool TEXT, data TEXT, created REAL ; CREATE TABLE IF NOT EXISTS memory job id INTEGER, key TEXT, value TEXT, PRIMARY KEY job id, key ; """ DB.commit SQLite is fine for one machine. The schema is plain enough to move to Postgres later without changing a line of agent code. Tools are just functions. The agent never touches the network directly; it can only do what these functions allow. That is your safety boundary. I use three kinds of data here: python tools.py import os, requests KEIRO BASE = os.environ "KEIRO BASE URL" copy from your KeiroLabs dashboard KEIRO KEY = os.environ "KEIRO API KEY" def keiro path, payload : r = requests.post f"{KEIRO BASE}{path}", json=payload, timeout=60, headers={"Authorization": f"Bearer {KEIRO KEY}"} r.raise for status return r.json def web search query: str : return keiro "/search", {"query": query} def extract url url: str : return keiro "/extract", {"url": url} def keyword volume keywords: list str : r = requests.post "https://api.dataforseo.com/v3/keywords data/google ads/search volume/live", auth= os.environ "DFS LOGIN" , os.environ "DFS PASSWORD" , timeout=60, json= {"keywords": keywords :20 , "location code": 2840, "language code": "en"} r.raise for status return r.json Endpoint paths and request fields differ by provider and plan. Check each provider's docs and adjust the three small functions above. The rest of the system does not care how they work inside. Now the heart. The model gets the goal, last time's memory, and a list of tools described as JSON schemas. Each turn it either asks for a tool or gives a final answer. python agent.py import json, time, anthropic from db import DB import tools client = anthropic.Anthropic MODEL = "claude-sonnet-5-5" MAX STEPS = 8 TOOL SPECS = {"name": "web search", "description": "Search the live web.", "input schema": {"type": "object", "properties": {"query": {"type": "string"}}, "required": "query" }}, {"name": "extract url", "description": "Read a page as clean text.", "input schema": {"type": "object", "properties": {"url": {"type": "string"}}, "required": "url" }}, {"name": "keyword volume", "description": "Monthly search volume for keywords.", "input schema": {"type": "object", "properties": {"keywords": {"type": "array", "items": {"type": "string"}}}, "required": "keywords" }}, {"name": "remember", "description": "Save a short note for the next run.", "input schema": {"type": "object", "properties": {"key": {"type": "string"}, "value": {"type": "string"}}, "required": "key", "value" }}, def run job job id: int : job = DB.execute "SELECT FROM jobs WHERE id=?", job id, .fetchone run id = DB.execute "INSERT INTO runs job id, started, status VALUES ?,?,? ", job id, time.time , "running" .lastrowid DB.commit notes = {r "key" : r "value" for r in DB.execute "SELECT key, value FROM memory WHERE job id=?", job id, } def remember key, value : DB.execute "INSERT OR REPLACE INTO memory VALUES ?,?,? ", job id, key, value return "saved" handlers = {"web search": tools.web search, "extract url": tools.extract url, "keyword volume": tools.keyword volume, "remember": remember} messages = {"role": "user", "content": f"Goal: {job 'goal' }\n\nYour notes from earlier runs: {json.dumps notes }\n" "Use tools to research, then write the final report."} summary, status = "", "done" try: for in range MAX STEPS : resp = client.messages.create model=MODEL, max tokens=2000, tools=TOOL SPECS, messages=messages, system="You are a careful research agent. Prefer few, targeted tool calls." messages.append {"role": "assistant", "content": resp.content} if resp.stop reason = "tool use": summary = "".join b.text for b in resp.content if b.type == "text" break results = for b in resp.content: if b.type = "tool use": continue try: out = handlers