Anonymizers
Anonymizers are available in both cloud and self-hosted installations of Opik.
Anonymizers help you protect sensitive information in your LLM applications by automatically detecting and replacing personally identifiable information (PII) and other sensitive data before it’s logged to Opik. This ensures compliance with privacy regulations and prevents accidental exposure of sensitive information in your trace data.

How it works
Anonymizers work by processing all data that flows through Opik’s tracing system - including inputs, outputs, and metadata - before it’s stored or displayed. They apply a set of rules to detect and replace sensitive information with anonymized placeholders.
The anonymization happens automatically and transparently:
- Data Ingestion: When you log traces and spans to Opik
- Rule Application: Registered anonymizers scan the data using their configured rules
- Replacement: Sensitive information is replaced with anonymized placeholders
- Storage: Only the anonymized data is stored in Opik
Types of Anonymizers
Rules-based Anonymizer
The most common type of anonymizer uses pattern-matching rules to identify and replace sensitive information. Rules can be defined in several formats:
Regex Rules
Use regular expressions to match specific patterns:
Function Rules
Use custom Python functions for more complex anonymization logic:
Mixed Rules
Combine different rule types for comprehensive anonymization:
Custom Anonymizers
For advanced use cases, create custom anonymizers by extending the Anonymizer base class:
Usage Examples
Basic Setup
Here’s a complete example showing how to set up anonymization for a simple LLM application:
Advanced Configuration
For more sophisticated anonymization scenarios:
Integration with Frameworks
Anonymizers work seamlessly with all Opik integrations:
OpenAI Integration
LangChain Integration
Configuration Options
Max Depth
Control how deeply nested data structures are processed:
Multiple Anonymizers
Register multiple anonymizers that will be applied in sequence:
Best Practices
Rule Ordering
Rules are applied in the order they’re defined. More specific patterns should come before general ones:
Performance Considerations
- Use precompiled regex patterns for improved performance on large datasets when implementing custom anonymization functions. (OPIK automatically compiles regex patterns when an anonymizer is registered using a regex pattern.)
- Keep the number of rules reasonable to avoid performance impacts
- Consider using more specific patterns to reduce false positives
Testing Anonymizers
Always test your anonymization rules to ensure they work correctly:
Troubleshooting
Common Issues
Anonymizer not working:
- Ensure the anonymizer is registered with
opik.hooks.add_anonymizer() - Check that your patterns are correct using a regex tester
- Verify that
opik.flush_tracker()is called if needed
Performance issues:
- Reduce the complexity of regex patterns
- Limit the number of registered anonymizers
- Consider using more specific patterns to reduce processing overhead
False positives:
- Make your regex patterns more specific
- Test thoroughly with representative data
- Consider using negative lookbehind/lookahead assertions
Security Considerations
- Test thoroughly: Always test anonymization rules with representative data
- Regular updates: Review and update patterns as your application evolves
- Compliance: Ensure your anonymization approach meets regulatory requirements
- Backup strategy: Consider how to handle cases where anonymization fails
- Access control: Limit access to original data and anonymization rules
Remember that anonymization is a one-way process — once data is anonymized in Opik, the original values cannot be recovered. Plan your anonymization strategy accordingly.