Skip to content
The AI Agent Factory

35.3 The control plane

Approved by
Panaversity ·
7 min read

In everyday life. An airport's control tower does not fly any plane. It knows every plane, gives each a runway and a slot, and can hold all of them on the ground with one call.

What you will learn. What a small control plane holds, the API Claude Code builds for it, and where the company ID must come from.

Why it matters. Without a control plane, "which workers do we run for Acme?" is answered by searching code and dashboards. With one, it is one query.

The spec

You write spec-platform.md. The control plane part, in short:

## Control plane
- Tables in Neon: tenants, workers, worker_versions, assignments, usage.
- A worker belongs to one tenant. Its definition is versioned; a new
  version never changes a running conversation or process.
- Lifecycle: create, suspend, resume, retire. Suspend sets the worker's
  revocation flag (Chapter 34) and stops new assignments. Retire is final.
- Assignments: every piece of work gets a stable assignment ID and goes to
  exactly one worker of the right tenant.
- Usage: every model call, process step and review minute is written with
  the worker ID, tenant ID and assignment ID.
- The tenant ID always comes from the signed-in user or the assignment
  record. Never from a model, never from the request body.

## Failure tests
1. A user of Acme cannot list, start or suspend a Lahore Foods worker.
2. A suspended worker refuses new assignments and stops at its next
   consequential step (one that acts, such as posting an invoice).
3. A new worker version does not change a conversation already running.
4. The same assignment sent twice creates one assignment.
5. Every model call in a test run has a usage row with all three IDs.

The tables

Step 1. Read spec-platform.md. Write the Neon migration for the control plane tables, with foreign keys and unique constraints. Every table that holds client data has a tenant_id column. Show me the migration before you write any API code.

create table tenants (id text primary key, name text not null, allowed_regions text[] not null);

create table workers (
  id text primary key, tenant_id text not null references tenants(id),
  role text not null, status text not null default 'active'
    check (status in ('active','suspended','retired')),
  current_version int not null
);

create table worker_versions (
  worker_id text references workers(id), version int,
  role_contract text not null, model_policy text not null,
  tools text[] not null, processes text[] not null,
  primary key (worker_id, version)
);

create table assignments (
  id text primary key,                         -- stable, from the source
  tenant_id text not null, worker_id text not null references workers(id),
  worker_version int not null, status text not null, created_at timestamptz default now()
);

create table usage (
  id bigserial primary key, tenant_id text not null, worker_id text not null,
  assignment_id text not null, kind text not null,   -- 'model','process','review'
  activity text, model text, cost_usd numeric(12,6) not null, at timestamptz default now()
);

allowed_regions on the tenant is not decoration. The router in 35.4 reads it.

What to check. Does every table with client data have tenant_id? Is the assignment ID the primary key, so the same assignment cannot be created twice? Does worker_versions keep old versions, rather than overwrite them?

The API

Step 2. Build the control plane as a small API on Workers: create, suspend, resume, retire, list workers, and create an assignment. Take the tenant from the signed-in session only. Suspend must also call revoke() on the worker's coordinator object from Chapter 34. Write failure tests 1 to 4 first.

A shortened version of the part that matters most:

async function createAssignment(req: Request, env: Env) {
  const session = await requireSession(req, env);     // who is signed in
  const body = await req.json();
  const worker = await db(env).workerFor(session.tenantId, body.role);  // tenant from session
  if (!worker || worker.status !== "active") return deny("no active worker");
  const created = await db(env).insertAssignmentOnce({
    id: body.assignmentId, tenantId: session.tenantId,
    workerId: worker.id, workerVersion: worker.current_version,
  });
  return Response.json(created);
}

The tenant comes from session.tenantId, not from body. The worker is looked up within that tenant, so a user of Acme cannot reach a Lahore Foods worker even by guessing its ID. And the assignment records the worker version at the moment it was created, which is how failure test 3 passes.

How the pieces connect. An assignment becomes a conversation in the worker's Durable Object, named by the assignment ID, as in Chapter 33. The object reads its worker version and contract when it starts, and keeps them. Suspending a worker sets the revocation flag in its coordinator object, which every conversation and process already checks, as in Chapter 34. The control plane adds nothing new to those parts; it is the one place that tells them what to do.

A signed-in user at Acme, whose session says tenantId = acme, while a tenantId in the request body is ignored, calls the control plane API. The API can create, suspend, resume, retire and list workers. Its createAssignment takes the tenant from the session, finds the worker within that tenant, and inserts the assignment once, by assignment ID. It writes to Neon tables with tenant_id on every client row: tenants (id, name, allowed regions), workers (tenant, role, status, current version), worker_versions (old versions kept), assignments (id from the source, the primary key) and usage (every call, charged to one worker). An arrow labelled "an assignment becomes a conversation" leads to a conversation object named by the assignment ID, which reads its worker version and role contract once, at the start, and keeps them (Chapter 33). A second arrow, in terracotta, leads to "Suspend a worker", which sets the revocation flag in the worker's coordinator (Chapter 34); every conversation and every Workflow already checks it before a step that acts. A line at the foot says the control plane adds nothing new to the conversations, Workflows or coordinator; it is the one place that knows every worker.

Figure 35.1. The tenant comes from who is signed in. Everything else is looked up inside that tenant.

What to check. Search the API for every place a tenantId is read. Each must come from the session or from a row already found by the session's tenant. Ask Claude Code: "Is there any route where the tenant comes from the request?" Then check its answer yourself.

The invariant. The control plane is the one place that knows every worker. The tenant comes from who is signed in, never from what is sent, unless a platform-admin role, checked in code, allows it.

Check yourself. An admin tool lets Brightline's staff create workers for any client, so it takes tenantId from the form. Is that a breach of the rule? (Not if the signed-in user is Brightline staff with a platform-admin role, checked in code, and the action is logged. The rule is that the tenant is decided by trusted identity, not that it can never be chosen.)

  • Building Affordable, Self-Improving AI Workers (Panaversity, 29 November 2026), section 9.1
Owner
Panaversity