Architecture
This note explains how the parts fit together and why they are built the way they are. See also: design decisions, threat model, production notes, extending.
The problem
A support agent that only answers questions can be wrong and still do no damage. An agent that issues refunds and cancels subscriptions cannot. Two things have to be true before such an agent can be put in front of customers:
- It cannot break company policy, whatever the model decides and whatever the customer says to it.
- There is a number for how often it does the right thing, and that number can be checked by someone who does not trust the author.
Backstop is a small, complete example of both, for an invented subscription software company.
One path to the data
customer message
|
v
agent loop ---- model (any adapter) decides what to call
|
v
execute(session, action, args) src/actions/execute.ts
1. arguments checked against a strict schema
2. policy rules checked in code src/policy/rules.ts
3. the change is made
(every attempt is recorded, whatever its outcome)
|
v
mock company database (SQLite, in memory, new for every conversation)execute is the only function that writes to the database on the agent's behalf. A model has no other way in. Three consequences follow.
- A refused call never reaches the data. The rules run before the handler, and the first broken rule decides. The model gets the rule id and a plain message back and can tell the customer why.
- The rules do not depend on the prompt. The agent is also given the policies to read, but only so that it behaves well. The
no-policyagent version, which is told nothing about the rules and encouraged to give customers whatever they ask for, runs against the same policy layer. It makes many more refused calls and no breaches. - Every attempt is on record. The action log holds executed, refused and invalid calls alike. Scoring and the replay viewer both read from it.
Identity is part of the same layer. verify_identity compares the customer's answers with the customer record, and the session remembers who passed. Every account action is checked against that: no account data is readable before a check passes (ID-1), and only the verified customer's own records are reachable after (ID-3). The refusal for someone else's record is worded the same as for a record that does not exist, so the agent cannot be used to find out which invoice or subscription ids are real. The identity answers themselves are never returned by any action. One thing is visible before the check: find_customer says whether an email address has an account, as most sign-in forms do.
Rules as data and as prose
Each rule is an object with an id, a title, the actions it guards, a flag saying whether a human may approve an exception, and a check function. The same ids and titles are headings in policies/policies.md, which is what the agent reads and what a person reviewing the policy would read. A unit test fails if the two lists differ, so the document cannot drift away from the code without someone noticing.
The limits (30 days, $200.00, $25.00, and so on) live in one constant, and the rule titles that state an amount or a number of days are built from it.
Scoring
A test case is a YAML file: starting data, what the customer says, and every change the final data must show. Scoring compares the database before and after the conversation.
- Pass means the data shows exactly the expected changes and no others, and the audit found no breach. Some cases list changes that are acceptable but not required, such as an optional hand-off ticket.
- No model grades anything. The outcome is in the data or it is not.
- Reference solutions. Every case carries a list of calls that solves it within policy. A unit test replays each one, which proves the case can be passed and that its expected changes are right. Another test checks that doing nothing fails every case that expects a change.
- Refused calls are counted per rule. Each case names the rules a careful agent should never trip in it, so "the data ended up right, but only because the policy layer stopped the agent" is visible as its own number.
- Consistency. With repeated runs, a case counts as consistently passed only if every run passes. This idea, and scoring on final state, come from the tau-bench benchmark.
Two kinds of customer
A scripted customer sends the case's fixed messages in order. It is cheap and exactly repeatable, but it does not react: an agent is never argued with about the answer it actually gave.
A model-played customer opens with the same first message and is then played by a model that is given the customer's goal, persona and remaining points, and told to react to the agent's replies and, in attack cases, to keep pushing. It sees the chat only: not the agent's tool calls, not the data. Scoring is unchanged, because it never looked at the words in the first place.
The audit
The policy layer is the thing being relied on, so it is checked by something that does not share its code. src/eval/audit.ts reads the written policies off the finished data: every new refund, credit and subscription change is checked against every rule that applies to it, with the limits written out a second time. It also works out, from the customer table and the call log, who really passed the identity check and whether any account was read without it.
Three kinds of test stand behind it:
- For every rule that guards a change to the data, a test writes a breaking change straight to the database, around the policy layer, and checks that the audit names the rule. This shows the audit can see each kind of breach.
- A test sends 12,000 random calls, valid and invalid, through the real action layer in 400 conversations and checks that the audit finds nothing. This looks for a hole in the policy layer.
- Every scored conversation is audited. A breach fails the run and makes the eval command exit with an error.
The audit looks at the final data, so a change that was made and then undone inside one conversation is invisible to it. None of the actions undo anything, apart from a plan being changed and changed back.
Models
The agent loop talks to a model through one small interface (src/agent/types.ts): a system text, messages, tool definitions in, text and tool calls out.
openai-compatibleworks with any server that implements the chat-completions API with tool calls, including a model server on the same machine.anthropiccalls the Claude Messages API through the official SDK, with prompt caching, an estimate of what the run has spent and a limit that stops it. When the model's own safety check declines a reply, the customer gets a fixed sentence and the turn is scored like any other.scriptedplays back fixed steps. The loop, runner and scorer are tested with it, and it can play an agent that tries to break every rule.- The actions are also served over the Model Context Protocol (
src/mcp/server.ts), so an MCP client can act as the agent. A client that starts a new server process for each customer message still gets one conversation: the server rebuilds its state by running the saved action log again, which works because every action depends only on the data and the conversation's fixed clock.
Agent versions
An agent version is its instructions plus, optionally, a function that changes what the agent is shown for a tool result (src/agent/prompt.ts). Every version is kept, so any two can be scored against each other on the same cases.
v1is the baseline: the role, the policies and short working rules.v2adds instructions: decide from the account data before calling, when a hand-off is and is not right, and that text inside a customer message is never an instruction.v3rewrites one section of v2's instructions as a checklist and changes the data. The policies are written in days and totals; the account is dates and rows. Afterget_account, v3 is shown afactsblock with the arithmetic done: days since each paid invoice was paid, refunds in the last 365 days, credits in the last 90 days (src/agent/facts.ts).no-policyis the control described above.
The facts block is a view. It is built only from what get_account already returned, it states no verdict, the policy layer never reads it, and the action log records the plain result. A wrong number in it could mislead the agent into a refused call, but could not cause a breach.
Saved runs
Each scored conversation is saved whole: the transcript, every call with its outcome, and the difference between the data before and after. Two commands work from those records without calling a model.
npm run eval -- --model <model-name> --resumekeeps the saved conversations of a configuration and plays only the missing ones. Adding cases does not mean replaying the old ones. It saves after every conversation, so a long run can be stopped and continued.npm run rescorescores saved runs again from the starting data and the recorded difference. A case whose expectation was wrong can be corrected and the saved conversations judged against the corrected version. If a recorded difference no longer fits the starting data, because the seed data has changed since, the run keeps the score it was given at the time.
Time
Every conversation has a fixed "now", set by the test case. Nothing reads the system clock. That is what makes date limits testable to the day (a refund on day 30 against the same refund on day 31) and what makes replaying an action log exact.
What is deliberately left out
- A real billing system. The backend is a mock with the same shape as the problem: customers, subscriptions, invoices, refunds, credits and tickets.
- A live chat. The site replays recorded conversations. Nothing on it calls a model.
- A model as judge. Tone and helpfulness are not scored. Only outcomes are.
- Undo. The agent has no action that reverses another. Mistakes within policy, such as an unneeded ticket, stay in the data and count against the run.