Skip to main content
This page documents the public surface of trulayer. For narrative usage, see the tutorial. For configuration details, see configuration. For runnable end-to-end demos, see examples.

Source and issues

Module functions

trulayer.init()

Initialise the global client. Call once at app startup.
Returns the created TruLayerClient. Stored as the process-wide global — retrievable via get_client().

trulayer.get_client()

Return the global client. Raises RuntimeError if init() has not been called.

trulayer.trace()

Context manager that starts a new trace.
Usage:

trulayer.atrace()

Async variant of trace(). Same signature; used with async with.

trulayer.current_trace()

Return the trace in the current async-local context, or None if no trace is active.

trulayer.shutdown()

Flush buffered spans and stop the background worker. Call on process exit.

Instrumentation helpers

trulayer.instrument_openai()

Patch client.chat.completions.create and client.embeddings.create to emit spans automatically. Reversible via uninstrument_openai(client).

trulayer.instrument_anthropic()

Patch client.messages.create to emit spans. Reversible via uninstrument_anthropic(client).

trulayer.instrument_langchain()

Return a LangChain callback handler. Pass it to any ChatModel, Chain, or Retriever via the callbacks=[...] argument.

Classes

TruLayerClient

Explicit client for multi-tenant apps or tests. Prefer init() + get_client() for single-client apps.
Key methods:

TraceContext

Returned by trace() and atrace(). Key methods:

SpanContext

Returned by TraceContext.span(). Key methods:

Models (Pydantic)

TraceData

SpanData

FeedbackData

EventData

Exceptions

The SDK never raises these to your call site — they’re logged and the trace is dropped. Subscribe via trulayer.on_error(fn) if you need to handle them yourself.