Running the scheduler inside AgentOS with automatic polling

Run the scheduler inside AgentOS with automatic schedule discovery.

This example demonstrates the primary DX for the scheduler: - Setting scheduler=True on AgentOS to enable cron polling - The poller starts automatically on app startup and stops on shutdown - Schedules are created via the REST API (POST /schedules) - The internal service token handles auth between scheduler and agent endpoints

1"""Running the scheduler inside AgentOS with automatic polling.
2
3This example demonstrates the primary DX for the scheduler:
4- Setting scheduler=True on AgentOS to enable cron polling
5- The poller starts automatically on app startup and stops on shutdown
6- Schedules are created via the REST API (POST /schedules)
7- The internal service token handles auth between scheduler and agent endpoints
8
9Run with:
10 .venvs/demo/bin/python cookbook/05_agent_os/scheduler/scheduler_with_agentos.py
11"""
12
13from kern.agent import Agent
14from kern.db.sqlite import SqliteDb
15from kern.models.openai import OpenAIChat
16from kern.os import AgentOS
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 AgentOS with scheduler enabled.
37# This does three things:
38# 1. Registers the /schedules REST endpoints
39# 2. Starts a SchedulePoller on app startup (polls every 15s by default)
40# 3. Auto-generates an internal service token for scheduler -> agent auth
41agent_os = AgentOS(
42 name="Scheduled OS",
43 agents=[greeter, reporter],
44 db=db,
45 scheduler=True,
46 scheduler_poll_interval=15, # seconds between poll cycles (default: 15)
47 # scheduler_base_url="http://127.0.0.1:7777", # default
48 # internal_service_token="my-secret", # auto-generated if omitted
49)
50app = agent_os.get_app()
51
52# --- Run the server ---
53# Once running, create schedules via:
54#
55# curl -X POST http://127.0.0.1:7777/schedules \
56# -H "Content-Type: application/json" \
57# -d '{
58# "name": "greet-every-5-min",
59# "cron_expr": "*/5 * * * *",
60# "endpoint": "/agents/greeter/runs",
61# "payload": {"message": "Say hello!"}
62# }'
63#
64# The poller will pick it up on the next poll cycle and run the agent.
65
66if __name__ == "__main__":
67 agent_os.serve(app="scheduler_with_agentos:app", reload=True)

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 scheduler_with_agentos.py