Count tokens

POST/v1/models/{model}:countTokens

Count 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

contentsarray

Conversation turns, including the current user input.

contents.rolestring

Content author: user or model. Allowed values: user, model.

contents.partsarray

Ordered text, media, or tool parts for this turn.

generateContentRequestobject

Complete generation input, including system instructions and tools. Use this instead of contents.

generateContentRequest.modelstringrequired

Model resource name, in models/{model} format.

generateContentRequest.contentsarrayrequired

Conversation turns, including the current user input.

generateContentRequest.systemInstructionobject

System instructions expressed as Content; use text parts.

generateContentRequest.generationConfigobject

Generation controls; supported options vary by model.

generateContentRequest.safetySettingsarray

Safety thresholds by category; do not repeat a category.

generateContentRequest.toolsarray

Available tools, including callable functions and built-in tools.

generateContentRequest.toolConfigobject

Tool selection and retrieval configuration.

generateContentRequest.cachedContentstring

Existing cache resource name, in cachedContents/{id} format.

generateContentRequest.labelsobject

User-defined request labels.

generateContentRequest.serviceTierstring

Requested service tier, subject to model and provider availability. Allowed values: unspecified, standard, flex, priority.

generateContentRequest.storeboolean

Request logging preference, when supported.

generateContentRequest.continuationTokenstring

Continuation 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

totalTokensinteger

Total number of input tokens.

cachedContentTokenCountinteger

Tokens in cached content, when returned.

promptTokensDetailsobject[]

Input token counts by modality, when returned.

promptTokensDetails.modalitystring

Input modality.

promptTokensDetails.tokenCountinteger

Token count for this modality.

cacheTokensDetailsobject[]

Cached token counts by modality, when returned.

cacheTokensDetails.modalitystring

Cached input modality.

cacheTokensDetails.tokenCountinteger

Cached token count for this modality.

Solicitar

1curl 'https://api.token360.ai/v1/models/gemini-2.5-flash:countTokens' \
2  -H "x-goog-api-key: $TOKEN360_API_KEY" \
3  -H 'Content-Type: application/json' \
4  -d '{"contents":[{"role":"user","parts":[{"text":"Hello!"}]}]}'

Respuesta

JSON
1{"totalTokens":2}
¿Ha sido de ayuda?
Count tokens · Token360