Get the token count and billable characters for a request

The Count Tokens API returns the token count and billable characters of a request.

Code sample

Python

Before trying this sample, follow the Python setup instructions in the Vertex AI quickstart using client libraries. For more information, see the Vertex AI Python API reference documentation.

To authenticate to Vertex AI, set up Application Default Credentials. For more information, see Set up authentication for a local development environment.

import vertexai
from vertexai.generative_models import GenerativeModel

# TODO(developer): Update project & location
vertexai.init(project=PROJECT_ID, location="us-central1")

# using Vertex AI Model as tokenzier
model = GenerativeModel("gemini-1.5-flash-002")

prompt = "hello world"
response = model.count_tokens(prompt)
print(f"Prompt Token Count: {response.total_tokens}")
print(f"Prompt Character Count: {response.total_billable_characters}")
# Example response:
#     Prompt Token Count: 2
#     Prompt Token Count: 10

prompt = ["hello world", "what's the weather today"]
response = model.count_tokens(prompt)
print(f"Prompt Token Count: {response.total_tokens}")
print(f"Prompt Character Count: {response.total_billable_characters}")
# Example response:
#     Prompt Token Count: 8
#     Prompt Token Count: 31

What's next

To search and filter code samples for other Google Cloud products, see the Google Cloud sample browser.