Discussions

Ask a Question
Back to All

How to access the usage of a stream when using OpenAI sdk?

Hi, I'm currently having a hard time accessing the usage from the stream in JS. It's fine in Python as we can just iterate through the response but can't find a way in JS. I'm also using Vercel AI SDK where I've tried to use AIStream but without success. Here's my code

// Define a simple parser that logs each chunk and returns nothing.
function logChunkParser(): AIStreamParser {
    console.log('logChunkParser')
    return (data: any) => {
        console.log('Chunk received:', data);
        // Since we're only logging, we return nothing here.
    };
}

AIStream(response, logChunkParser())

I don't mind not using Vercel SDK but would still need to count the number of tokens. Does anyone has a way?