This guide shows you how to list and count tokens for a prompt using the Google Gen AI SDK, covering the following topics: Generative AI models process prompts by breaking down text and other data into units called tokens. A token can be a word, part of a word, or punctuation. The algorithm that converts text into tokens is called a tokenizer. Listing and counting tokens is useful for the following reasons: The following table shows you the models that support token listing and token
counting: The Google Gen AI SDK provides two utilities for working with tokens: listing and counting. The following table helps you decide which one to use. To get a list of tokens and their corresponding IDs for a prompt, use the following code sample. This operation supports only text prompts; multimodal prompts are not supported.
To learn more, see the
SDK reference documentation.
Set environment variables to use the Gen AI SDK with Vertex AI:
Learn how to install or update the Go.
To learn more, see the
SDK reference documentation.
Set environment variables to use the Gen AI SDK with Vertex AI:
What are tokens and why is counting them important?
Supported models
Choose a token utility
Utility
Description
Use Case
Supported Prompts
List tokens
Returns a list of tokens and their associated token IDs.
Troubleshooting and analyzing model behavior.
Text-only
Count tokens
Returns the total token count and the number of billable characters.
Checking model input limits and estimating costs.
Text and multimodal
Get a list of tokens and token IDs for a prompt
Python
Install
pip install --upgrade google-genai
# Replace the `GOOGLE_CLOUD_PROJECT` and `GOOGLE_CLOUD_LOCATION` values
# with appropriate values for your project.
export GOOGLE_CLOUD_PROJECT=GOOGLE_CLOUD_PROJECT
export GOOGLE_CLOUD_LOCATION=global
export GOOGLE_GENAI_USE_VERTEXAI=True
Go
# Replace the `GOOGLE_CLOUD_PROJECT` and `GOOGLE_CLOUD_LOCATION` values
# with appropriate values for your project.
export GOOGLE_CLOUD_PROJECT=GOOGLE_CLOUD_PROJECT
export GOOGLE_CLOUD_LOCATION=global
export GOOGLE_GENAI_USE_VERTEXAI=True
Get the token count and billable characters of a prompt
To get the token count and the number of billable characters for a prompt, use the following code sample. This operation supports both text-only and multimodal prompts.
Python
Install
pip install --upgrade google-genai
To learn more, see the SDK reference documentation.
Set environment variables to use the Gen AI SDK with Vertex AI:
# Replace the `GOOGLE_CLOUD_PROJECT` and `GOOGLE_CLOUD_LOCATION` values # with appropriate values for your project. export GOOGLE_CLOUD_PROJECT=GOOGLE_CLOUD_PROJECT export GOOGLE_CLOUD_LOCATION=global export GOOGLE_GENAI_USE_VERTEXAI=True
Go
Learn how to install or update the Go.
To learn more, see the SDK reference documentation.
Set environment variables to use the Gen AI SDK with Vertex AI:
# Replace the `GOOGLE_CLOUD_PROJECT` and `GOOGLE_CLOUD_LOCATION` values # with appropriate values for your project. export GOOGLE_CLOUD_PROJECT=GOOGLE_CLOUD_PROJECT export GOOGLE_CLOUD_LOCATION=global export GOOGLE_GENAI_USE_VERTEXAI=True