-
Notifications
You must be signed in to change notification settings - Fork 1.2k
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
[computer use] Counting tokens with new count_tokens() method? #160
Comments
Duh - I realize my mistake - both beta flags must be added:
|
Well, maybe this will be useful for someone else. |
Sorry for reopening this, but I realize that it is not really clear to me how to correctly count tokens for tool useage in the context of computer use: This is a typical entry from the requests that uses the bash tool
This is the result of that tools use:
It is not really clear to me which of these values go where in the client.beta.messages.count_tokens() call - do I put them into the messages part? Do I pass certain parameters or encode them all into the "content" string? Do the tool_use and the tool_result go into two separate calls? A simple code example would be really very nice to have - thank you! |
After a lot of try and error I figured it out - The "secret" is that for tools one has to pass in the assistants "tool_use" and the users(!) "tool_result" together in the message. Also the "tool_use_id" has to be passed as "id":
|
One more observation - removing tools that are not used in that call from the "tool" list gives a different count:
|
In trying to get a better picture of how many tokens each message that Claude sends needs during computer use I am trying to use the new token count endpoint which is right now in beta.
This works fine for regular text messages and responses, but I am struggling with the tool use:
According to the docs for computer use (https://docs.anthropic.com/en/docs/build-with-claude/computer-use) this is how a regular request gets sent:
The docs for the token counting endpoint (https://docs.anthropic.com/en/docs/build-with-claude/token-counting) give this example for tool use:
The thing that I have not figured out yet is how to translate the computer-use tools into the correct format to query the token count, since this approach does not work:
The text was updated successfully, but these errors were encountered: