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.
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.
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.
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
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.