Documentation menu
Documentation

How agents work

An agent instance is a durable, addressable micro-server with its own state, storage, connections, and work.

Class, route, instance

A folder such as agents/support/agent.ts defines an agent class and the route /support/:name. The class describes behavior; the final path segment selects one durable instance.

The same name always resolves to the same instance. That is the unit to model around: one user, game, coding session, project, or coordination boundary.

PartExampleMeaning
ClassSupportAgentShared behavior and lifecycle
Route/supportHuman-facing address
Instancecustomer-42Durable identity and isolated SQLite

Lifecycle

onStart() runs when an instance starts or wakes. HTTP reaches onRequest(). Realtime connections use onConnect(), onMessage(), onClose(), and onError(). State changes notify onStateChanged().

Instances can hibernate between events. Persist anything important in state or SQLite; do not treat in-memory fields as durable.

agents/support/agent.ts
import { Agent } from "ayjnt";

type State = { status: "idle" | "working"; task?: string };

export default class SupportAgent extends Agent<State> {
  initialState: State = { status: "idle" };

  async onStart() {
    console.log("ready", this.name);
  }

  async onRequest() {
    return Response.json(this.state);
  }
}