Skip to main content
When you have the environment variable 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.
To reduce trace volume by logging only a percentage of all runs, refer to Set a sampling rate for traces.
The 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.

How tracing context works

When you use the tracing_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:
  1. tracing_context(enabled=...): highest priority (context manager for scoped tracing control).
  2. ls.configure(enabled=...): global configuration (sets global tracing behavior).
  3. Environment variables: lowest priority (LANGSMITH_TRACING).

Disable tracing for specific invocations

To disable tracing for a specific operation, wrap it in a tracing_context with enabled=False:
This pattern is useful for one-off cases where you know specific data should not be logged.

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.
This pattern is useful for:
  • 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

The tracing_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 nest tracing_context blocks, the innermost context takes precedence.
Python
This can be useful when you want to temporarily enable tracing for debugging within a normally non-traced section.

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 from Client(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.
You can use any subset of run fields in 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 with tracing_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.