TruLayer class
The main client. Import and construct once per app.
Methods
| Method | Signature | Returns |
|---|---|---|
trace | (name, fn, opts?) | Promise<T> where T is the fn’s resolved value |
submitFeedback | ({ traceId, label?, score?, comment?, metadata? }) | Promise<void> |
runEval | ({ traceId, evaluatorType, metricName }) | Promise<EvalResult> |
getMetrics | ({ projectId, from, to, filters? }) | Promise<Metrics> |
getTraces | ({ projectId?, from?, to?, cursor?, limit?, filters? }) | Promise<TracesPage> |
getTrace | (id) | Promise<TraceData> |
flush | () | Promise<void> |
shutdown | () | Promise<void> |
trace()
fn becomes the return value of trace(). Exceptions are captured onto the trace as errors, then re-thrown.
TraceContext
Passed to the trace() callback.
| Method | Purpose |
|---|---|
setInput(value) | Trace input |
setOutput(value) | Trace output |
setMetadata(obj) | Merge key-value metadata |
setError(err) | Mark errored |
span(name, type, fn) | Create a child span (see below) |
id (getter) | Trace UUID |
sessionId (getter) | Session ID if set |
span()
SpanContext
| Method | Purpose |
|---|---|
setInput(value) | Span input |
setOutput(value) | Span output |
setMetadata(obj) | Metadata |
setModel(name) | Model name (for llm spans) |
setTokens({ promptTokens, completionTokens }) | Token counts |
setError(err) | Mark errored |
id (getter) | Span UUID |
Instrumentation helpers
instrumentOpenAI(client, tl?)
chat.completions.create and embeddings.create call becomes a span automatically.
instrumentAnthropic(client, tl?)
instrumentVercelAI(tl?)
Module-level helpers
init() / getClient()
If you prefer a global pattern over passing tl explicitly:
Types
All of the following are exported type-only (no runtime cost):Error handling
SDK errors are logged, not thrown to your code — a failed flush drops traces but never interrupts your app. Subscribe to errors with:| Class | Reason |
|---|---|
TruLayerError | Base class |
AuthenticationError | Invalid API key |
RateLimitError | Plan limit hit |
ValidationError | Bad trace/span payload |