I’m working with a workflow that gets used in two different API endpoints. The workflow has multiple nodes and connections between them.
Here’s my setup:
- First endpoint
/service-a should log to LangSmith project_alpha
- Second endpoint
/service-b should log to LangSmith project_beta
The issue is that both endpoints use the same workflow methods and nodes, so I can’t use the @traceable decorator to differentiate them.
Is there a way to tell my workflow which LangSmith project to use when I run it?
Right now I’m setting the environment variable like this:
os.environ["LANGCHAIN_PROJECT"] = my_config.project_name
This works for the first case but I need dynamic switching.
I’ve been looking into RunTrees but I’m stuck on how to pass the configuration properly:
settings = {
"execution_id": req.session_id,
"max_depth": 100,
"configurable": {
"session_id": str(uuid.uuid4()),
"namespace": ""
},
}
my_workflow = workflow_factory.create()
run_tree = RunTree(
run_type="workflow",
name="My Workflow",
inputs=initial_state,
project_name="project_beta"
)
# How do I execute my_workflow with settings and capture the output?
run_tree.end(outputs=???)
run_tree.postRun()
Any ideas on how to make this work?
You’re on the right track with RunTree. Create it with your project name first, then use it as the parent context for your workflow.
Try this:
with run_tree:
result = my_workflow.invoke(initial_state, settings)
run_tree.end(outputs=result)
The with statement makes RunTree the active tracing context, so all nested operations get logged to your project. I’ve used this setup for multi-tenant apps where different customers needed separate tracking - works great.
Had this same headache for months until I automated it.
You need something that switches projects automatically based on your endpoint routes. Passing configs manually becomes a nightmare once you have multiple endpoints.
I set up automation that detects the incoming endpoint and sets the LangSmith project on the fly. It grabs the request, checks the route, and configures tracing before your workflow kicks in.
It also handles sessions and logging without dealing with RunTree context or config mess. Deploy once and you’re done.
No more switching environment variables or passing configs everywhere. Everything happens automatically based on your routing.
You can build this with Latenode - it handles endpoint detection and LangSmith config without the hassle: https://latenode.com
Setting environment variables at runtime won’t work reliably - other concurrent requests will overwrite them. The RunTree approach you started is actually the cleanest solution, but you need to connect it properly to your workflow execution. Skip the context manager and create the RunTree, then pass it through LangChain callbacks instead. Modify your workflow invocation to include the RunTree as a callback handler in your settings dictionary. This way all traced operations within that specific workflow run get logged to the correct project without messing with other concurrent executions.
you can switch project names by invoking your workflow with a config param like this: my_workflow.invoke(initial_state, config={"project_name": "project_alpha" if endpoint == '/service-a' else 'project_beta'}). this way, it picks the right project based on the endpoint.