Running the scheduler inside AgentOS with programmatic schedule creation

Run the scheduler inside AgentOS with programmatic schedule creation.

This example demonstrates: - Setting scheduler=True on AgentOS to enable cron polling - Using ScheduleManager to create schedules directly (no curl needed) - The poller starts automatically on app startup and executes due schedules

1"""Running the scheduler inside AgentOS with programmatic schedule creation.
2
3This example demonstrates:
4- Setting scheduler=True on AgentOS to enable cron polling
5- Using ScheduleManager to create schedules directly (no curl needed)
6- The poller starts automatically on app startup and executes due schedules
7
8Run with:
9 .venvs/demo/bin/python cookbook/05_agent_os/scheduler/demo.py
10"""
11
12from kern.agent import Agent
13from kern.db.sqlite import SqliteDb
14from kern.models.openai import OpenAIChat
15from kern.os import AgentOS
16from kern.scheduler import ScheduleManager
17
18# --- Setup ---
19
20db = SqliteDb(id="scheduler-os-demo", db_file="tmp/scheduler_os_demo.db")
21
22greeter = Agent(
23 name="Greeter",
24 model=OpenAIChat(id="gpt-4o-mini"),
25 instructions=["You are a friendly greeter."],
26 db=db,
27)
28
29reporter = Agent(
30 name="Reporter",
31 model=OpenAIChat(id="gpt-4o-mini"),
32 instructions=["You summarize news headlines in 2-3 sentences."],
33 db=db,
34)
35
36# --- Create schedules programmatically ---
37
38mgr = ScheduleManager(db)
39
40# Create a schedule for the greeter agent (every 5 minutes)
41greet_schedule = mgr.create(
42 name="greet-every-5-min",
43 cron="* * * * *",
44 endpoint="/agents/greeter/runs",
45 payload={"message": "Say hello!"},
46 description="Greet every 5 minutes",
47 if_exists="update",
48)
49print(f"Schedule ready: {greet_schedule.name} (next run: {greet_schedule.next_run_at})")
50
51# Create a schedule for the reporter agent (daily at 9 AM)
52report_schedule = mgr.create(
53 name="daily-news-report",
54 cron="* * * * *",
55 endpoint="/agents/reporter/runs",
56 payload={"message": "Summarize today's top headlines."},
57 description="Daily news summary at 9 AM UTC",
58 if_exists="update",
59)
60print(
61 f"Schedule ready: {report_schedule.name} (next run: {report_schedule.next_run_at})"
62)
63
64# --- Create AgentOS with scheduler enabled ---
65
66agent_os = AgentOS(
67 name="Scheduled OS",
68 agents=[greeter, reporter],
69 db=db,
70 scheduler=True,
71 scheduler_poll_interval=15,
72)
73
74# --- Run the server ---
75# The poller will automatically pick up the schedules created above.
76
77if __name__ == "__main__":
78 import uvicorn
79
80 uvicorn.run(agent_os.get_app(), host="0.0.0.0", port=7777)

Run the Example

1# Clone and setup repo
2git clone https://github.com/kern-ai/kern.git
3cd kern/cookbook/05_agent_os/scheduler
4
5# Create and activate virtual environment
6./scripts/demo_setup.sh
7source .venvs/demo/bin/activate
8
9python demo.py