Context
When using LangSmith for tracing LangChain executions, you may want to temporarily disable tracing or control when traces are collected during your application's runtime. This could be useful when you only want to trace specific parts of your code or need to manage the volume of traces being collected.
Answer
The recommended way to toggle LangSmith tracing during execution is to use the tracing context manager. Here's how to do it:
Import the langsmith package:
import langsmith as lsUse the tracing_context manager to disable tracing for specific code blocks:
with ls.tracing_context(enabled=False): my_thing.invoke()
Important notes:
The tracing context should be set before executing the trace you want to disable
While environment variables like LANGSMITH_TRACING can be used to configure tracing, they are loaded at startup and won't affect runtime behavior
Setting the tracing_sample_rate directly on a client instance won't affect the global tracing behavior in LangChain
Source: LangSmith documentation and official guidance from the LangSmith team.