> ## Documentation Index
> Fetch the complete documentation index at: https://docs.tryhamsa.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Protect personal data on calls

> Use the Protect personal data guardrail to turn phone numbers and emails into codes the model sees, and show the real values in call review with a key.

The **Protect personal data** template keeps phone numbers, emails and long ID numbers away from the model. When the caller says one, it is replaced by a code that starts with `e_` before the model, the conversation history or the logs see it. When the agent says the code back, it is turned into the real value right before it is spoken, so the caller hears the number they gave.

## What gets protected

| Protected | Not protected |
| - | - |
| Email addresses | Names |
| Runs of 7 or more digits: phone numbers, account and ID numbers. Spaces, dashes and a leading + are allowed, and Arabic and Persian digits count. | Addresses |
| | Numbers shorter than 7 digits, such as a 4-digit PIN |

To stop the agent asking for or reading out other sensitive data, add the **Privacy** templates "Never ask for secrets" and "Don't read back full numbers" as well. See [Policies & guardrails](https://docs.tryhamsa.com/agents/safety/policies-and-guardrails.md).

## Before you start

* [Policies & guardrails](https://docs.tryhamsa.com/agents/safety/policies-and-guardrails.md) and [Functions & macros](https://docs.tryhamsa.com/agents/safety/functions-and-macros.md) must be available in your project.
* Owners, admins and members can all add the template to an agent by default.

## Set it up

**1. Add the template**

Open the agent's **Policies & guardrails** settings, click **Add** and pick **Protect personal data** from the templates. You can also start it from **Policies & guardrails** in the sidebar: **Create** > **From a template**.

**2. Check what it created**

The template adds a guardrail and two functions, `maskPersonalData` and `unmaskPersonalData`, with a new random key written into both. The guardrail is set up for you:

* **Check**: **Both**.
* **Caller says**: a pattern for emails and 7 or more digits, then **Function or macro** with the function `maskPersonalData`.
* **Agent says**: the pattern `e_\d+`, then **Function or macro** with the function `unmaskPersonalData`.
* **Severity**: **Critical**. **Mode**: **Enforce**, because only an enforced guardrail rewrites what was said.
* An instruction that tells the model to copy `e_` codes exactly and never mention them.

![The Protect personal data guardrail checking both sides: a pattern on what the caller says and the pattern e\_\d+ on what the agent says](https://mintcdn.com/hamsa-452fa79a/IyoYQ-nNrFRmlJOT/images/agents/safety/protect-personal-data-template-light.webp?fit=max\&auto=format\&n=IyoYQ-nNrFRmlJOT\&q=85\&s=6b220e99742bf538bda0b2b02a1654e4)

**3. Make sure both functions are on the agent**

In the agent's **Functions & macros** settings, both functions must be on the agent and switched on. If one isn't, the guardrail shows that it never runs here.

**4. Test it**

[Test the agent](https://docs.tryhamsa.com/agents/release/test-your-agent.md), say a phone number, and ask the agent to repeat it. Then open the call to check that the transcript shows it as hidden.

> [!WARNING]
>
> Like every rule, this guardrail is not part of a version. Turning it on or off changes live calls right away.

## Show the real values in call review

The conversation history keeps the codes, so in [call details](https://docs.tryhamsa.com/agents/monitor/call-details.md) protected values show as **Hidden**: "Encoded value. Enter the key to show it."

**1. Open Show encoded values**

Click a **Hidden** value, or **Show values** in the bar that says "Personal details on this call are hidden."

**2. Enter the key**

Paste the key from the two functions under **Key** and click **Show values**. "The key stays in this browser tab." A wrong key shows "That key doesn't open these values."

**3. Hide them again**

Click **Hide values** when you are done.

The key is near the top of the code of `maskPersonalData` and `unmaskPersonalData`. Both functions must use the same key, and so does whoever reads the values in call review. To change it, edit it in both functions.

## How it works on a call

* The caller's words are masked first, before the model sees them and before [extracted variables](https://docs.tryhamsa.com/agents/variables/extracted-variables.md) are filled in. An extracted phone number or email holds the `e_` code, not the real value.
* The agent's reply is checked sentence by sentence. Codes are turned back into the real values right before each sentence is spoken.
* A [tool](https://docs.tryhamsa.com/agents/tools/overview.md) that the agent calls with a protected value receives the code, not the value.
* The caller's own number, from the call itself, is not affected: [`user_number`](https://docs.tryhamsa.com/agents/variables/system-variables) holds the real number. Use it when a tool needs the caller's phone number.

## Good to know

* This is encoding, not encryption. In the code's own words: "It hides values from casual reading; it is not encryption against someone who has a token and time."
* Anyone who can open the functions can read the key. By default that is every member of the project.

- If you make your own copies from the built-in `maskPersonalData` and `unmaskPersonalData`, they start with the key `change-this-secret-key`. Change it in both.
- The model only sees codes, so it can't check a number's format or read part of it back.

## Related

> [card] [Policies & guardrails](https://docs.tryhamsa.com/agents/safety/policies-and-guardrails.md)
>
> How guardrails check and rewrite what is said.

> [card] [Functions & macros](https://docs.tryhamsa.com/agents/safety/functions-and-macros.md)
>
> The functions that mask and unmask values.

> [card] [Call details](https://docs.tryhamsa.com/agents/monitor/call-details.md)
>
> Review a call and show its hidden values.

> [card] [System variables](https://docs.tryhamsa.com/agents/variables/system-variables.md)
>
> Values the call provides, like the caller's number.


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.