Skip to main content
Sometimes, you need to trace a request across multiple services. LangSmith supports distributed tracing out of the box, linking runs within a trace across services using context propagation headers (langsmith-trace and optional baggage for metadata/tags). Example client-server setup:
  • Trace starts on client
  • Continues on server
Only accept distributed-tracing headers from trusted services. The langsmith-trace and baggage headers are consumed as trusted tracing context. Do not add TracingMiddleware (or pass inbound request headers as the tracing parent) on a service that receives requests directly from untrusted third parties or the public internet. Keep distributed tracing to internal, service-to-service calls, and strip these headers from untrusted inbound requests at your gateway or proxy. Trusting baggage from an external caller lets them influence how your runs are recorded.

Distributed tracing in Python

Then the server (or other service) can continue the trace by handling the headers appropriately. If you are using an asgi app Starlette or FastAPI, you can connect the distributed trace using LangSmith’s TracingMiddleware.
The TracingMiddleware class was added in langsmith==0.1.133.
Example using FastAPI:
Or in Starlette:
If you are using other server frameworks, you can always “receive” the distributed trace by passing the headers in through langsmith_extra:
The example above uses the tracing_context context manager. You can also directly specify the parent run context in the langsmith_extra parameter of a method wrapped with @traceable.

Distributed tracing in TypeScript

Distributed tracing in TypeScript requires langsmith version >=0.1.31
First, we obtain the current run tree from the client and convert it to langsmith-trace and baggage header values, which we can pass to the server:
Then, the server converts the headers back to a run tree, which it uses to further continue the tracing. To pass the newly created run tree to a traceable function, we can use the withRunTree helper, which will ensure the run tree is propagated within traceable invocations.