LANGSMITH_TRACING=true set globally, traces are automatically sent to LangSmith. This guide shows you how to disable or customize tracing selectively for specific requests.
Use conditional tracing when you need to:
- Comply with data retention policies: Some clients may require zero data retention for compliance or privacy reasons.
- Handle sensitive operations: Disable tracing for operations involving PII, credentials, or confidential data.
- Implement per-tenant configurations: Route traces to different projects or apply different settings based on the customer.
- Control costs: Disable tracing for low-value requests while maintaining visibility into critical operations.
- Support feature flags: Enable tracing only when specific features or experimental code paths are active.
tracing_context context manager (Python) and tracingEnabled option (TypeScript) allow you to override global tracing settings at runtime, without restructuring your code or changing environment variables.
The following sections provide language-specific examples that you can adapt to your application logic and business requirements.
- Python
- TypeScript
How tracing context works
When you use thetracing_context context manager, it overrides the global tracing configuration for code executed within its scope. This means you can keep automatic tracing enabled globally while selectively controlling tracing behavior for specific function calls.There are three priority levels of control:tracing_context(enabled=...): highest priority (context manager for scoped tracing control).ls.configure(enabled=...): global configuration (sets global tracing behavior).- Environment variables: lowest priority (
LANGSMITH_TRACING).
Disable tracing for specific invocations
To disable tracing for a specific operation, wrap it in atracing_context with enabled=False:Enable conditional tracing based on business logic
You can dynamically enable or disable tracing based on runtime conditions, such as client settings or request properties.Customize tracing configuration per request
You can also customize tracing settings dynamically, such as routing traces to different projects or adding request-specific metadata.- Multi-tenant applications: Isolate traces by customer in separate projects
- Regional deployments: Track performance and behavior by geographic region
- Feature branches: Route experimental feature traces to dedicated projects
- User segmentation: Analyze behavior by user tier, cohort, or A/B test group
Work with automatic tracing
Thetracing_context context manager works with automatic tracing. You can keep LANGSMITH_TRACING=true set globally and use tracing_context to override settings for specific requests:Nest tracing contexts
When you nesttracing_context blocks, the innermost context takes precedence.Python
Conditionally redact inputs and outputs
Sometimes you want the trace to be recorded—so you keep run timing, structure, errors, and metadata—but the inputs and outputs should be hidden for specific requests (for example, traces from tenants with strict privacy requirements). This is different from disabling tracing entirely and fromClient(hide_inputs=...), which applies the same redaction to every trace the client sends.To redact per-request, use tracing_context with the replicas parameter and pass an updates dict that overrides inputs and outputs on the recorded run. Because tracing_context is scoped to the current execution context, concurrent requests with different redaction policies do not race.updates (for example, {"inputs": {"redacted": True}} to keep a marker, or {"outputs": {}} to redact only outputs). The same pattern works for routing different redaction policies to different destinations—each replica can specify its own project_name, api_key, and updates. See Write traces to multiple destinations with replicas for the full replica reference.Always set
project_name on the replica when using updates to redact inputs or outputs. If the replica’s project_name matches the active session’s project, the updates may be dropped and the unredacted inputs/outputs will be sent.Customize tracing in deployed agents
Tracing is enabled by default within LangSmith Deployment’s Agent Server. When using a factory function, you can wrap the yielded graph withtracing_context to control tracing per-execution. This is useful for adding custom metadata, disabling tracing entirely, or customizing tracing based on the authenticated user.Disable tracing for a graph
Per-user tracing
Reusable tracing wrapper
Create a decorator to automatically apply conditional tracing logic.Python
Comparison with sampling
Conditional tracing and sampling serve different purposes:
You can combine both approaches for fine-grained control.
Related
- Trace without environment variables: Configure tracing programmatically instead of using environment variables.
- Set a sampling rate for traces: Probabilistically sample traces to reduce volume
- Mask inputs and outputs: Hide sensitive data in traces instead of disabling tracing entirely.
- Add metadata and tags to traces: Categorize and filter traces with custom attributes.
Connect these docs to Claude, VSCode, and more via MCP for real-time answers.

