Overview

The session context manager creates a new session, binds it to the current async/thread context, and automatically ends it when the context exits. This is the recommended pattern for self-contained workflows.

Syntax

with lai.session(
  session_name: Optional[str] = None,
  session_id: Optional[str] = None,
  api_key: Optional[str] = None,
  agent_id: Optional[str] = None,
  task: Optional[str] = None,
  providers: Optional[List[ProviderType]] = [],
  production_monitoring: Optional[bool] = False,
  mass_sim_id: Optional[str] = None,
  experiment_id: Optional[str] = None,
  rubrics: Optional[list] = None,
  tags: Optional[list] = None,
  masking_function = None,
  auto_end: Optional[bool] = True,
):
  ...

Parameters

  • session_name (string, optional): Display name for the session.
  • session_id (string, optional): Provide to resume under this ID; otherwise a new one is created.
  • api_key, agent_id (string, optional): Credentials; fall back to LUCIDIC_API_KEY and LUCIDIC_AGENT_ID.
  • task, rubrics, tags (optional): Session metadata.
  • providers (list, optional): Instrumentation targets like "openai", "anthropic", etc.
  • masking_function (callable, optional): Mask sensitive data before sending.
  • auto_end (bool, optional): Ignored inside this context; session always ends on exit.

Returns

None. The active session is bound to context for the duration of the with block.

Examples

import lucidicai as lai
from openai import OpenAI

with lai.session(session_name="Process Order", providers=["openai"]):
    client = OpenAI()
    resp = client.chat.completions.create(
        model="gpt-4o-mini",
        messages=[{"role": "user", "content": "Summarize the order"}],
    )

Notes

  • Spans are stamped with the active lucidic.session_id at start for correct attribution.
  • Prefer this for request-scoped work or jobs that should clean up automatically.

See Also