(edited)
I've been working on capturing every streamText / generateText call in a Next.js app on Vercel (prompt timing, tokens, failures) together with the request that triggered it, and I'm stuck on a design choice I'd like other opinions on.
Two ways I see:
- Patch
next.configat install time so every AI SDK call is recorded with zero code changes. Nothing gets missed, but it's "magic": you don't see in your code that it's happening. - Opt in per call (the AI SDK's own telemetry option), so it's explicit in the code, but any call someone forgets to flag goes unrecorded.
A few things I learned on the request side, in case they help anyone doing similar work:
instrumentation.tsis the clean entry point for server-side recording. If the app lives insrc/, the file has to go there too.- Naming requests by Next's own route (
GET /api/orders/[id]) instead of the raw URL keeps the data groupable. - Never overwrite an existing
instrumentation.ts; many apps already have one for something else. - On Next.js 13/14 you may need
experimental.instrumentationHook: trueinnext.config. - Stack traces from the compiled
.nextoutput only map back to your source if the source maps sit next to the build.
Questions:
- Would you accept a tool patching
next.configfor this, or would that be a dealbreaker? - Anyone combining AI SDK telemetry with request traces in production? How do you link an AI call to the user request that caused it?
Disclosure: I'm building Relia, a monitoring tool, and this is for its Next.js setup. I'm asking for opinions on the approach, not trying to sell anything here.