> For the complete documentation index, see [llms.txt](https://docs.raga.ai/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.raga.ai/ragaai-catalyst/concepts/uploading-data/trace-masking-functions.md).

# Trace Masking Functions

While RagaAI Catalyst sits on-prem for complete data security, enterprises might want to redact certain information, keywords, or patterns from their traces logged on the system. This can be done using custom Python functions as follows:

* **Prerequisites**

  Users need to define the relevant platform keys and Tracer object as usual:

```python
from ragaai_catalyst.tracers import Tracer
from ragaai_catalyst import (
    RagaAICatalyst,
    init_tracing
)

# Initialize RagaAI Catalyst
catalyst = RagaAICatalyst(
    access_key="<your-access-key>",
    secret_key="<your-secret-key>",
    base_url="https://catalyst.raga.ai/api"
)

# Setup tracing
tracer = Tracer(
    project_name="<your-project-name>",
    dataset_name="<your-dataset-name>",
    tracer_type="langchain"
)
```

* **Defining Custom Masking Functions**

  Users can define their custom Python logic to replace certain keywords with a redaction phrase as shown below:

```python
def masking_function(value):
    # Mask specific medical symptoms
    symptoms = ['Fever', 'Cough', 'Headache']
    for symptom in symptoms:
        # Case insensitive replacement using regex
        value = re.sub(rf'\b{symptom}\b', '<REDACTED SYMPTOM>', value, flags=re.IGNORECASE)

    return value

# Another sample masking function
def another_masking_function(value):
    """
    Returns masked strings with dates and emails redacted
    """
    # Mask dates in various formats (YYYY-MM-DD, MM/DD/YYYY, etc.)
    value = re.sub(r'\b\d{4}-\d{2}-\d{2}\b', '<REDACTED DATE>', value)
    value = re.sub(r'\b\d{1,2}/\d{1,2}/\d{4}\b', '<REDACTED DATE>', value)
    value = re.sub(r'\b[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}\b', '< REDACTED EMAIL ADDRESS>', value)
    return value
```

* **Enabling Masking Function**

  Lastly, users can pass the desired masking function (limited to one) to the initialized Tracer object using the `register_masking_function` method as follows:

```python
tracer.register_masking_function(masking_function)
init_tracing(catalyst=catalyst, tracer=tracer)
```

* **Running Your Application**

  Your RAG application can be defined further as usual. Any LLM calls should be traced with the above masking logic applied.


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://docs.raga.ai/ragaai-catalyst/concepts/uploading-data/trace-masking-functions.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
