This page covers redacting secrets (API keys, tokens, credentials) from trace data via the SDK. For redacting personally identifiable information (PII) such as emails, names, or SSNs, see Prevent logging of sensitive data in traces. To redact secrets at the LLM Gateway layer, see PII and secrets redaction.
Use the SDK anonymizer
The
create_anonymizer / createAnonymizer function requires:- Python SDK: 0.1.81 and above
- TypeScript SDK: 0.1.33 and above
create_anonymizer function accepts a list of regex patterns and replacement strings. Pass the resulting anonymizer to the Client constructor, and it will automatically apply to all run inputs and outputs before they reach LangSmith.
The following example redacts common secret formats, including OpenAI API keys, generic bearer tokens, and sk- prefixed keys:
max_depth parameter:
Use a custom function
If your redaction logic is more complex, pass a function instead of a list of patterns. The function receives a string and returns the redacted string:Combine with LANGSMITH_HIDE_INPUTS
If your use case requires completely suppressing all inputs (for example, for zero-retention compliance), useLANGSMITH_HIDE_INPUTS=true instead. The anonymizer is skipped when LANGSMITH_HIDE_INPUTS or LANGSMITH_HIDE_OUTPUTS is set to true.
For more options, including hiding all inputs and outputs, hiding metadata, function-level processors, and third-party PII libraries, see Prevent logging of sensitive data in traces.
Connect these docs to Claude, VSCode, and more via MCP for real-time answers.

