agent.serve() starts a runtime that runs messaging interfaces, an HTTP server, and scheduled tasks concurrently. One line to start everything.
Quick Start
serve() / aserve()
agent.serve() is a blocking sync call. Use agent.aserve() in async contexts.
Interface instances (Telegram, Discord, Signal) to run concurrently.
Display name for logs. Defaults to the agent’s name.
HTTP server bind address.
HTTP server port.
Force the HTTP server on or off. When
None, the server starts automatically if webhooks are registered.Enable hot-reload dev mode. Watches
.py files and auto-restarts on changes. Requires watchfiles.Webhooks
Register HTTP webhook handlers with theWebhook trigger:
Webhook Parameters
URL path for the webhook (e.g.,
"/github").HTTP method to listen on.
Per-webhook auth override.
None inherits from agent.auth. False disables auth for this endpoint.Handler Return Values
The return value from a webhook handler controls what happens next:| Return | Behavior |
|---|---|
str | Runs the agent with the string as input |
dict | Runs the agent with the dict as keyword arguments |
None | No-op (acknowledge without running the agent) |
TriggerEvent
Every trigger handler receives aTriggerEvent:
| Field | Type | Description |
|---|---|---|
body | dict | None | Parsed JSON body (webhooks) or event data |
headers | dict | None | HTTP headers (webhooks only) |
source | str | Trigger identifier (e.g., "POST /github") |
timestamp | float | Unix timestamp of the event |
raw | Any | Raw request object |
Cron Jobs
Schedule tasks with standard cron expressions:Standard cron expression (e.g.,
"0 9 * * *").Timezone for the schedule.
Requires
croniter: pip install croniter or pip install 'definable[cron]'Event Triggers
Fire triggers programmatically from anywhere in your code:emit() does not block or return a result.
Lifecycle Hooks
Run logic before every agent run or after every response:- Hooks are always non-fatal — errors are logged but never raised
- Both sync and async functions are supported
- Both
@agent.before_requestand@agent.before_request()syntax work before_requestreceives aRunContext,after_responsereceives aRunOutput
HTTP Server
The built-in server is powered by FastAPI and provides these endpoints:| Method | Path | Description |
|---|---|---|
POST | /run | Invoke the agent with {input, session_id, user_id} |
GET | /health | Health check (always public) |
* | Webhook paths | Auto-registered from Webhook triggers |
agent.auth is set, all endpoints except /health require authentication. See Authentication for details.
Requires
fastapi and uvicorn: pip install fastapi uvicorn or pip install 'definable[serve]'Dev Mode
Enable hot-reload for development:- Watches
.pyfile changes and auto-restarts the process - Swagger docs available at
/docs - Requires
watchfiles:pip install watchfiles
With Interfaces
Combine messaging interfaces, webhooks, cron, auth, and hooks in a singleserve() call:
How It Works
AgentRuntime orchestrates up to three concurrent tasks:
- HTTP server — FastAPI app serving
/run,/health, and webhook routes - Interface supervisor — runs messaging interfaces (Telegram, Discord, etc.) with exponential backoff auto-restart (1s → 60s)
- Cron scheduler — tracks next-fire times and dispatches cron handlers
SIGINT/SIGTERM cancels all tasks and waits for clean exit.