Sessions are the foundation of the rLLM SDK’s trace collection system. They provide automatic tracking of all LLM calls within a context, along with metadata management and nested context support.
Uses Python’s contextvars for in-process context tracking:
from rllm.sdk import session, get_current_session# Create sessionwith session(experiment="v1") as sess: # Get current session from anywhere in call stack current = get_current_session() assert current is sess llm.chat.completions.create(...)
Uses W3C baggage for distributed tracing across processes:
from rllm.sdk.session import otel_session, configure_default_tracer# Configure tracer once per processconfigure_default_tracer(service_name="my-agent")# Sessions use W3C baggage for context propagationwith otel_session(name="client") as client_session: # HTTP calls automatically carry session context via baggage headers httpx.post("http://server/api", ...)# On the server side:with otel_session(name="handler") as server_session: llm.chat.completions.create(...) # server_session automatically inherits client's UID chain
Features:
W3C baggage as single source of truth
Automatic context propagation across HTTP boundaries
with session( experiment="v1", task="math_solver", user_id="alice", model_version="1.0"): llm.chat.completions.create(...) # All traces include the metadata
from rllm.sdk import get_current_metadata, get_current_session_namewith session(experiment="v1", task="greeting"): # Get current metadata from anywhere metadata = get_current_metadata() print(metadata) # {"experiment": "v1", "task": "greeting"} # Get current session name name = get_current_session_name() print(name) # "sess_abc123def456"
from rllm.sdk import ( get_current_session, get_current_session_name, get_current_metadata, get_active_session_uids,)with session(experiment="v1") as sess: # Get current session instance (ContextVar backend only) current = get_current_session() # Get session name (works with all backends) name = get_current_session_name() # Get merged metadata metadata = get_current_metadata() # Get active session UID chain uids = get_active_session_uids() # [outer_uid, ..., inner_uid]
get_current_session() only works with the ContextVar backend. For OpenTelemetry, use get_current_metadata(), get_current_session_name(), or get_active_session_uids() instead.
Implement custom storage for persistent trace collection:
from rllm.sdk import SqliteTracertracer = SqliteTracer(db_path="./traces.db")# Traces are automatically persisted to SQLitewith session(experiment="v1") as sess: llm.chat.completions.create(...) # Traces saved to database