You don't want to use either of these approaches any more, as they're only valid for older (2.x) models.
Instead, if you need to get token counts for a message before generating a response, you will want to use the count_tokens endpoint (this link is for the Python SDK but is available in the Typescript one and via HTTP requests).
The other, more accurate option, would be to generate your message, and in the resulting Message object, get the exact number of tokens used for the input and output with message.usage.input_tokens and message.usage.output_tokens.