Is onFinish Fire-and-Forget?

I use the onFinish callback from getText, streamText, etc. to track token usage. However, it seems like the promise given isn’t actually awaited which results in no usage being tracked.

My question is if this is the intended use for onFinish especially in getText contexts or not? I ended up tracking the usage after getText which works fine further confirming my suspicions.

Before:

        const { output } = await generateText({
          model,
          messages: [
            {
              role: "system",
              content: Handlebars.compile(
                ai.healthInsurance.servicesExtraction.systemPrompt
              )({}),
            },
            {
              role: "user",
              content: [
                {
                  type: "file",
                  data: getPublicUrl(serviceOverview.url!),
                  mediaType: "application/pdf",
                },
              ],
            },
          ],
          output: Output.object({ schema: insuranceProviderSchema }),
          onFinish: trackUsage(
            user,
            payload,
            ai.healthInsurance.comparison.packageExtractionModelId
          ),
        });

After:

        const { output, usage, totalUsage, response } = await generateText({
          model,
          messages: [
            {
              role: "system",
              content: Handlebars.compile(
                ai.healthInsurance.servicesExtraction.systemPrompt
              )({}),
            },
            {
              role: "user",
              content: [
                {
                  type: "file",
                  data: getPublicUrl(serviceOverview.url!),
                  mediaType: "application/pdf",
                },
              ],
            },
          ],
          output: Output.object({ schema: insuranceProviderSchema }),
        });

        await trackUsage(
          user,
          payload,
          ai.healthInsurance.comparison.packageExtractionModelId,
          { relationTo: "insurance-providers", value: job.input.provider }
        )({ usage, totalUsage, response });

Hi @dan6erbond!

I’m the Vercel Community Bot, and I’m here to help make sure your question gets answered quickly!

To help our community team assist you better, could you please provide:

Error messages
Config files
Deployment/build logs
Reproduction steps
Environment details

Having this information will help us identify and solve your issue much faster. For more tips on getting great answers, check out How to Get Good Answers. Thanks!