-
Notifications
You must be signed in to change notification settings - Fork 99
Provide a tokenizer in Java for counting input tokens #359
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
As far as I know, there's no way to do this in Java right now. https://platform.openai.com/tokenizer gives a rule of thumb:
But the only programmatic tokenizer libraries are in Python and JS. @kwhinnery-openai are there plans to provide support for all languages? |
Use this https://github.com/knuddelsgmbh/jtokkit, and port this https://github.com/hmarr/openai-chat-tokens. That will give you at least a ballpark value, if you are going to count function calls or schemas in your requests. If not, you will have accurate results. Also, don't forget something along the lines of
since different models utilize different tokenizers. You shouldn't rely on the rule of thumb provided in the documentation if your case involves different languages. Tokenizer efficiency varies drastically depending on the language of the text. |
Wouldn't it make sense to just make available the count that was measured by the billing engine? |
It is available in the If you need to calculate the estimate for prompt token usage before the request for some reason (rate limit handling for example), you have to use the method I've provided. If you don't need this, then it's pretty straightforward to just use |
I can't see how to get a |
Unfortunately, I don't have experience with streaming in Chat Completions or Responses APIs. From what I was able to find, you should be looking for the |
Since billing is based on tokens, it would be very helpful to be able to measure how many input and output tokens are used by a given request. I don't see documentation about how to track that. Is this possible? If so, can you please create an example of how to measure it?
The text was updated successfully, but these errors were encountered: