Are you also accounting for costs of sending images and function calls? I didn't see that when I looked through the code. I developed this package so that I could count those sorts of calls as well:
https://github.com/pamelafox/openai-messages-token-helper