Skip to content
The AI Agent Factory

33.4 One object per conversation, and the worker's record

Approved by
Panaversity ·
10 min read

In everyday life. A clinic gives each patient one folder. Every visit, test and letter goes in that folder, and only that folder. The clinic's list of doctors, with their licences, is kept somewhere else, where everyone can check it.

What you will learn. Why each conversation gets its own Durable Object, how a message is accepted exactly once, and why the worker's identity lives in a database and not in any one conversation.

Why it matters. A worker for three client companies must never mix their invoices. And when someone asks "which version of the worker paid this invoice?", you need one place with the answer.

Step 4. Route each conversation to its own object

Step 4. In the Worker's fetch handler, route every request for a conversation to the ApWorker object named by its conversation ID. The client sends an operation ID with each message; pass it to submit. Add a test that sends the same message twice with the same operation ID and checks that it runs once.

What Claude Code produced.

const w = env.AP_WORKER.getByName(conversationId); // one object per conversation
return Response.json(await w.submit(body.text, body.operationId));

getByName always returns the same object for the same name, wherever in the world the request arrives. So one conversation is one object, with its own storage, its own run and its own queue. Two conversations never share state, which is exactly what you want for two client companies.

Exactly once. Networks repeat messages. A user presses Send twice. A webhook retries after a timeout. Pi Durable accepts each operationId once. When we sent the same operation ID twice, the second answer was accepted: false, and the invoice was handled once.

One at a time. If two different messages reach one conversation together, Pi Durable queues the second and runs it after the first. Test 6 of the spec checks this.

What to check. Where does the conversation ID come from? It must come from your own system, such as a ticket number or a thread ID, and be checked against the caller's company. A conversation ID that a client can choose freely lets one client open another's conversation.

What usually goes wrong. Claude Code makes one object per worker rather than per conversation, because it seems simpler. Everything then runs through one object, one message at a time, and all clients share one transcript. Ask: "How many objects exist after three conversations?" The answer must be three.

Step 5. Record the worker in Postgres

The Durable Object holds the run. It does not hold the answer to "who is this worker?". That answer belongs to the whole company, so it goes in the Neon database you set up in Chapter 32.

Step 5. Add a migration in migrations/ that creates the workers and releases tables described below. At the start of each conversation, read the worker's current release and save its number in the conversation. Every ledger post must carry that release number. Use the Neon connection string from the environment; never put it in code.

The tables Claude Code created:

create table workers (
  id            text primary key,       -- 'brightline-ap'
  display_name  text not null,          -- 'Brightline AP worker'
  client_id     text not null,
  role_contract text not null,          -- version of the written rules, e.g. 'ap-rc-3'
  current_release text not null
);

create table releases (
  id          text primary key,         -- '33.1'
  worker_id   text references workers(id),
  model       text not null,            -- 'claude-haiku'
  prompt_hash text not null,
  tools       text[] not null,
  created_at  timestamptz default now()
);

Why here and not in the object? Three reasons. A person auditing a payment needs to find the release without opening conversations one by one. The platform you build later in this part reads these tables to decide what each worker may do. And when you change the model, as in the lab's port, the change is one new row in releases, not a code change in a hundred objects.

Why save the release in the conversation? A conversation that starts on release 33.1 must finish on 33.1, even if 33.2 is switched on during a three-day approval wait. Otherwise one invoice is read by one prompt and paid by another. Pinning releases for every kind of work, not only conversations, comes later in this part.

Three requests on the left: Acme, ticket T-2041, operation ID op-77; the same Acme ticket again, with op-77 sent twice; and Lahore Foods, ticket L-118, operation ID op-12. All three go to the Worker, which calls getByName with the conversation ID; a note says the ID comes from your own system, checked against the caller's company. The Worker sends them to two objects. Object T-2041 holds its Pi run and transcript, a queue that runs one message at a time, accepts op-77 once and answers the repeat with accepted: false, and has release 33.1 pinned. Object L-118 holds its own Pi run, transcript and storage, never shares state with Acme, and also has release 33.1 pinned. On the right, Postgres on Neon holds the workers table (id, client, role contract, current release) and the releases table (id, model, prompt hash, tools). It is read by auditors and by the platform later in this part. Each conversation reads the current release when it starts and keeps it, so a new release applies to new conversations only. A line at the foot says: three requests, two conversations, two objects; a conversation ID a client may choose freely would let one client open another's conversation.

Figure 33.2. A conversation's state lives with the conversation. The worker's identity lives where the whole company can read it.

What to check. Is the connection string read from the environment, and listed as a secret in Wrangler, not in wrangler.jsonc? Does each ledger post carry the release number? Run one invoice and read the post.

A data question. Postgres now holds the worker's description, and the ledger posts hold invoice numbers. Chapter 32's data rule applies to the database as much as to the model. Choose the Neon region your client's agreement allows.

The invariant. A conversation's state lives with the conversation. The worker's identity lives where the whole company can read it.

Check yourself. A new release changes the prompt while Ana's approval is still waiting. After she approves, which prompt does the worker use to post? (The one from the release saved in the conversation when it started, 33.1. The new release applies to new conversations.)

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