Mehran Toosi · Principal Software Engineer · Copenhagen

blog · · 6 min read

Keeping customer data from the model without blinding the agent

An agent that does real work needs real data. To send a payment reminder it has to know who the customer is and where to write, so names, email addresses and IBANs end up in the prompt. Sending them to a model provider is a GDPR problem.

Privacy Guard is the service I built to sit between an agent harness and the model provider. It replaces personal data with tokens before the model sees anything, keeps the mapping in an encrypted vault per conversation, and puts the real values back at the tool boundary. It runs in production behind ecoai at Visma e-conomic and is open source. This post goes through how it works, one mechanism at a time.

  1. 01 / 09

    Redaction stops the leak, and the work with it

    The obvious fix is redaction: replace every name and email with a fixed marker before the model call. The model never sees the data, and it can no longer act on it either. When a tool result says who to remind and the model asks to send the reminder, the address it writes is the marker, and there is nothing to send.

    What the agent needs is a placeholder the model can carry through its reasoning and copy into a tool call, and that the harness can turn back into the real value afterwards. That is pseudonymization with a way back.

    one tool result through redaction and through reversible tokens · values illustrative, token format from the service
  2. 02 / 09

    A service between the harness and the model

    Privacy Guard is a Cloudflare Worker with two endpoints that matter. /v1/anonymize takes text, detects personal data and returns the text with tokens in its place. /v1/deanonymize takes text with tokens and returns the real values. The rest of the pattern lives in the agent harness: anonymize before every model call, and deanonymize tool arguments before every tool runs.

    Detection runs in Python containers behind the Worker. The mappings live in one Durable Object per vault, encrypted. As shipped, vaults are created under Cloudflare’s EU jurisdiction and the detector containers are pinned to a European region.

    anonymize before the model call, deanonymize before the tool runs · layout illustrative
  3. 03 / 09

    The caller decides what gets scanned

    Only text between __PG_ANON_START__ and __PG_ANON_END__ is processed. Everything outside the markers passes through unchanged, and the markers are removed from the output. A request can be a single text field or a whole chat message array, tool-call arguments included, so the harness can send the conversation as it is and mark only the parts that carry customer data.

    That keeps the system prompt, the tool schemas and the model’s own words away from the detector. They hold no personal data, and a detector that never sees an instruction can’t mistake one for a name.

    one message array with two marked segments · markers from the service, content illustrative
  4. 04 / 09

    Rules for structure, a model for names

    Detection has two parts. Presidio pattern recognizers find structured values: email and IP addresses, IBANs, phone numbers, government IDs, and card numbers that pass a Luhn check. GLiNER, a small multilingual entity model, finds what a regex can’t: person names, addresses and usernames. It reads the text in 1,200-character chunks with 200 characters of overlap, and its spans count from a score of 0.62.

    The two sets of spans overlap, so they are merged by rank. Rule spans have priority 100 and GLiNER spans 40; after that, higher confidence and then longer spans win, and a span that overlaps one already accepted is dropped. If GLiNER marks the name part of an email address as a person, the rule’s email span still wins and the whole address becomes one token.

    one sentence, rule spans and GLiNER spans merged by rank · priorities and threshold from the detector, scores illustrative
  5. 05 / 09

    One value, one token, stored encrypted

    A detected value becomes a token of the form __PG_{vaultTag}_{ENTITY}_{n}__. The tag is the first eight hex characters of the vault ID, and n counts per entity type, so the first email in a conversation is EMAIL_ADDRESS_1 and the second is EMAIL_ADDRESS_2. The model can tell what kind of value it is looking at without seeing the value.

    Before a token is minted, the vault looks up the value’s fingerprint, a SHA-256 of a secret and the value. A known value gets its existing token. A new one is encrypted with AES-GCM under a fresh 12-byte IV and stored with its token, entity and fingerprint. The vault never holds plaintext.

    minting a token, then meeting the same value again · record fields from the vault code
  6. 06 / 09

    The same person keeps the same token

    A conversation spans many model calls, and the model has to see the same token for the same person in every one of them. Before detection runs on new text, the vault replays every mapping it holds into that text as a plain string replacement, longest value first. Detection then only has to find what is new.

    That turns consistency into string matching instead of detector recall. The detector has to catch a value once per vault, and every later mention is replaced even where the model would have missed it. Names that GLiNER found are replayed only at word boundaries, so a stored “Anna” doesn’t turn “Hosanna” into a token.

    two turns against the same vault · values and tokens illustrative
  7. 07 / 09

    Restore before the tool runs, and stop if you can’t

    The model writes tool calls with tokens in their arguments. The harness sends the arguments to /v1/deanonymize before executing, and the vault replaces every token it knows with its value. A token it doesn’t know stays as it is and is reported as Unresolved token preserved. That happens when the vault has expired, when the token came from another vault, or when the model damaged it while copying.

    In production the model damaged a placeholder in 0.61 percent of responses over a three-month sample. That is rare, but it tends to hit exactly the calls that do something, so a harness should treat the warning as fatal for any action with side effects. A cheap first response is to ask the model to regenerate and copy placeholders exactly, and to abort only if that fails too.

    two tool calls, one with a damaged token · warning text from the service
  8. 08 / 09

    A cold container doesn’t fail the conversation

    The detectors run in four container shards. A request goes to the shard picked by an FNV-1a hash of its text, salted with the vault ID. Containers sleep after 20 idle minutes and need a while to load their models when they wake.

    If a shard reports that it is starting, or doesn’t answer, the request moves to the next shard on the ring after a back-off of 250 ms, then 500 ms, 1 s and 2 s, for up to two passes around the ring. A restarting container costs some latency instead of a failed request.

    four detector shards, one of them restarting · timing compressed
  9. 09 / 09

    Vaults expire, and expired means gone

    A vault lives as long as its conversation is in use. Every anonymize or deanonymize call extends a sliding 24-hour window, but never past seven days from creation. A Durable Object alarm deletes the vault’s state when the window closes. A caller that is done can delete it right away, or pass deleteAfter on the last restore.

    There is no grace period. A request for an expired vault gets a 404, and any token from it stays unresolved, which the tool boundary already treats as a reason to stop. A vault can also only be read by the caller that created it.

    two vaults’ lifetimes · window and cap from the vault code, access times illustrative

Privacy Guard doesn’t make data anonymous, and it costs something. Detection is lossy: a value the detectors miss on first sight reaches the model in plaintext, and replay only keeps a value consistent once it is in the vault. For whoever holds the vault, tokenized text is still personal data, and unmasked context, like a job title next to a company name, can still point to a person. Streaming goes too, because a response can only be restored once it is complete. Within those limits it lets an agent act on customer data its model never sees.