Sessions in Laminar provide a way to group related traces together. This is particularly useful for:
Grouping traces from a single user interaction
Connecting multiple API requests that form a logical sequence
Organizing conversational turns in a chatbot
Tracking complex workflows across multiple functions or services
For example, in a conversational agent, each turn in the conversation might be represented as a trace, while the entire conversation would be a session.
You can associate a trace with a session using the following methods:
Use the Laminar.setTraceSessionId function inside a span context:
Copy
import { Laminar, observe } from "@lmnr-ai/lmnr";Laminar.initialize({ projectApiKey: process.env.LMNR_PROJECT_API_KEY, instrumentModules: { // your libraries to instrument },});await observe({ name: "myFunction" }, async () => { Laminar.setTraceSessionId("session123"); // Your code here});
Use the Laminar.setTraceSessionId function inside a span context:
Copy
import { Laminar, observe } from "@lmnr-ai/lmnr";Laminar.initialize({ projectApiKey: process.env.LMNR_PROJECT_API_KEY, instrumentModules: { // your libraries to instrument },});await observe({ name: "myFunction" }, async () => { Laminar.setTraceSessionId("session123"); // Your code here});
Call Laminar.set_trace_session_id() within an active span context:
Copy
from lmnr import Laminar, observeimport osLaminar.initialize(project_api_key=os.environ["LMNR_PROJECT_API_KEY"])@observe()def my_function(): # IMPORTANT: set_session must be called within an active span context # In this case, the @observe decorator creates that span context Laminar.set_trace_session_id("session123")
Another example using start_as_current_span:
Copy
from lmnr import Laminardef process_request(): with Laminar.start_as_current_span(name="process_request") as span: # Now we have an active span context Laminar.set_trace_session_id("session123") # Your code here
You can also set the session ID directly when using the observe decorator/wrapper:
Copy
import { Laminar, observe } from "@lmnr-ai/lmnr";// Set session ID in the observe functionawait observe( { name: "myFunction", sessionId: "session123" }, async () => { // Function code here });
Copy
import { Laminar, observe } from "@lmnr-ai/lmnr";// Set session ID in the observe functionawait observe( { name: "myFunction", sessionId: "session123" }, async () => { // Function code here });
Copy
from lmnr import Laminar, observe# Set session ID directly in the observe decorator# Note that this is static, i.e. you can't change this at runtime.@observe(session_id="session123")def my_function(): # Function code here pass
For a chatbot, each user message and response can be a separate trace, with the entire conversation as a session:
Copy
@observe()def handle_turn(user_message, conversation_id): # Must be within span context Laminar.set_trace_session_id(session_id=conversation_id) # Process message, call LLMs, etc. return response