tracer.llmobs SDK lets you manually create LLMObs spans, annotate them with inputs/outputs and metrics, submit evaluation scores, and control the LLMObs lifecycle programmatically.
Prerequisites
Enable LLMObs before using any SDK methods. See LLM Observability Overview for setup instructions.Lifecycle methods
enable(options)
Enables LLMObs programmatically. Has no effect ifDD_LLMOBS_ENABLED=false is set.
| Option | Type | Description |
|---|---|---|
mlApp | string | Name of your ML application |
agentlessEnabled | boolean | Send data directly to Datadog without an Agent |
disable()
Disables LLMObs. Stops writers and unsubscribes channel listeners.flush()
Forces all buffered LLMObs spans and evaluation metrics to be sent immediately. Use this in serverless environments (AWS Lambda, Vercel, etc.) where the process may exit before the next scheduled flush.Creating spans
trace(options, fn)
Instruments a function by creating an LLMObs span that is active for the duration of the function. The span is automatically finished when the function returns, resolves (if it returns a promise), or calls its callback.| Option | Type | Required | Description |
|---|---|---|---|
kind | spanKind | Yes | One of llm, embedding, retrieval, tool, task, agent, workflow |
name | string | Yes | Name of the operation |
modelName | string | No | LLM or embedding model name. Only used on llm and embedding spans. |
modelProvider | string | No | Model provider (e.g. openai). Defaults to custom. |
sessionId | string | No | User session ID for session tracking |
mlApp | string | No | ML app name override for this span |
wrap(options, fn)
Wraps a function so that an LLMObs span is automatically created every time the wrapped function is called. Useful for decorating existing functions.wrap method attempts to automatically annotate the span with the function’s arguments as input and its return value (or callback result) as output for non-llm and non-embedding span kinds.
Annotating spans
annotate(span?, options)
Sets inputs, outputs, metadata, metrics, and tags on an LLMObs span. If no span is provided, annotates the currently active LLMObs span. Callingannotate overwrites any previously set values for each field.
| Field | Description |
|---|---|
inputData | Input for the span. For llm spans: message objects { content, role }. For embedding spans: strings or objects { text, ... }. For all other kinds: any JSON-serialisable value. |
outputData | Output for the span. For llm spans: message objects. For retrieval spans: document objects { name, id, text, score }. For all other kinds: any JSON-serialisable value. |
metadata | Key-value pairs with operation metadata (e.g., temperature, max tokens). |
metrics | Numeric key-value pairs. Commonly { inputTokens, outputTokens, totalTokens }. |
tags | Key-value string pairs for span context. |
prompt | Prompt template metadata. Only used on llm spans. |
annotationContext(options, fn)
Applies annotation context to all LLMObs spans — including auto-instrumented spans — created within the provided function. Useful for propagating tags or prompt information without manually annotating every span.Exporting span context
exportSpan(span?)
Returns thetraceId and spanId of an LLMObs span as plain strings. Use this to associate evaluation metrics with a specific span after the fact.
Evaluation metrics
submitEvaluation(spanContext, options)
Submits a custom evaluation metric for a specific span. The span must be identified by thetraceId and spanId returned from exportSpan().
| Option | Type | Required | Description |
|---|---|---|---|
label | string | Yes | Name of the evaluation metric |
metricType | string | Yes | One of categorical, score, boolean, json |
value | varies | Yes | String for categorical, number for score, boolean for boolean, object for json |
mlApp | string | No | ML app override |
timestampMs | number | No | Timestamp of the evaluation in milliseconds |
tags | object | No | String key-value tags |
reasoning | string | No | Explanation for the evaluation result |
assessment | 'pass' | 'fail' | No | Pass/fail assessment |
metadata | object | No | Arbitrary JSON metadata |
Custom span processors
registerProcessor(processor)
Registers a callback that is invoked for every finished LLMObs span before it is sent. Use this to modify span data, add tags, or drop spans entirely (by returningnull).
deregisterProcessor() before registering a new one.
