How to get the “OUTPUT” tokens count from an streamed response of Gemini from OpenAI SDK.
Hi @Suparva,
You can check the response object to get the number of tokens used. The output looks like this
ChatCompletion(id=None, choices=[Choice(finish_reason='stop', index=0, logprobs=None, message=ChatCompletionMessage(content='I am doing well, thank you for asking! How are you today?\n', refusal=None, role='assistant', annotations=None, audio=None, function_call=None, tool_calls=None))], created=1742584713, model='gemini-2.0-pro-exp-02-05', object='chat.completion', service_tier=None, system_fingerprint=None, usage=CompletionUsage(completion_tokens=15, prompt_tokens=14, total_tokens=29, completion_tokens_details=None, prompt_tokens_details=None))
But in case before supplying to the Gemini if you want to know the number of tokens you can use count_tokens api and count tokens before sending into the model.