Starting April 29, 2025, Gemini 1.5 Pro and Gemini 1.5 Flash models are not available in projects that have no prior usage of these models, including new projects. For details, see Model versions and lifecycle.
What are tokens and why is counting them important?
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:
Manage context window: Each model has a maximum number of tokens it can accept in a prompt and generate in a response. Counting tokens helps you stay within this limit.
Estimate costs: For text prompts, counting tokens also returns the number of billable characters, which helps you understand and predict costs.
Troubleshoot and analyze: Listing tokens shows you exactly how your prompt is tokenized and provides token IDs. This is useful for troubleshooting unexpected model behavior and for deeper analysis.
Supported models
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.
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
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.
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.
[[["Easy to understand","easyToUnderstand","thumb-up"],["Solved my problem","solvedMyProblem","thumb-up"],["Other","otherUp","thumb-up"]],[["Hard to understand","hardToUnderstand","thumb-down"],["Incorrect information or sample code","incorrectInformationOrSampleCode","thumb-down"],["Missing the information/samples I need","missingTheInformationSamplesINeed","thumb-down"],["Other","otherDown","thumb-down"]],["Last updated 2025-08-29 UTC."],[],[],null,["# List and count tokens\n\n| **Preview**\n|\n|\n| This product or feature is subject to the \"Pre-GA Offerings Terms\" in the General Service Terms section\n| of the [Service Specific Terms](/terms/service-terms#1).\n|\n| Pre-GA products and features are available \"as is\" and might have limited support.\n|\n| For more information, see the\n| [launch stage descriptions](/products#product-launch-stages).\n\nThis page shows you how to list the tokens and their token IDs of a prompt\nand how to get a total token count of a prompt by using the Google Gen AI SDK.\n\nTokens and the importance of token listing and counting\n-------------------------------------------------------\n\nGenerative AI models break down text and other data in a prompt into units called\ntokens for processing. The way that data is converted into tokens depends on the\ntokenizer used. A token can be characters, words, or phrases.\n\nEach model has a maximum number of tokens that it can handle in a prompt and\nresponse. Knowing the token count of your prompt lets you know whether you've\nexceeded this limit or not. Additionally, counting tokens also returns the billable\ncharacters for the prompt, which helps you estimate cost.\n\nListing tokens returns a list of the tokens that your prompt is broken down into.\nEach listed token is associated with a token ID, which helps you perform\ntroubleshooting and analyze model behavior.\n\nSupported models\n----------------\n\nThe following table shows you the models that support token listing and token\ncounting:\n\n- [Gemini 2.5 Flash Image Preview](/vertex-ai/generative-ai/docs/models/gemini/2-5-flash#image) (Preview)\n- [Gemini 2.5 Flash-Lite](/vertex-ai/generative-ai/docs/models/gemini/2-5-flash-lite)\n- [Gemini 2.0 Flash with image generation](/vertex-ai/generative-ai/docs/models/gemini/2-0-flash) (Preview)\n- [Vertex AI Model Optimizer](/vertex-ai/generative-ai/docs/model-reference/vertex-ai-model-optimizer) (Experimental)\n- [Gemini 2.5 Pro](/vertex-ai/generative-ai/docs/models/gemini/2-5-pro)\n- [Gemini 2.5 Flash](/vertex-ai/generative-ai/docs/models/gemini/2-5-flash)\n- [Gemini 2.0 Flash](/vertex-ai/generative-ai/docs/models/gemini/2-0-flash)\n- [Gemini 2.0 Flash-Lite](/vertex-ai/generative-ai/docs/models/gemini/2-0-flash-lite)\n\nGet a list of tokens and token IDs for a prompt\n-----------------------------------------------\n\nThe following code sample shows you how to get a list of tokens and token IDs for\na prompt. The prompt must contain only text. Multimodal prompts are not supported. \n\n### Python\n\n#### Install\n\n```\npip install --upgrade google-genai\n```\n\n\nTo learn more, see the\n[SDK reference documentation](https://googleapis.github.io/python-genai/).\n\n\nSet environment variables to use the Gen AI SDK with Vertex AI:\n\n```bash\n# Replace the `GOOGLE_CLOUD_PROJECT` and `GOOGLE_CLOUD_LOCATION` values\n# with appropriate values for your project.\nexport GOOGLE_CLOUD_PROJECT=GOOGLE_CLOUD_PROJECT\nexport GOOGLE_CLOUD_LOCATION=global\nexport GOOGLE_GENAI_USE_VERTEXAI=True\n```\n\n\u003cbr /\u003e\n\n from google import genai\n from google.genai.types import HttpOptions\n\n client = genai.Client(http_options=HttpOptions(api_version=\"v1\"))\n response = client.models.compute_tokens(\n model=\"gemini-2.5-flash\",\n contents=\"What's the longest word in the English language?\",\n )\n\n print(response)\n # Example output:\n # tokens_info=[TokensInfo(\n # role='user',\n # token_ids=[1841, 235303, 235256, 573, 32514, 2204, 575, 573, 4645, 5255, 235336],\n # tokens=[b'What', b\"'\", b's', b' the', b' longest', b' word', b' in', b' the', b' English', b' language', b'?']\n # )]\n\n### Go\n\nLearn how to install or update the [Go](/vertex-ai/generative-ai/docs/sdks/overview).\n\n\nTo learn more, see the\n[SDK reference documentation](https://pkg.go.dev/google.golang.org/genai).\n\n\nSet environment variables to use the Gen AI SDK with Vertex AI:\n\n```bash\n# Replace the `GOOGLE_CLOUD_PROJECT` and `GOOGLE_CLOUD_LOCATION` values\n# with appropriate values for your project.\nexport GOOGLE_CLOUD_PROJECT=GOOGLE_CLOUD_PROJECT\nexport GOOGLE_CLOUD_LOCATION=global\nexport GOOGLE_GENAI_USE_VERTEXAI=True\n```\n\n\u003cbr /\u003e\n\n import (\n \t\"context\"\n \t\"encoding/json\"\n \t\"fmt\"\n \t\"io\"\n\n \tgenai \"google.golang.org/genai\"\n )\n\n // computeWithTxt shows how to compute tokens with text input.\n func computeWithTxt(w io.Writer) error {\n \tctx := context.Background()\n\n \tclient, err := genai.NewClient(ctx, &genai.ClientConfig{\n \t\tHTTPOptions: genai.HTTPOptions{APIVersion: \"v1\"},\n \t})\n \tif err != nil {\n \t\treturn fmt.Errorf(\"failed to create genai client: %w\", err)\n \t}\n\n \tmodelName := \"gemini-2.5-flash\"\n \tcontents := []*genai.Content{\n \t\t{Parts: []*genai.Part{\n \t\t\t{Text: \"What's the longest word in the English language?\"},\n \t\t},\n \t\t\tRole: \"user\"},\n \t}\n\n \tresp, err := client.Models.ComputeTokens(ctx, modelName, contents, nil)\n \tif err != nil {\n \t\treturn fmt.Errorf(\"failed to generate content: %w\", err)\n \t}\n\n \ttype tokenInfoDisplay struct {\n \t\tIDs []int64 `json:\"token_ids\"`\n \t\tTokens []string `json:\"tokens\"`\n \t}\n \t// See the documentation: https://pkg.go.dev/google.golang.org/genai#ComputeTokensResponse\n \tfor _, instance := range resp.TokensInfo {\n \t\tdisplay := tokenInfoDisplay{\n \t\t\tIDs: instance.TokenIDs,\n \t\t\tTokens: make([]string, len(instance.Tokens)),\n \t\t}\n \t\tfor i, t := range instance.Tokens {\n \t\t\tdisplay.Tokens[i] = string(t)\n \t\t}\n\n \t\tdata, err := json.MarshalIndent(display, \"\", \" \")\n \t\tif err != nil {\n \t\t\treturn fmt.Errorf(\"failed to marshal token info: %w\", err)\n \t\t}\n \t\tfmt.Fprintln(w, string(data))\n \t}\n\n \t// Example response:\n \t// {\n \t// \t\"ids\": [\n \t// \t\t1841,\n \t// \t\t235303,\n \t// \t\t235256,\n \t// ...\n \t// \t],\n \t// \t\"values\": [\n \t// \t\t\"What\",\n \t// \t\t\"'\",\n \t// \t\t\"s\",\n \t// ...\n \t// \t]\n \t// }\n\n \treturn nil\n }\n\n\u003cbr /\u003e\n\nGet the token count and billable characters of a prompt\n-------------------------------------------------------\n\nThe following code sample shows you how to Get the token count and the number of\nbillable characters of a prompt. Both text-only and multimodal prompts are\nsupported. \n\n### Python\n\n#### Install\n\n```\npip install --upgrade google-genai\n```\n\n\nTo learn more, see the\n[SDK reference documentation](https://googleapis.github.io/python-genai/).\n\n\nSet environment variables to use the Gen AI SDK with Vertex AI:\n\n```bash\n# Replace the `GOOGLE_CLOUD_PROJECT` and `GOOGLE_CLOUD_LOCATION` values\n# with appropriate values for your project.\nexport GOOGLE_CLOUD_PROJECT=GOOGLE_CLOUD_PROJECT\nexport GOOGLE_CLOUD_LOCATION=global\nexport GOOGLE_GENAI_USE_VERTEXAI=True\n```\n\n\u003cbr /\u003e\n\n from google import genai\n from google.genai.types import HttpOptions\n\n client = genai.Client(http_options=HttpOptions(api_version=\"v1\"))\n\n prompt = \"Why is the sky blue?\"\n\n # Send text to Gemini\n response = client.models.generate_content(\n model=\"gemini-2.5-flash\", contents=prompt\n )\n\n # Prompt and response tokens count\n print(response.usage_metadata)\n\n # Example output:\n # cached_content_token_count=None\n # candidates_token_count=311\n # prompt_token_count=6\n # total_token_count=317\n\n### Go\n\nLearn how to install or update the [Go](/vertex-ai/generative-ai/docs/sdks/overview).\n\n\nTo learn more, see the\n[SDK reference documentation](https://pkg.go.dev/google.golang.org/genai).\n\n\nSet environment variables to use the Gen AI SDK with Vertex AI:\n\n```bash\n# Replace the `GOOGLE_CLOUD_PROJECT` and `GOOGLE_CLOUD_LOCATION` values\n# with appropriate values for your project.\nexport GOOGLE_CLOUD_PROJECT=GOOGLE_CLOUD_PROJECT\nexport GOOGLE_CLOUD_LOCATION=global\nexport GOOGLE_GENAI_USE_VERTEXAI=True\n```\n\n\u003cbr /\u003e\n\n import (\n \t\"context\"\n \t\"encoding/json\"\n \t\"fmt\"\n \t\"io\"\n\n \tgenai \"google.golang.org/genai\"\n )\n\n // generateTextAndCount shows how to generate text and obtain token count metadata from the model response.\n func generateTextAndCount(w io.Writer) error {\n \tctx := context.Background()\n\n \tclient, err := genai.NewClient(ctx, &genai.ClientConfig{\n \t\tHTTPOptions: genai.HTTPOptions{APIVersion: \"v1\"},\n \t})\n \tif err != nil {\n \t\treturn fmt.Errorf(\"failed to create genai client: %w\", err)\n \t}\n\n \tmodelName := \"gemini-2.5-flash\"\n \tcontents := []*genai.Content{\n \t\t{Parts: []*genai.Part{\n \t\t\t{Text: \"Why is the sky blue?\"},\n \t\t},\n \t\t\tRole: \"user\"},\n \t}\n\n \tresp, err := client.Models.GenerateContent(ctx, modelName, contents, nil)\n \tif err != nil {\n \t\treturn fmt.Errorf(\"failed to generate content: %w\", err)\n \t}\n\n \tusage, err := json.MarshalIndent(resp.UsageMetadata, \"\", \" \")\n \tif err != nil {\n \t\treturn fmt.Errorf(\"failed to convert usage metadata to JSON: %w\", err)\n \t}\n \tfmt.Fprintln(w, string(usage))\n\n \t// Example response:\n \t// {\n \t// \t \"candidatesTokenCount\": 339,\n \t// \t \"promptTokenCount\": 6,\n \t// \t \"totalTokenCount\": 345\n \t// }\n\n \treturn nil\n }"]]