from google import genaiclient = genai.Client(api_key='your-api-key')response = client.models.count_tokens( model='gemini-2.0-flash', contents='What is your name?',)print(f"Total tokens: {response.total_tokens}")# Output: Total tokens: 5
from google.genai import typesconversation = [ types.Content(role='user', parts=[types.Part.from_text('Hello!')]), types.Content(role='model', parts=[types.Part.from_text('Hi there! How can I help you?')]), types.Content(role='user', parts=[types.Part.from_text('Tell me about Python')]),]response = client.models.count_tokens( model='gemini-2.0-flash', contents=conversation,)print(f"Conversation tokens: {response.total_tokens}")
response = client.models.count_tokens( model='gemini-2.0-flash', contents=[ types.Part.from_text('Describe this image'), types.Part.from_uri( file_uri='gs://generativeai-downloads/images/scones.jpg', mime_type='image/jpeg' ), ],)print(f"Multimodal tokens: {response.total_tokens}")# Includes both text and image tokens
client = genai.Client(vertexai=True, project='my-project', location='us-central1')response = client.models.count_tokens( model='gemini-2.0-flash', contents='What is the weather?', config=types.CountTokensConfig( system_instruction='You are a helpful weather assistant.', tools=[weather_tool], ),)print(f"Total tokens (including system instruction and tools): {response.total_tokens}")
Given a list of contents, returns a corresponding TokensInfo containing the list of tokens and list of token IDs.This method is only supported by Vertex AI API (not Gemini Developer API).Useful for:
from google import genaiclient = genai.Client(vertexai=True, project='my-project', location='us-central1')response = client.models.compute_tokens( model='gemini-2.0-flash', contents='What is your name?',)print(f"Tokens: {response.tokens_info[0].tokens}")print(f"Token IDs: {response.tokens_info[0].token_ids}")# Output:# Tokens: [b'What', b' is', b' your', b' name', b'?']# Token IDs: ['1841', '374', '574', '836', '30']
text = "The quick brown fox jumps over the lazy dog."response = client.models.compute_tokens( model='gemini-2.0-flash', contents=text,)tokens_info = response.tokens_info[0]print(f"Original text: {text}")print(f"Number of tokens: {len(tokens_info.tokens)}")print("\nTokenization:")for token, token_id in zip(tokens_info.tokens, tokens_info.token_ids): print(f" '{token.decode('utf-8')}' -> ID: {token_id}")