Basic scheduled agent run.

Starts an AgentOS with the scheduler enabled.

Starts an AgentOS with the scheduler enabled. After the server is running, use the REST API to create a schedule that triggers an agent every 5 minutes.

1"""Basic scheduled agent run.
2
3Starts an AgentOS with the scheduler enabled. After the server is running,
4use the REST API to create a schedule that triggers an agent every 5 minutes.
5
6Prerequisites:
7 pip install kern-ai[scheduler]
8 # Start postgres: ./cookbook/scripts/run_pgvector.sh
9
10Usage:
11 python cookbook/05_agent_os/scheduler/basic_schedule.py
12
13Then, in another terminal, create a schedule:
14 curl -X POST http://localhost:7777/schedules \
15 -H "Content-Type: application/json" \
16 -d '{
17 "name": "greeting-every-5m",
18 "cron_expr": "*/5 * * * *",
19 "endpoint": "/agents/greeter/runs",
20 "payload": {"message": "Say hello!"}
21 }'
22"""
23
24from kern.agent import Agent
25from kern.db.postgres import PostgresDb
26from kern.models.openai import OpenAIChat
27from kern.os import AgentOS
28
29# ---------------------------------------------------------------------------
30# Create Example
31# ---------------------------------------------------------------------------
32
33db = PostgresDb(
34 id="scheduler-demo-db",
35 db_url="postgresql+psycopg://ai:ai@localhost:5532/ai",
36)
37
38greeter = Agent(
39 id="greeter",
40 name="Greeter Agent",
41 model=OpenAIChat(id="gpt-4o-mini"),
42 instructions=[
43 "You are a friendly greeter. Say hello and include the current time."
44 ],
45 db=db,
46 markdown=True,
47)
48
49app = AgentOS(
50 agents=[greeter],
51 db=db,
52 scheduler=True,
53 scheduler_poll_interval=15,
54).get_app()
55
56# ---------------------------------------------------------------------------
57# Run Example
58# ---------------------------------------------------------------------------
59
60if __name__ == "__main__":
61 import uvicorn
62
63 uvicorn.run(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
9# Optiona: Run PgVector (needs docker)
10./cookbook/scripts/run_pgvector.sh
11
12python basic_schedule.py