Once the masking worked, I had a new place where unmasked data could pile up: the audit log.
An audit log is supposed to record what was asked and what came back. Do that faithfully for a person who's allowed to see customer details, and the log holds a second copy of those details, kept for longer and read by different people than the original.
This is the last part of a series on Prism, the gateway I built so my teammates' AI agents can read company data safely. Parts 2 and 3 were about what an agent can reach. This one is about knowing who is behind each call, and what to write down about it.
A token the person makes themselves
An agent can't sign in with a browser, so it needs a credential it can hold. I looked at three ways to hand one out:
- An admin issues tokens. Simple, but then removing access when someone leaves depends on the admin remembering.
- Use the company login's own short-lived tokens. Leavers are handled automatically, but those tokens rotate too often to sit in an agent's configuration.
- Sign in with the company account, then create your own long-lived token. This is the one I chose.
A person signs in to a small portal with their company account and creates a token for their agent. The token is random, with a recognisable prefix so secret scanners can spot one in a commit. Prism stores only its hash and shows the value once.
The token carries identity and nothing else. Permissions aren't inside it. Every request looks them up on the server, so taking a permission away from a person takes effect without touching their tokens.
A new person starts with no permissions at all. Each grant is made by an admin in the portal, needs a written reason, and is recorded.
The agent's token can't open the admin door
The portal is for people with a browser session. A token doesn't work there, for any page.
That one rule answers a question I'd otherwise have to keep asking: what if the agent's token leaks, or the agent is talked into misusing it? Whatever happens, the token can't grant itself a permission, can't create another token, and can't read the audit log. It can do what its person was allowed to do through the tools, and every one of those calls is recorded.
There's a matching rule on the human side. An admin can't remove their own admin permission or disable their own account, so a slip can't leave the system with no admin.
Scopes can only narrow
At first, a token could do everything its person could. That's more than most agents need. A person may run more than one agent, and they don't all need the same reach.
So a token now has a scope, chosen when it's created. What the token can actually do is the overlap of two sets: its scope, and the person's permissions right now.
The overlap matters in both directions. A scope can't give a token more than its person has. And when a permission is taken from the person, every token loses it at once, whatever its scope says.
Two defaults do the rest. The permissions for personal data are unticked when you create a token, so reaching customer details through an agent is always a deliberate choice. And admin permissions can't be put into a token at all.
When people leave
I didn't want removing a leaver's access to depend on anyone's memory, including mine. The design is a periodic check against the company directory: an account that's suspended or gone there loses its sessions and tokens here.
A job that revokes access automatically needs a brake, because the directory can be wrong too. An outage or a bad credential could make it look as if everyone had left. So the check refuses to act if the directory's answer is empty, or if it would disable more than a few people who are also a large share of all users. It records that it stopped and waits for a person.
What gets written down
Every call produces a record: who, through which token, which tool, the full query or request, and the outcome. A few decisions shaped it.
Refusals are recorded too. If only successes are logged, you can't later show that something was blocked, or see who keeps running into the same wall.
No record, no data. If the record can't be written, Prism returns an error instead of the result. This wasn't in my plan; I added it during the build. Without it, a failing log store quietly turns into unrecorded access.
The application can't edit its own history. The database account Prism runs as can add records and read them. It can't change or delete one. Schema changes use a different account whose password the running application never sees.
The server names the request. Each call gets an ID generated by Prism. An ID supplied by the client is ignored, so a caller can't choose how its own request will be found later.
Keeping the log from becoming the leak
Back to where this post started.
For results that contain personal data, Prism records the size and a hash, and nothing else. The hash is enough to confirm later that a particular result is what was returned. It isn't enough to read anything. Masked results are safe to sample, so for those the record also keeps the first part.
The request side is harder. A person who's allowed to see customer details can write a query that contains some: a phone number in a WHERE clause, for example. The full query is in the record, so that phone number is too. Query parameters that are known to carry personal data get replaced with a placeholder, and real secrets are scrubbed. But free-form SQL can't be cleaned reliably. That's the parser problem from part 2 again.
So I stopped trying to make the log clean and changed what I assume about it: the audit store is treated as a store of personal data, and it's protected and access-controlled on that basis. Masking most of it doesn't change the category.
Your agent's log is yours to read
In the first design, one person could read the audit log: me. Twelve days later I changed that. Everyone can now see their own records and filter them by token, which in practice means by agent.
The reason is simple. If you hand work to an agent, you need to be able to check what it asked on your behalf. An audit log only an admin can read protects the company. It does nothing for the person whose name is on every line.
Reading everyone's records needs a separate admin permission, and that reading is itself recorded. Reading your own isn't, because it reveals nothing new and would fill your list with entries about looking at your list.
Promises as tests
In part 1 I wrote three promises. For the build I turned them into ten invariants, each one sentence long. Some examples:
- Real keys and passwords appear in no response and no log.
- A call that isn't on the list never reaches the supplier.
- The masked account can't read the real tables.
- A token can't use the portal.
- The application can't change or delete an audit record.
- The SQL that reaches the database is the SQL the agent sent.
Each invariant has tests that try to break it, and the suite has grown to nearly four hundred tests. The rule I set for them: a security test, once written, is never deleted.
I've broken that rule once. When the stand-in keys from part 4 were removed, the tests guarding them had nothing left to guard. I deleted them, reworded the invariant, and wrote down why. I'd rather record an exception than keep a rule that quietly stops being true.
What I'd tell someone building one
- Let people make their own tokens, and keep permissions on the server. Then revoking is one change in one place.
- Keep agent credentials away from the admin surface. Then a leaked token can't promote itself.
- Make a token's reach the overlap of its scope and its person's permissions. Scopes should only ever narrow.
- Give automatic revocation a brake. The source of truth can be wrong too.
- Log refusals, and fail closed when you can't log.
- Decide what the log is before it fills up. If it can contain personal data, treat it that way from the first day.
- Let people read their own agent's trail.
The series
- A read-only account is not a safe account
- I didn't write a SQL parser
- The API key can cancel bookings
- I designed it for curl. Agents needed a tool
- Who asked, and what did they get? (this post)
That's the design. The next step is my teammates using it, and I expect that to change it again. I'll write that up when there's something to measure. If you want the wider context, this series grew out of my field notes on working with AI agents.
If an agent acted for you yesterday, could you see today what it asked for?
