Skip to content
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

New Feature Request : Incorporation of Token Count Feature for Gemini #373

Open
arunrajes opened this issue Mar 17, 2025 · 5 comments
Open

Comments

@arunrajes
Copy link

Hi Team,

Request you to incorporate the token count feature inside gemini.R as it will help users to get a view of tokens (both input and output) for cost budgeting (informed decision making)

Documentation Reference: https://cloud.google.com/vertex-ai/generative-ai/docs/multimodal/get-token-count

Thanks
Arun

@hadley
Copy link
Member

hadley commented Mar 17, 2025

You mean before you do the request? I can't quite imagine how that would fit into the existing ellmer API.

@arunrajes
Copy link
Author

arunrajes commented Mar 17, 2025

Hi @hadley ,

Assume we have prompt and then some data (may be a pdf or an image file) which are passed as inputs to Gemini. As an end user, we would be interested to know how much input tokens are being consumed . This helps us in optimizing the prompt and the type of input which we can pass (obviously having control over the cost ). The Output token in gemini (max can be 8124 and configurable).

This is very similar to computing token cost
#203 (comment)

Sharing you python sample code from gemini documentation (vertex ai)

from google import genai
from google.genai.types import HttpOptions, Part

client = genai.Client(http_options=HttpOptions(api_version="v1"))

contents = [
Part.from_uri(
file_uri="gs://cloud-samples-data/generative-ai/video/pixel8.mp4",
mime_type="video/mp4",
),
"Provide a description of the video.",
]

response = client.models.count_tokens(
model="gemini-2.0-flash-001",
contents=contents,
)
print(response)

Example output:
total_tokens=16252 cached_content_token_count=None

Thanks
Arun

@hadley
Copy link
Member

hadley commented Mar 18, 2025

I understand how this could be done, but it's very far from the existing ellmer interface and I don't think many other proividers provide a way to determine the number of tokens apart from actually doing the request.

@cpsievert
Copy link
Contributor

Anthropic also supports it https://docs.anthropic.com/en/docs/build-with-claude/token-counting

Last time I looked, OpenAI didn't appear to have API for this, but in Python there is {tiktoken}, which at least gives a rough estimate of the input tokens for a given model.

@arunrajes
Copy link
Author

Hi @cpsievert ,

Running this code shall give the count of input and output tokens
chat_CoT_ellmer$tokens()
input output
[1,] 0 0
[2,] 31771 474

Thanks
Arun

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants