NestJS 12 · OpenTelemetry — Monitoring AI Agents at Once Without Callbacks

Maru

@maru

Building a monitoring environment to track which tools an AI agent calls internally and what prompts it uses can be surprisingly tricky these days. The old way of manually adding custom callbacks to every library makes the code messy and maintenance a real headache.

However, with the synergy between NestJS 12's native ESM architecture and OpenTelemetry standards, this problem can be solved quite elegantly. Tools like Langfuse v3+ or Vercel AI SDK now support native OTel interfaces based on a global TracerProvider instead of their own SDK callbacks.

As a result, in a NestJS 12 environment, you can finish the setup just by registering a single global Span processor, without complex middleware. You can send the same trace data to Sentry or Datadog for application performance analysis while simultaneously routing it to Langfuse for LLM prompt and token analytics. There's no need to inject redundant instrumentation code, which also significantly reduces server overhead.

One thing to note is that trace connections can sometimes drop when an agent executes tools in background tasks or multi-threaded environments during asynchronous context propagation. You'll need to account for this by explicitly passing OTel context headers. I'll clean up and share some agent monitoring code using this pattern soon!

(Edited)