Implement distributed tracing
note
This feature is currently only available in the Python SDK. Support for TypeScript is coming soon.
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
# client.py
from langsmith.run_helpers import get_current_run_tree, traceable
import httpx
@traceable
async def my_client_function():
headers = {}
async with httpx.AsyncClient(base_url="...") as client:
if run_tree := get_current_run_tree():
# add langsmith-id to headers
headers.update(run_tree.to_headers())
return await client.post("/my-route", headers=headers)
Then the server (or other service) can continue the trace by passing the headers in as langsmith_extra
:
# server.py
from langsmith import traceable
from langsmith.run_helpers import tracing_context
from fastapi import FastAPI, Request
@traceable
async def my_application():
...
app = FastAPI() # Or Flask, Django, or any other framework
@app.post("/my-route")
async def fake_route(request: Request):
# request.headers: {"langsmith-trace": "..."}
# as well as optional metadata/tags in `baggage`
with tracing_context(parent=request.headers):
return await my_application()
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
.
from langsmith.run_helpers import traceable, trace
# ... same as above
@app.post("/my-route")
async def fake_route(request: Request):
# request.headers: {"langsmith-trace": "..."}
my_application(langsmith_extra={"parent": request.headers})