Get cost of streamText using the ai gateway sdk

I’m trying to calculate the cost of doing a streamText.

Based on the docs here, I was under the impression I could use gateway.getGenerationInfo({ id: generationId }) in the onFinish hook. But here I just get:

"data": {
        "error": "Usage event not found",
        "id": "gen_01KT4BW3JSXKZG1CHRJRYQASH8",
        "message": "No usage event found with ID gen_01KT4BW3JSXKZG1CHRJRYQASH8."
      }

I can see that result.providerMetadata.gateway contains cost information when I log it:

{
  cost: '0.00849',
  marketCost: '0.00849',
  surchargeCost: '0',
  gatewayCost: '0.00849',
  inferenceCost: '0.00849',
  inputInferenceCost: '0.00744',
  outputInferenceCost: '0.00105',
  generationId: 'gen_01KT4C7A322AA7XN4JV02506VR'
}

However this is untyped in the typescript SDK, with the type JSONObject ?

So what’s the verdict here - do I parse/validate the result.providerMetadata.gateway, or should gateway.getGenerationInfo actually work?

I’m wondering the same. I’m interpreting GET /v1/generation?id={generation_id} docs that say “Generation information becomes available shortly after the generation completes” to mean not available immediately. So that would mean using providerMetadata is the way to go. Would appreciate confirmation, thanks!