trulayer. For narrative usage, see the tutorial. For configuration details, see configuration.
Module functions
trulayer.init()
Initialise the global client. Call once at app startup.
TruLayerClient. Stored as the process-wide global — retrievable via get_client().
trulayer.get_client()
RuntimeError if init() has not been called.
trulayer.trace()
Context manager that starts a new trace.
trulayer.atrace()
Async variant of trace(). Same signature; used with async with.
trulayer.current_trace()
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()
client.chat.completions.create and client.embeddings.create to emit spans automatically. Reversible via uninstrument_openai(client).
trulayer.instrument_anthropic()
client.messages.create to emit spans. Reversible via uninstrument_anthropic(client).
trulayer.instrument_langchain()
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.
| Method | Signature |
|---|---|
trace(name, session_id=None, metadata=None) | → TraceContext |
atrace(name, ...) | → async TraceContext |
submit_feedback(trace_id, label=None, score=None, comment=None, metadata=None) | → None |
run_eval(trace_id, evaluator_type, metric_name) | → EvalResult |
get_metrics(project_id, from_time, to_time, ...) | → Metrics |
flush() | → None (blocks until shipped) |
shutdown() | → None |
TraceContext
Returned by trace() and atrace(). Key methods:
| Method | Purpose |
|---|---|
set_input(value) | Set the trace’s input payload |
set_output(value) | Set the trace’s output payload |
set_metadata(**kwargs) | Attach key-value metadata |
set_error(exc) | Mark the trace as errored with an exception |
span(name, span_type="custom", ...) | → SpanContext |
.id | Trace UUID (read-only) |
.session_id | Session identifier (read-only) |
SpanContext
Returned by TraceContext.span(). Key methods:
| Method | Purpose |
|---|---|
set_input(value) | Span input |
set_output(value) | Span output |
set_metadata(**kwargs) | Key-value metadata |
set_model(model) | Model name (for llm spans) |
set_tokens(prompt_tokens, completion_tokens) | Token counts |
set_error(exc) | Mark errored |
.id | Span UUID (read-only) |
Models (Pydantic)
TraceData
SpanData
FeedbackData
EventData
Exceptions
| Exception | Raised when |
|---|---|
TruLayerError | Base class for all SDK errors |
AuthenticationError | Invalid or revoked API key |
RateLimitError | Plan limit hit; trace dropped |
ValidationError | Trace/span payload failed validation |
trulayer.on_error(fn) if you need to handle them yourself.