openllmetry-js icon indicating copy to clipboard operation
openllmetry-js copied to clipboard

Workflow timing doesn't include streaming duration (NextJS + App Router + AI SDK)

Open chenlevy opened this issue 4 months ago • 0 comments

I'm trying to create a workflow that contains tasks and stream back to the client using NextJS app router API. The span and total workflow time I get only include the sync workflow tasks. Is it possible to count the stream in total time?

My setup is illustrated below:

traceloop.initialize({
  appName: "builder_app",
  apiKey: process.env.TRACELOOP_API_KEY,
  disableBatch: true,
  traceloopSyncEnabled: false,
});

export const POST = 
  async (
    req: NextRequest & WithAuth,
    { params }: { params: { appId: string } },
  ) => {
    return await traceloop.withWorkflow({ name: "builder_workflow" }, async () => {

      // ...tasks before stream

      const resultStream = streamText({            
        experimental_telemetry: {
          isEnabled: true,
          functionId: "main_code_generation_stream",
        },
        messages,
        model,
        onFinish: async (result) => {

          // ...tasks after stream

        }
      }); 
      
      return Response.stream(resultStream);
  })
})
Image

chenlevy avatar Jul 17 '25 20:07 chenlevy