Count tokens
/v1/models/{model}:countTokensCount tokens in Gemini input without generating a response. Availability depends on the selected model.
Use POST /v1/models/{model}:countTokens for new integrations. The /v1beta/models/{model}:countTokens endpoint remains supported with the same request and response format.
Authentication
Send your Token360 API key in x-goog-api-key.
Request Body
contentsarrayConversation turns, including the current user input.
contents.rolestringContent author: user or model. Allowed values: user, model.
contents.partsarrayOrdered text, media, or tool parts for this turn.
generateContentRequestobjectComplete generation input, including system instructions and tools. Use this instead of contents.
generateContentRequest.modelstringrequiredModel resource name, in models/{model} format.
generateContentRequest.contentsarrayrequiredConversation turns, including the current user input.
generateContentRequest.systemInstructionobjectSystem instructions expressed as Content; use text parts.
generateContentRequest.generationConfigobjectGeneration controls; supported options vary by model.
generateContentRequest.safetySettingsarraySafety thresholds by category; do not repeat a category.
generateContentRequest.toolsarrayAvailable tools, including callable functions and built-in tools.
generateContentRequest.toolConfigobjectTool selection and retrieval configuration.
generateContentRequest.cachedContentstringExisting cache resource name, in cachedContents/{id} format.
generateContentRequest.labelsobjectUser-defined request labels.
generateContentRequest.serviceTierstringRequested service tier, subject to model and provider availability. Allowed values: unspecified, standard, flex, priority.
generateContentRequest.storebooleanRequest logging preference, when supported.
generateContentRequest.continuationTokenstringContinuation token; resend the original contents unchanged.
Send contents for a conversation, or generateContentRequest to include system instructions, tools, and other generation settings. When using generateContentRequest, set its model to models/{model} using the same model as the URL.
Token counts describe the submitted input. The final usage of a generation request may differ.
Explicit cached-content references are not supported by countTokens. A request containing cachedContent (including inside generateContentRequest, or using snake_case aliases) returns HTTP 501 with status UNIMPLEMENTED, even when explicit caching is enabled. Use the returned cache name with generateContent or streamGenerateContent instead. See Create explicit cache.
Response
totalTokensintegerTotal number of input tokens.
cachedContentTokenCountintegerTokens in cached content, when returned.
promptTokensDetailsobject[]Input token counts by modality, when returned.
promptTokensDetails.modalitystringInput modality.
promptTokensDetails.tokenCountintegerToken count for this modality.
cacheTokensDetailsobject[]Cached token counts by modality, when returned.
cacheTokensDetails.modalitystringCached input modality.
cacheTokensDetails.tokenCountintegerCached token count for this modality.