> ## AI Agent Context
> Get the complete documentation index at: https://aistudio.google.com/llms.txt
> Use this file to discover all available pages.
> For the full documentation in a single file, fetch: https://aistudio.google.com/llms-full.txt

# Counting tokens

{% mark class="http-post" %}{% icon name="north_east" /%}`POST`{% /mark %} `https://generativelanguage.googleapis.com/v1/models/{model}:countTokens`

Runs a model's tokenizer on input `Content` and returns the token count.
Refer to the [tokens guide](/docs/tokens)
to learn more about tokens.

{% two-column-layout %}
  {% left-column %}
## Parameters  {% class="hide-from-toc" id="parameters" %}

  {% section class="api-ref-property-list" %}
  **model**: {% mark %}required{% /mark %} `string`

  The model ID, for example `gemini-2.5-flash`. Forms the resource name `models/{model}`.

  {% /section %}

## Request Body  {% class="hide-from-toc" id="request-body" %}

  {% section class="api-ref-property-list" %}
  {% details %}
  {% summary %}
  **contents**: `Content array`

  Optional. The input given to the model as a prompt. This field is ignored when `generate_content_request` is set.

  {% /summary %}

  {% details %}
  {% summary %}
  **parts**: `Part array`

  Ordered `Parts` that constitute a single message. Parts may have different MIME types.

  {% /summary %}

  {% details %}
  {% summary %}
  **data**: `Union type`

  {% /summary %}

  Union field `data` can be only one of the following:

  **text**: `string`

  Inline text.

  {% details %}
  {% summary %}
  **inlineData**: `Blob`

  Inline media bytes.

  {% /summary %}

  **mimeType**: `string`

  The IANA standard MIME type of the source data. Examples of supported types:

  - Images: image/png, image/jpeg, image/jpg, image/webp, image/heic, image/heif, image/gif, image/avif
  - Audio: audio/*, video/audio/s16le, video/audio/wav
  - Video: video/*
  - Text: text/plain, text/html, text/css, text/javascript, text/x-typescript, text/csv, text/markdown, text/x-python, text/xml, text/rtf, video/text/timestamp
  - Applications: application/x-javascript, application/x-typescript, application/x-python-code, application/json, application/x-ipynb+json, application/rtf, application/pdf For additional context, see [Supported file formats](/docs/file-input-methods#supported-content-types).

  **data**: `string`

  Raw bytes for media formats.

  {% /details %}

  {% details %}
  {% summary %}
  **functionCall**: `FunctionCall`

  A predicted `FunctionCall` returned from the model that contains a string representing the `FunctionDeclaration.name` with the arguments and their values.

  {% /summary %}

  **name**: {% mark %}required{% /mark %} `string`

  The name of the function to call. Must be a-z, A-Z, 0-9, or contain underscores and dashes, with a maximum length of 128.

  **id**: `string`

  Optional. Unique identifier of the function call. If populated, the client to execute the `function_call` and return the response with the matching `id`.

  **args**: `object`

  Optional. The function parameters and values in JSON object format.

  {% /details %}

  {% details %}
  {% summary %}
  **functionResponse**: `FunctionResponse`

  The result output of a `FunctionCall` that contains a string representing the `FunctionDeclaration.name` and a structured JSON object containing any output from the function is used as context to the model.

  {% /summary %}

  **name**: {% mark %}required{% /mark %} `string`

  The name of the function to call. Must be a-z, A-Z, 0-9, or contain underscores and dashes, with a maximum length of 128.

  **response**: {% mark %}required{% /mark %} `object`

  The function response in JSON object format. Callers can use any keys of their choice that fit the function's syntax to return the function output, e.g. "output", "result", etc. In particular, if the function call failed to execute, the response can have an "error" key to return error details to the model. Multimedia can be included by using a subobject containing a single "$ref" key whose value is the `inline_data.display_name` or `file_data.display_name` of a `FunctionResponsePart` holding the multimedia. See [Multimodal function responses](/docs/function-calling#multimodal).

  **id**: `string`

  Optional. The identifier of the function call this response is for. Populated by the client to match the corresponding function call `id`.

  {% details %}
  {% summary %}
  **parts**: `FunctionResponsePart array`

  Optional. Ordered `Parts` that constitute a function response. Parts may have different IANA MIME types.

  {% /summary %}

  {% details %}
  {% summary %}
  **data**: `Union type`

  The data of the function response part.

  {% /summary %}

  Union field `data` can be only one of the following:

  {% details %}
  {% summary %}
  **inlineData**: `FunctionResponseBlob`

  Inline media bytes.

  {% /summary %}

  **mimeType**: `string`

  The IANA standard MIME type of the source data. Examples:

  - image/png
  - image/jpeg

  If an unsupported MIME type is provided, an error will be returned. For a complete list of supported types, see [Supported file formats](/docs/file-input-methods#supported-content-types).

  **data**: `string`

  Raw bytes for media formats.

  **displayName**: `string`

  Optional. Display name of the blob. Used to provide a label or filename to distinguish blobs, and to reference the blob from `FunctionResponse.response` (for example, via `{"$ref": "DISPLAY_NAME"}`).

  {% /details %}

  {% details %}
  {% summary %}
  **fileData**: `FunctionResponseFileData`

  URI based data.

  {% /summary %}

  **fileUri**: {% mark %}required{% /mark %} `string`

  URI.

  **mimeType**: `string`

  The IANA standard MIME type of the source data. If an unsupported MIME type is provided, an error will be returned. For a complete list of supported types, see [Multimodal function responses](/docs/function-calling#multimodal) and [Supported file formats](/docs/file-input-methods#supported-content-types).

  **displayName**: `string`

  Optional. Display name of the file. Used to provide a label or filename to distinguish file data, and to reference the file from `FunctionResponse.response` (for example, via `{"$ref": "DISPLAY_NAME"}`).

  {% /details %}

  {% /details %}

  {% /details %}

  {% /details %}

  {% details %}
  {% summary %}
  **fileData**: `FileData`

  URI based data.

  {% /summary %}

  **fileUri**: {% mark %}required{% /mark %} `string`

  URI.

  **mimeType**: `string`

  Optional. The IANA standard MIME type of the source data.

  {% /details %}

  {% details %}
  {% summary %}
  **executableCode**: `ExecutableCode`

  Code generated by the model that is meant to be executed.

  {% /summary %}

  {% details %}
  {% summary %}
  **language**: {% mark %}required{% /mark %} `string enum`

  Programming language of the `code`.

  {% /summary %}

  Possible values:

  - `LANGUAGE_UNSPECIFIED`: Unspecified language. This value should not be used.
  - `PYTHON`: Python >= 3.10, with numpy and simpy available. Python is the default language.

  {% /details %}

  **code**: {% mark %}required{% /mark %} `string`

  The code to be executed.

  **id**: `string`

  Optional. Unique identifier of the `ExecutableCode` part. The server returns the `CodeExecutionResult` with the matching `id`.

  {% /details %}

  {% details %}
  {% summary %}
  **codeExecutionResult**: `CodeExecutionResult`

  Result of executing the `ExecutableCode`.

  {% /summary %}

  {% details %}
  {% summary %}
  **outcome**: {% mark %}required{% /mark %} `string enum`

  Outcome of the code execution.

  {% /summary %}

  Possible values:

  - `OUTCOME_UNSPECIFIED`: Unspecified status. This value should not be used.
  - `OUTCOME_OK`: Code execution completed successfully. `output` contains the stdout, if any.
  - `OUTCOME_FAILED`: Code execution failed. `output` contains the stderr and stdout, if any.
  - `OUTCOME_DEADLINE_EXCEEDED`: Code execution ran for too long, and was cancelled. There may or may not be a partial `output` present.

  {% /details %}

  **id**: `string`

  Optional. The identifier of the `ExecutableCode` part this result is for. Only populated if the corresponding `ExecutableCode` has an id.

  **output**: `string`

  Optional. Contains stdout when code execution is successful, stderr or other description otherwise.

  {% /details %}

  {% details %}
  {% summary %}
  **toolCall**: `ToolCall`

  Server-side tool call. This field is populated when the model predicts a tool invocation that should be executed on the server. The client is expected to echo this message back to the API.

  {% /summary %}

  {% details %}
  {% summary %}
  **toolType**: {% mark %}required{% /mark %} `ToolType`

  The type of tool that was called.

  {% /summary %}

  Possible values:

  - `TOOL_TYPE_UNSPECIFIED`: Unspecified tool type.
  - `GOOGLE_SEARCH_WEB`: Google search tool, maps to Tool.google_search.search_types.web_search.
  - `GOOGLE_SEARCH_IMAGE`: Image search tool, maps to Tool.google_search.search_types.image_search.
  - `URL_CONTEXT`: URL context tool, maps to Tool.url_context.
  - `GOOGLE_MAPS`: Google maps tool, maps to Tool.google_maps.
  - `FILE_SEARCH`: File search tool, maps to Tool.file_search.
  - `MEDIA_PROCESSING`: Media processing tool types (e.g. video, audio, etc.), maps to invisible tools used for multimodal understanding like GOOGLE_UNDERSTANDING_VIDEO.

  {% /details %}

  **id**: `string`

  Optional. Unique identifier of the tool call. The server returns the tool response with the matching `id`.

  **toolName**: `string`

  Optional. The name of the tool that was called.

  **args**: `object`

  Optional. The tool call arguments. Example: {"arg1" : "value1", "arg2" : "value2" , ...}

  {% /details %}

  {% details %}
  {% summary %}
  **toolResponse**: `ToolResponse`

  The output from a server-side `ToolCall` execution. This field is populated by the client with the results of executing the corresponding `ToolCall`.

  {% /summary %}

  {% details %}
  {% summary %}
  **toolType**: {% mark %}required{% /mark %} `ToolType`

  The type of tool that was called, matching the `tool_type` in the corresponding `ToolCall`.

  {% /summary %}

  Possible values:

  - `TOOL_TYPE_UNSPECIFIED`: Unspecified tool type.
  - `GOOGLE_SEARCH_WEB`: Google search tool, maps to Tool.google_search.search_types.web_search.
  - `GOOGLE_SEARCH_IMAGE`: Image search tool, maps to Tool.google_search.search_types.image_search.
  - `URL_CONTEXT`: URL context tool, maps to Tool.url_context.
  - `GOOGLE_MAPS`: Google maps tool, maps to Tool.google_maps.
  - `FILE_SEARCH`: File search tool, maps to Tool.file_search.
  - `MEDIA_PROCESSING`: Media processing tool types (e.g. video, audio, etc.), maps to invisible tools used for multimodal understanding like GOOGLE_UNDERSTANDING_VIDEO.

  {% /details %}

  **id**: `string`

  Optional. The identifier of the tool call this response is for.

  **response**: `object`

  Optional. The tool response.

  {% /details %}

  {% /details %}

  {% details %}
  {% summary %}
  **metadata**: `Union type`

  Controls extra preprocessing of data. Set this when `inline_data` or `file_data` is present to configure per-part static media preprocessing.

  {% /summary %}

  Union field `metadata` can be only one of the following:

  {% details %}
  {% summary %}
  **videoMetadata**: `VideoMetadata`

  Optional. Video metadata. The metadata should only be specified while the video data is presented in inline_data or file_data.

  {% /summary %}

  **startOffset**: `string`

  Optional. The start offset of the video.

  **endOffset**: `string`

  Optional. The end offset of the video.

  **fps**: `number`

  Optional. The frame rate of the video sent to the model. If not specified, the default value will be 1.0. The fps range is (0.0, 24.0].

  {% /details %}

  {% /details %}

  **thought**: `boolean`

  Optional. Indicates if the part is thought from the model.

  **thoughtSignature**: `string`

  Optional. An opaque signature for the thought so it can be reused in subsequent requests.

  **partMetadata**: `object`

  Custom metadata associated with the Part. Agents using genai.Part as content representation may need to keep track of the additional information. For example it can be name of a file/source from which the Part originates or a way to multiplex multiple Part streams.

  {% details %}
  {% summary %}
  **mediaResolution**: `MediaResolution`

  Optional. Media resolution for the input media.

  {% /summary %}

  {% details %}
  {% summary %}
  **value**: `Union type`

  The media resolution level.

  {% /summary %}

  Union field `value` can be only one of the following:

  {% details %}
  {% summary %}
  **level**: `string enum`

  The tokenization quality used for given media. for Gemini API support .

  {% /summary %}

  Possible values:

  - `MEDIA_RESOLUTION_UNSPECIFIED`: Media resolution has not been set.
  - `MEDIA_RESOLUTION_LOW`: Media resolution set to low.
  - `MEDIA_RESOLUTION_MEDIUM`: Media resolution set to medium.
  - `MEDIA_RESOLUTION_HIGH`: Media resolution set to high.
  - `MEDIA_RESOLUTION_ULTRA_HIGH`: Media resolution set to ultra high.

  {% /details %}

  {% /details %}

  {% /details %}

  {% details %}
  {% summary %}
  **mediaProcessing**: `string enum`

  Optional. How the model processes this part's media for understanding. Only meaningful for video parts (`inline_data` or `file_data` with video mime). Non-video parts ignore this field.

  {% /summary %}

  Possible values:

  - `MEDIA_PROCESSING_UNSPECIFIED`: Defaults to model-specific processing.
  - `STATIC`: Fixed-rate frame extraction. All frames placed in context.
  - `AGENTIC`: Model-driven dynamic navigation. Recommended for most use cases.

  {% /details %}

  {% /details %}

  **role**: `string`

  Optional. The producer of the content. Must be either 'user' or 'model'. Useful to set for multi-turn conversations, otherwise can be left blank or unset.

  {% /details %}

  {% hr /%}

  {% details %}
  {% summary %}
  **generateContentRequest**: `GenerateContentRequest`

  Optional. The overall input given to the `Model`. This includes the prompt as well as other model steering information like [system instructions](/docs/text-generation#system-instructions), and/or function declarations for [function calling](/docs/function-calling). `Model`s/`Content`s and `generate_content_request`s are mutually exclusive. You can either send `Model` + `Content`s or a `generate_content_request`, but never both.

  {% /summary %}

  **model**: {% mark %}required{% /mark %} `string`

  The name of the `Model` to use for generating the completion. Format: `models/{model}`.

  {% details %}
  {% summary %}
  **contents**: {% mark %}required{% /mark %} `Content array`

  The content of the current conversation with the model. For single-turn queries, this is a single instance. For multi-turn queries like [chat](/docs/text-generation#multi-turn-conversations), this is a repeated field that contains the conversation history and the latest request.

  {% /summary %}

  {% details %}
  {% summary %}
  **parts**: `Part array`

  Ordered `Parts` that constitute a single message. Parts may have different MIME types.

  {% /summary %}

  {% details %}
  {% summary %}
  **data**: `Union type`

  {% /summary %}

  Union field `data` can be only one of the following:

  **text**: `string`

  Inline text.

  {% details %}
  {% summary %}
  **inlineData**: `Blob`

  Inline media bytes.

  {% /summary %}

  **mimeType**: `string`

  The IANA standard MIME type of the source data. Examples of supported types:

  - Images: image/png, image/jpeg, image/jpg, image/webp, image/heic, image/heif, image/gif, image/avif
  - Audio: audio/*, video/audio/s16le, video/audio/wav
  - Video: video/*
  - Text: text/plain, text/html, text/css, text/javascript, text/x-typescript, text/csv, text/markdown, text/x-python, text/xml, text/rtf, video/text/timestamp
  - Applications: application/x-javascript, application/x-typescript, application/x-python-code, application/json, application/x-ipynb+json, application/rtf, application/pdf For additional context, see [Supported file formats](/docs/file-input-methods#supported-content-types).

  **data**: `string`

  Raw bytes for media formats.

  {% /details %}

  {% details %}
  {% summary %}
  **functionCall**: `FunctionCall`

  A predicted `FunctionCall` returned from the model that contains a string representing the `FunctionDeclaration.name` with the arguments and their values.

  {% /summary %}

  **name**: {% mark %}required{% /mark %} `string`

  The name of the function to call. Must be a-z, A-Z, 0-9, or contain underscores and dashes, with a maximum length of 128.

  **id**: `string`

  Optional. Unique identifier of the function call. If populated, the client to execute the `function_call` and return the response with the matching `id`.

  **args**: `object`

  Optional. The function parameters and values in JSON object format.

  {% /details %}

  {% details %}
  {% summary %}
  **functionResponse**: `FunctionResponse`

  The result output of a `FunctionCall` that contains a string representing the `FunctionDeclaration.name` and a structured JSON object containing any output from the function is used as context to the model.

  {% /summary %}

  **name**: {% mark %}required{% /mark %} `string`

  The name of the function to call. Must be a-z, A-Z, 0-9, or contain underscores and dashes, with a maximum length of 128.

  **response**: {% mark %}required{% /mark %} `object`

  The function response in JSON object format. Callers can use any keys of their choice that fit the function's syntax to return the function output, e.g. "output", "result", etc. In particular, if the function call failed to execute, the response can have an "error" key to return error details to the model. Multimedia can be included by using a subobject containing a single "$ref" key whose value is the `inline_data.display_name` or `file_data.display_name` of a `FunctionResponsePart` holding the multimedia. See [Multimodal function responses](/docs/function-calling#multimodal).

  **id**: `string`

  Optional. The identifier of the function call this response is for. Populated by the client to match the corresponding function call `id`.

  {% details %}
  {% summary %}
  **parts**: `FunctionResponsePart array`

  Optional. Ordered `Parts` that constitute a function response. Parts may have different IANA MIME types.

  {% /summary %}

  {% details %}
  {% summary %}
  **data**: `Union type`

  The data of the function response part.

  {% /summary %}

  Union field `data` can be only one of the following:

  {% details %}
  {% summary %}
  **inlineData**: `FunctionResponseBlob`

  Inline media bytes.

  {% /summary %}

  **mimeType**: `string`

  The IANA standard MIME type of the source data. Examples:

  - image/png
  - image/jpeg

  If an unsupported MIME type is provided, an error will be returned. For a complete list of supported types, see [Supported file formats](/docs/file-input-methods#supported-content-types).

  **data**: `string`

  Raw bytes for media formats.

  **displayName**: `string`

  Optional. Display name of the blob. Used to provide a label or filename to distinguish blobs, and to reference the blob from `FunctionResponse.response` (for example, via `{"$ref": "DISPLAY_NAME"}`).

  {% /details %}

  {% details %}
  {% summary %}
  **fileData**: `FunctionResponseFileData`

  URI based data.

  {% /summary %}

  **fileUri**: {% mark %}required{% /mark %} `string`

  URI.

  **mimeType**: `string`

  The IANA standard MIME type of the source data. If an unsupported MIME type is provided, an error will be returned. For a complete list of supported types, see [Multimodal function responses](/docs/function-calling#multimodal) and [Supported file formats](/docs/file-input-methods#supported-content-types).

  **displayName**: `string`

  Optional. Display name of the file. Used to provide a label or filename to distinguish file data, and to reference the file from `FunctionResponse.response` (for example, via `{"$ref": "DISPLAY_NAME"}`).

  {% /details %}

  {% /details %}

  {% /details %}

  {% /details %}

  {% details %}
  {% summary %}
  **fileData**: `FileData`

  URI based data.

  {% /summary %}

  **fileUri**: {% mark %}required{% /mark %} `string`

  URI.

  **mimeType**: `string`

  Optional. The IANA standard MIME type of the source data.

  {% /details %}

  {% details %}
  {% summary %}
  **executableCode**: `ExecutableCode`

  Code generated by the model that is meant to be executed.

  {% /summary %}

  {% details %}
  {% summary %}
  **language**: {% mark %}required{% /mark %} `string enum`

  Programming language of the `code`.

  {% /summary %}

  Possible values:

  - `LANGUAGE_UNSPECIFIED`: Unspecified language. This value should not be used.
  - `PYTHON`: Python >= 3.10, with numpy and simpy available. Python is the default language.

  {% /details %}

  **code**: {% mark %}required{% /mark %} `string`

  The code to be executed.

  **id**: `string`

  Optional. Unique identifier of the `ExecutableCode` part. The server returns the `CodeExecutionResult` with the matching `id`.

  {% /details %}

  {% details %}
  {% summary %}
  **codeExecutionResult**: `CodeExecutionResult`

  Result of executing the `ExecutableCode`.

  {% /summary %}

  {% details %}
  {% summary %}
  **outcome**: {% mark %}required{% /mark %} `string enum`

  Outcome of the code execution.

  {% /summary %}

  Possible values:

  - `OUTCOME_UNSPECIFIED`: Unspecified status. This value should not be used.
  - `OUTCOME_OK`: Code execution completed successfully. `output` contains the stdout, if any.
  - `OUTCOME_FAILED`: Code execution failed. `output` contains the stderr and stdout, if any.
  - `OUTCOME_DEADLINE_EXCEEDED`: Code execution ran for too long, and was cancelled. There may or may not be a partial `output` present.

  {% /details %}

  **id**: `string`

  Optional. The identifier of the `ExecutableCode` part this result is for. Only populated if the corresponding `ExecutableCode` has an id.

  **output**: `string`

  Optional. Contains stdout when code execution is successful, stderr or other description otherwise.

  {% /details %}

  {% details %}
  {% summary %}
  **toolCall**: `ToolCall`

  Server-side tool call. This field is populated when the model predicts a tool invocation that should be executed on the server. The client is expected to echo this message back to the API.

  {% /summary %}

  {% details %}
  {% summary %}
  **toolType**: {% mark %}required{% /mark %} `ToolType`

  The type of tool that was called.

  {% /summary %}

  Possible values:

  - `TOOL_TYPE_UNSPECIFIED`: Unspecified tool type.
  - `GOOGLE_SEARCH_WEB`: Google search tool, maps to Tool.google_search.search_types.web_search.
  - `GOOGLE_SEARCH_IMAGE`: Image search tool, maps to Tool.google_search.search_types.image_search.
  - `URL_CONTEXT`: URL context tool, maps to Tool.url_context.
  - `GOOGLE_MAPS`: Google maps tool, maps to Tool.google_maps.
  - `FILE_SEARCH`: File search tool, maps to Tool.file_search.
  - `MEDIA_PROCESSING`: Media processing tool types (e.g. video, audio, etc.), maps to invisible tools used for multimodal understanding like GOOGLE_UNDERSTANDING_VIDEO.

  {% /details %}

  **id**: `string`

  Optional. Unique identifier of the tool call. The server returns the tool response with the matching `id`.

  **toolName**: `string`

  Optional. The name of the tool that was called.

  **args**: `object`

  Optional. The tool call arguments. Example: {"arg1" : "value1", "arg2" : "value2" , ...}

  {% /details %}

  {% details %}
  {% summary %}
  **toolResponse**: `ToolResponse`

  The output from a server-side `ToolCall` execution. This field is populated by the client with the results of executing the corresponding `ToolCall`.

  {% /summary %}

  {% details %}
  {% summary %}
  **toolType**: {% mark %}required{% /mark %} `ToolType`

  The type of tool that was called, matching the `tool_type` in the corresponding `ToolCall`.

  {% /summary %}

  Possible values:

  - `TOOL_TYPE_UNSPECIFIED`: Unspecified tool type.
  - `GOOGLE_SEARCH_WEB`: Google search tool, maps to Tool.google_search.search_types.web_search.
  - `GOOGLE_SEARCH_IMAGE`: Image search tool, maps to Tool.google_search.search_types.image_search.
  - `URL_CONTEXT`: URL context tool, maps to Tool.url_context.
  - `GOOGLE_MAPS`: Google maps tool, maps to Tool.google_maps.
  - `FILE_SEARCH`: File search tool, maps to Tool.file_search.
  - `MEDIA_PROCESSING`: Media processing tool types (e.g. video, audio, etc.), maps to invisible tools used for multimodal understanding like GOOGLE_UNDERSTANDING_VIDEO.

  {% /details %}

  **id**: `string`

  Optional. The identifier of the tool call this response is for.

  **response**: `object`

  Optional. The tool response.

  {% /details %}

  {% /details %}

  {% details %}
  {% summary %}
  **metadata**: `Union type`

  Controls extra preprocessing of data. Set this when `inline_data` or `file_data` is present to configure per-part static media preprocessing.

  {% /summary %}

  Union field `metadata` can be only one of the following:

  {% details %}
  {% summary %}
  **videoMetadata**: `VideoMetadata`

  Optional. Video metadata. The metadata should only be specified while the video data is presented in inline_data or file_data.

  {% /summary %}

  **startOffset**: `string`

  Optional. The start offset of the video.

  **endOffset**: `string`

  Optional. The end offset of the video.

  **fps**: `number`

  Optional. The frame rate of the video sent to the model. If not specified, the default value will be 1.0. The fps range is (0.0, 24.0].

  {% /details %}

  {% /details %}

  **thought**: `boolean`

  Optional. Indicates if the part is thought from the model.

  **thoughtSignature**: `string`

  Optional. An opaque signature for the thought so it can be reused in subsequent requests.

  **partMetadata**: `object`

  Custom metadata associated with the Part. Agents using genai.Part as content representation may need to keep track of the additional information. For example it can be name of a file/source from which the Part originates or a way to multiplex multiple Part streams.

  {% details %}
  {% summary %}
  **mediaResolution**: `MediaResolution`

  Optional. Media resolution for the input media.

  {% /summary %}

  {% details %}
  {% summary %}
  **value**: `Union type`

  The media resolution level.

  {% /summary %}

  Union field `value` can be only one of the following:

  {% details %}
  {% summary %}
  **level**: `string enum`

  The tokenization quality used for given media. for Gemini API support .

  {% /summary %}

  Possible values:

  - `MEDIA_RESOLUTION_UNSPECIFIED`: Media resolution has not been set.
  - `MEDIA_RESOLUTION_LOW`: Media resolution set to low.
  - `MEDIA_RESOLUTION_MEDIUM`: Media resolution set to medium.
  - `MEDIA_RESOLUTION_HIGH`: Media resolution set to high.
  - `MEDIA_RESOLUTION_ULTRA_HIGH`: Media resolution set to ultra high.

  {% /details %}

  {% /details %}

  {% /details %}

  {% details %}
  {% summary %}
  **mediaProcessing**: `string enum`

  Optional. How the model processes this part's media for understanding. Only meaningful for video parts (`inline_data` or `file_data` with video mime). Non-video parts ignore this field.

  {% /summary %}

  Possible values:

  - `MEDIA_PROCESSING_UNSPECIFIED`: Defaults to model-specific processing.
  - `STATIC`: Fixed-rate frame extraction. All frames placed in context.
  - `AGENTIC`: Model-driven dynamic navigation. Recommended for most use cases.

  {% /details %}

  {% /details %}

  **role**: `string`

  Optional. The producer of the content. Must be either 'user' or 'model'. Useful to set for multi-turn conversations, otherwise can be left blank or unset.

  {% /details %}

  {% details %}
  {% summary %}
  **systemInstruction**: `Content`

  Optional. Developer set [system instruction(s)](/docs/text-generation#system-instructions). Currently, text only.

  {% /summary %}

  {% details %}
  {% summary %}
  **parts**: `Part array`

  Ordered `Parts` that constitute a single message. Parts may have different MIME types.

  {% /summary %}

  {% details %}
  {% summary %}
  **data**: `Union type`

  {% /summary %}

  Union field `data` can be only one of the following:

  **text**: `string`

  Inline text.

  {% details %}
  {% summary %}
  **inlineData**: `Blob`

  Inline media bytes.

  {% /summary %}

  **mimeType**: `string`

  The IANA standard MIME type of the source data. Examples of supported types:

  - Images: image/png, image/jpeg, image/jpg, image/webp, image/heic, image/heif, image/gif, image/avif
  - Audio: audio/*, video/audio/s16le, video/audio/wav
  - Video: video/*
  - Text: text/plain, text/html, text/css, text/javascript, text/x-typescript, text/csv, text/markdown, text/x-python, text/xml, text/rtf, video/text/timestamp
  - Applications: application/x-javascript, application/x-typescript, application/x-python-code, application/json, application/x-ipynb+json, application/rtf, application/pdf For additional context, see [Supported file formats](/docs/file-input-methods#supported-content-types).

  **data**: `string`

  Raw bytes for media formats.

  {% /details %}

  {% details %}
  {% summary %}
  **functionCall**: `FunctionCall`

  A predicted `FunctionCall` returned from the model that contains a string representing the `FunctionDeclaration.name` with the arguments and their values.

  {% /summary %}

  **name**: {% mark %}required{% /mark %} `string`

  The name of the function to call. Must be a-z, A-Z, 0-9, or contain underscores and dashes, with a maximum length of 128.

  **id**: `string`

  Optional. Unique identifier of the function call. If populated, the client to execute the `function_call` and return the response with the matching `id`.

  **args**: `object`

  Optional. The function parameters and values in JSON object format.

  {% /details %}

  {% details %}
  {% summary %}
  **functionResponse**: `FunctionResponse`

  The result output of a `FunctionCall` that contains a string representing the `FunctionDeclaration.name` and a structured JSON object containing any output from the function is used as context to the model.

  {% /summary %}

  **name**: {% mark %}required{% /mark %} `string`

  The name of the function to call. Must be a-z, A-Z, 0-9, or contain underscores and dashes, with a maximum length of 128.

  **response**: {% mark %}required{% /mark %} `object`

  The function response in JSON object format. Callers can use any keys of their choice that fit the function's syntax to return the function output, e.g. "output", "result", etc. In particular, if the function call failed to execute, the response can have an "error" key to return error details to the model. Multimedia can be included by using a subobject containing a single "$ref" key whose value is the `inline_data.display_name` or `file_data.display_name` of a `FunctionResponsePart` holding the multimedia. See [Multimodal function responses](/docs/function-calling#multimodal).

  **id**: `string`

  Optional. The identifier of the function call this response is for. Populated by the client to match the corresponding function call `id`.

  {% details %}
  {% summary %}
  **parts**: `FunctionResponsePart array`

  Optional. Ordered `Parts` that constitute a function response. Parts may have different IANA MIME types.

  {% /summary %}

  {% details %}
  {% summary %}
  **data**: `Union type`

  The data of the function response part.

  {% /summary %}

  Union field `data` can be only one of the following:

  {% details %}
  {% summary %}
  **inlineData**: `FunctionResponseBlob`

  Inline media bytes.

  {% /summary %}

  **mimeType**: `string`

  The IANA standard MIME type of the source data. Examples:

  - image/png
  - image/jpeg

  If an unsupported MIME type is provided, an error will be returned. For a complete list of supported types, see [Supported file formats](/docs/file-input-methods#supported-content-types).

  **data**: `string`

  Raw bytes for media formats.

  **displayName**: `string`

  Optional. Display name of the blob. Used to provide a label or filename to distinguish blobs, and to reference the blob from `FunctionResponse.response` (for example, via `{"$ref": "DISPLAY_NAME"}`).

  {% /details %}

  {% details %}
  {% summary %}
  **fileData**: `FunctionResponseFileData`

  URI based data.

  {% /summary %}

  **fileUri**: {% mark %}required{% /mark %} `string`

  URI.

  **mimeType**: `string`

  The IANA standard MIME type of the source data. If an unsupported MIME type is provided, an error will be returned. For a complete list of supported types, see [Multimodal function responses](/docs/function-calling#multimodal) and [Supported file formats](/docs/file-input-methods#supported-content-types).

  **displayName**: `string`

  Optional. Display name of the file. Used to provide a label or filename to distinguish file data, and to reference the file from `FunctionResponse.response` (for example, via `{"$ref": "DISPLAY_NAME"}`).

  {% /details %}

  {% /details %}

  {% /details %}

  {% /details %}

  {% details %}
  {% summary %}
  **fileData**: `FileData`

  URI based data.

  {% /summary %}

  **fileUri**: {% mark %}required{% /mark %} `string`

  URI.

  **mimeType**: `string`

  Optional. The IANA standard MIME type of the source data.

  {% /details %}

  {% details %}
  {% summary %}
  **executableCode**: `ExecutableCode`

  Code generated by the model that is meant to be executed.

  {% /summary %}

  {% details %}
  {% summary %}
  **language**: {% mark %}required{% /mark %} `string enum`

  Programming language of the `code`.

  {% /summary %}

  Possible values:

  - `LANGUAGE_UNSPECIFIED`: Unspecified language. This value should not be used.
  - `PYTHON`: Python >= 3.10, with numpy and simpy available. Python is the default language.

  {% /details %}

  **code**: {% mark %}required{% /mark %} `string`

  The code to be executed.

  **id**: `string`

  Optional. Unique identifier of the `ExecutableCode` part. The server returns the `CodeExecutionResult` with the matching `id`.

  {% /details %}

  {% details %}
  {% summary %}
  **codeExecutionResult**: `CodeExecutionResult`

  Result of executing the `ExecutableCode`.

  {% /summary %}

  {% details %}
  {% summary %}
  **outcome**: {% mark %}required{% /mark %} `string enum`

  Outcome of the code execution.

  {% /summary %}

  Possible values:

  - `OUTCOME_UNSPECIFIED`: Unspecified status. This value should not be used.
  - `OUTCOME_OK`: Code execution completed successfully. `output` contains the stdout, if any.
  - `OUTCOME_FAILED`: Code execution failed. `output` contains the stderr and stdout, if any.
  - `OUTCOME_DEADLINE_EXCEEDED`: Code execution ran for too long, and was cancelled. There may or may not be a partial `output` present.

  {% /details %}

  **id**: `string`

  Optional. The identifier of the `ExecutableCode` part this result is for. Only populated if the corresponding `ExecutableCode` has an id.

  **output**: `string`

  Optional. Contains stdout when code execution is successful, stderr or other description otherwise.

  {% /details %}

  {% details %}
  {% summary %}
  **toolCall**: `ToolCall`

  Server-side tool call. This field is populated when the model predicts a tool invocation that should be executed on the server. The client is expected to echo this message back to the API.

  {% /summary %}

  {% details %}
  {% summary %}
  **toolType**: {% mark %}required{% /mark %} `ToolType`

  The type of tool that was called.

  {% /summary %}

  Possible values:

  - `TOOL_TYPE_UNSPECIFIED`: Unspecified tool type.
  - `GOOGLE_SEARCH_WEB`: Google search tool, maps to Tool.google_search.search_types.web_search.
  - `GOOGLE_SEARCH_IMAGE`: Image search tool, maps to Tool.google_search.search_types.image_search.
  - `URL_CONTEXT`: URL context tool, maps to Tool.url_context.
  - `GOOGLE_MAPS`: Google maps tool, maps to Tool.google_maps.
  - `FILE_SEARCH`: File search tool, maps to Tool.file_search.
  - `MEDIA_PROCESSING`: Media processing tool types (e.g. video, audio, etc.), maps to invisible tools used for multimodal understanding like GOOGLE_UNDERSTANDING_VIDEO.

  {% /details %}

  **id**: `string`

  Optional. Unique identifier of the tool call. The server returns the tool response with the matching `id`.

  **toolName**: `string`

  Optional. The name of the tool that was called.

  **args**: `object`

  Optional. The tool call arguments. Example: {"arg1" : "value1", "arg2" : "value2" , ...}

  {% /details %}

  {% details %}
  {% summary %}
  **toolResponse**: `ToolResponse`

  The output from a server-side `ToolCall` execution. This field is populated by the client with the results of executing the corresponding `ToolCall`.

  {% /summary %}

  {% details %}
  {% summary %}
  **toolType**: {% mark %}required{% /mark %} `ToolType`

  The type of tool that was called, matching the `tool_type` in the corresponding `ToolCall`.

  {% /summary %}

  Possible values:

  - `TOOL_TYPE_UNSPECIFIED`: Unspecified tool type.
  - `GOOGLE_SEARCH_WEB`: Google search tool, maps to Tool.google_search.search_types.web_search.
  - `GOOGLE_SEARCH_IMAGE`: Image search tool, maps to Tool.google_search.search_types.image_search.
  - `URL_CONTEXT`: URL context tool, maps to Tool.url_context.
  - `GOOGLE_MAPS`: Google maps tool, maps to Tool.google_maps.
  - `FILE_SEARCH`: File search tool, maps to Tool.file_search.
  - `MEDIA_PROCESSING`: Media processing tool types (e.g. video, audio, etc.), maps to invisible tools used for multimodal understanding like GOOGLE_UNDERSTANDING_VIDEO.

  {% /details %}

  **id**: `string`

  Optional. The identifier of the tool call this response is for.

  **response**: `object`

  Optional. The tool response.

  {% /details %}

  {% /details %}

  {% details %}
  {% summary %}
  **metadata**: `Union type`

  Controls extra preprocessing of data. Set this when `inline_data` or `file_data` is present to configure per-part static media preprocessing.

  {% /summary %}

  Union field `metadata` can be only one of the following:

  {% details %}
  {% summary %}
  **videoMetadata**: `VideoMetadata`

  Optional. Video metadata. The metadata should only be specified while the video data is presented in inline_data or file_data.

  {% /summary %}

  **startOffset**: `string`

  Optional. The start offset of the video.

  **endOffset**: `string`

  Optional. The end offset of the video.

  **fps**: `number`

  Optional. The frame rate of the video sent to the model. If not specified, the default value will be 1.0. The fps range is (0.0, 24.0].

  {% /details %}

  {% /details %}

  **thought**: `boolean`

  Optional. Indicates if the part is thought from the model.

  **thoughtSignature**: `string`

  Optional. An opaque signature for the thought so it can be reused in subsequent requests.

  **partMetadata**: `object`

  Custom metadata associated with the Part. Agents using genai.Part as content representation may need to keep track of the additional information. For example it can be name of a file/source from which the Part originates or a way to multiplex multiple Part streams.

  {% details %}
  {% summary %}
  **mediaResolution**: `MediaResolution`

  Optional. Media resolution for the input media.

  {% /summary %}

  {% details %}
  {% summary %}
  **value**: `Union type`

  The media resolution level.

  {% /summary %}

  Union field `value` can be only one of the following:

  {% details %}
  {% summary %}
  **level**: `string enum`

  The tokenization quality used for given media. for Gemini API support .

  {% /summary %}

  Possible values:

  - `MEDIA_RESOLUTION_UNSPECIFIED`: Media resolution has not been set.
  - `MEDIA_RESOLUTION_LOW`: Media resolution set to low.
  - `MEDIA_RESOLUTION_MEDIUM`: Media resolution set to medium.
  - `MEDIA_RESOLUTION_HIGH`: Media resolution set to high.
  - `MEDIA_RESOLUTION_ULTRA_HIGH`: Media resolution set to ultra high.

  {% /details %}

  {% /details %}

  {% /details %}

  {% details %}
  {% summary %}
  **mediaProcessing**: `string enum`

  Optional. How the model processes this part's media for understanding. Only meaningful for video parts (`inline_data` or `file_data` with video mime). Non-video parts ignore this field.

  {% /summary %}

  Possible values:

  - `MEDIA_PROCESSING_UNSPECIFIED`: Defaults to model-specific processing.
  - `STATIC`: Fixed-rate frame extraction. All frames placed in context.
  - `AGENTIC`: Model-driven dynamic navigation. Recommended for most use cases.

  {% /details %}

  {% /details %}

  **role**: `string`

  Optional. The producer of the content. Must be either 'user' or 'model'. Useful to set for multi-turn conversations, otherwise can be left blank or unset.

  {% /details %}

  {% details %}
  {% summary %}
  **tools**: `Tool array`

  Optional. A list of `Tools` the `Model` may use to generate the next response. A `Tool` is a piece of code that enables the system to interact with external systems to perform an action, or set of actions, outside of knowledge and scope of the `Model`. Supported `Tool`s are `Function` and `code_execution`. Refer to the [Function calling](/docs/function-calling) and the [Code execution](/docs/code-execution) guides to learn more.

  {% /summary %}

  {% details %}
  {% summary %}
  **functionDeclarations**: `FunctionDeclaration array`

  Optional. A list of `FunctionDeclarations` available to the model that can be used for function calling. The model or system does not execute the function. Instead the defined function may be returned as a FunctionCall with arguments to the client side for execution. The model may decide to call a subset of these functions by populating FunctionCall in the response. The next conversation turn may contain a FunctionResponse with the Content.role "function" generation context for the next model turn.

  {% /summary %}

  **name**: {% mark %}required{% /mark %} `string`

  The name of the function. Must be a-z, A-Z, 0-9, or contain underscores, colons, dots, and dashes, with a maximum length of 128.

  **description**: {% mark %}required{% /mark %} `string`

  A brief description of the function.

  {% details %}
  {% summary %}
  **parameters**: `Schema`

  Optional. Describes the parameters to this function. Reflects the Open API 3.03 Parameter Object string Key: the name of the parameter. Parameter names are case sensitive. Schema Value: the Schema defining the type used for the parameter.

  {% /summary %}

  {% details %}
  {% summary %}
  **type**: {% mark %}required{% /mark %} `Type`

  Data type.

  {% /summary %}

  Possible values:

  - `TYPE_UNSPECIFIED`: Not specified, should not be used.
  - `STRING`: String type.
  - `NUMBER`: Number type.
  - `INTEGER`: Integer type.
  - `BOOLEAN`: Boolean type.
  - `ARRAY`: Array type.
  - `OBJECT`: Object type.
  - `NULL`: Null type.

  {% /details %}

  **format**: `string`

  Optional. The format of the data. Any value is allowed, but most do not trigger any special functionality.

  **title**: `string`

  Optional. The title of the schema.

  **description**: `string`

  Optional. A brief description of the parameter. This could contain examples of use. Parameter description may be formatted as Markdown.

  **nullable**: `boolean`

  Optional. Indicates if the value may be null.

  **enum**: `string array`

  Optional. Possible values of the element of Type.STRING with enum format. For example we can define an Enum Direction as : {type:STRING, format:enum, enum:["EAST", NORTH", "SOUTH", "WEST"]}

  **items**: `Schema`

  Optional. Schema of the elements of Type.ARRAY.

  **maxItems**: `string`

  Optional. Maximum number of the elements for Type.ARRAY.

  **minItems**: `string`

  Optional. Minimum number of the elements for Type.ARRAY.

  **properties**: `object`

  Optional. Properties of Type.OBJECT.

  **required**: `string array`

  Optional. Required properties of Type.OBJECT.

  **minProperties**: `string`

  Optional. Minimum number of the properties for Type.OBJECT.

  **maxProperties**: `string`

  Optional. Maximum number of the properties for Type.OBJECT.

  **minimum**: `number`

  Optional. SCHEMA FIELDS FOR TYPE INTEGER and NUMBER Minimum value of the Type.INTEGER and Type.NUMBER

  **maximum**: `number`

  Optional. Maximum value of the Type.INTEGER and Type.NUMBER

  **minLength**: `string`

  Optional. SCHEMA FIELDS FOR TYPE STRING Minimum length of the Type.STRING

  **maxLength**: `string`

  Optional. Maximum length of the Type.STRING

  **pattern**: `string`

  Optional. Pattern of the Type.STRING to restrict a string to a regular expression.

  **example**: `object`

  Optional. Example of the object. Will only populated when the object is the root.

  **anyOf**: `Schema array`

  Optional. The value should be validated against any (one or more) of the subschemas in the list.

  **propertyOrdering**: `string array`

  Optional. The order of the properties. Not a standard field in open api spec. Used to determine the order of the properties in the response.

  **default**: `object`

  Optional. Default value of the field. Per JSON Schema, this field is intended for documentation generators and doesn't affect validation. Thus it's included here and ignored so that developers who send schemas with a `default` field don't get unknown-field errors.

  {% /details %}

  **parametersJsonSchema**: `object`

  Optional. Describes the parameters to the function in JSON Schema format. The schema must describe an object where the properties are the parameters to the function. For example: `{ "type": "object", "properties": { "name": { "type": "string" }, "age": { "type": "integer" } }, "additionalProperties": false, "required": ["name", "age"], "propertyOrdering": ["name", "age"] }` This field is mutually exclusive with `parameters`.

  {% details %}
  {% summary %}
  **response**: `Schema`

  Optional. Describes the output from this function in JSON Schema format. Reflects the Open API 3.03 Response Object. The Schema defines the type used for the response value of the function.

  {% /summary %}

  {% details %}
  {% summary %}
  **type**: {% mark %}required{% /mark %} `Type`

  Data type.

  {% /summary %}

  Possible values:

  - `TYPE_UNSPECIFIED`: Not specified, should not be used.
  - `STRING`: String type.
  - `NUMBER`: Number type.
  - `INTEGER`: Integer type.
  - `BOOLEAN`: Boolean type.
  - `ARRAY`: Array type.
  - `OBJECT`: Object type.
  - `NULL`: Null type.

  {% /details %}

  **format**: `string`

  Optional. The format of the data. Any value is allowed, but most do not trigger any special functionality.

  **title**: `string`

  Optional. The title of the schema.

  **description**: `string`

  Optional. A brief description of the parameter. This could contain examples of use. Parameter description may be formatted as Markdown.

  **nullable**: `boolean`

  Optional. Indicates if the value may be null.

  **enum**: `string array`

  Optional. Possible values of the element of Type.STRING with enum format. For example we can define an Enum Direction as : {type:STRING, format:enum, enum:["EAST", NORTH", "SOUTH", "WEST"]}

  **items**: `Schema`

  Optional. Schema of the elements of Type.ARRAY.

  **maxItems**: `string`

  Optional. Maximum number of the elements for Type.ARRAY.

  **minItems**: `string`

  Optional. Minimum number of the elements for Type.ARRAY.

  **properties**: `object`

  Optional. Properties of Type.OBJECT.

  **required**: `string array`

  Optional. Required properties of Type.OBJECT.

  **minProperties**: `string`

  Optional. Minimum number of the properties for Type.OBJECT.

  **maxProperties**: `string`

  Optional. Maximum number of the properties for Type.OBJECT.

  **minimum**: `number`

  Optional. SCHEMA FIELDS FOR TYPE INTEGER and NUMBER Minimum value of the Type.INTEGER and Type.NUMBER

  **maximum**: `number`

  Optional. Maximum value of the Type.INTEGER and Type.NUMBER

  **minLength**: `string`

  Optional. SCHEMA FIELDS FOR TYPE STRING Minimum length of the Type.STRING

  **maxLength**: `string`

  Optional. Maximum length of the Type.STRING

  **pattern**: `string`

  Optional. Pattern of the Type.STRING to restrict a string to a regular expression.

  **example**: `object`

  Optional. Example of the object. Will only populated when the object is the root.

  **anyOf**: `Schema array`

  Optional. The value should be validated against any (one or more) of the subschemas in the list.

  **propertyOrdering**: `string array`

  Optional. The order of the properties. Not a standard field in open api spec. Used to determine the order of the properties in the response.

  **default**: `object`

  Optional. Default value of the field. Per JSON Schema, this field is intended for documentation generators and doesn't affect validation. Thus it's included here and ignored so that developers who send schemas with a `default` field don't get unknown-field errors.

  {% /details %}

  **responseJsonSchema**: `object`

  Optional. Describes the output from this function in JSON Schema format. The value specified by the schema is the response value of the function. This field is mutually exclusive with `response`.

  {% /details %}

  {% details %}
  {% summary %}
  **googleSearchRetrieval**: `GoogleSearchRetrieval`

  Optional. Retrieval tool that is powered by Google search.

  {% /summary %}

  {% details %}
  {% summary %}
  **dynamicRetrievalConfig**: `DynamicRetrievalConfig`

  Specifies the dynamic retrieval configuration for the given source.

  {% /summary %}

  {% details %}
  {% summary %}
  **mode**: `string enum`

  The mode of the predictor to be used in dynamic retrieval.

  {% /summary %}

  Possible values:

  - `MODE_UNSPECIFIED`: Always trigger retrieval.
  - `MODE_DYNAMIC`: Run retrieval only when system decides it is necessary.

  {% /details %}

  **dynamicThreshold**: `number`

  The threshold to be used in dynamic retrieval. If not set, a system default value is used.

  {% /details %}

  {% /details %}

  {% details %}
  {% summary %}
  **codeExecution**: `CodeExecution`

  Optional. Enables the model to execute code as part of generation.

  {% /summary %}

  {% /details %}

  {% details %}
  {% summary %}
  **googleSearch**: `GoogleSearch`

  Optional. GoogleSearch tool type. Tool to support Google Search in Model. Powered by Google.

  {% /summary %}

  {% details %}
  {% summary %}
  **timeRangeFilter**: `Interval`

  Optional. Filter search results to a specific time range. If customers set a start time, they must set an end time (and vice versa).

  {% /summary %}

  **startTime**: `string`

  Optional. Inclusive start of the interval. If specified, a Timestamp matching this interval will have to be the same or after the start.

  **endTime**: `string`

  Optional. Exclusive end of the interval. If specified, a Timestamp matching this interval will have to be before the end.

  {% /details %}

  {% details %}
  {% summary %}
  **searchTypes**: `SearchTypes`

  Optional. The set of search types to enable. If not set, web search is enabled by default.

  {% /summary %}

  {% details %}
  {% summary %}
  **webSearch**: `WebSearch`

  Optional. Enables web search. Only text results are returned.

  {% /summary %}

  {% /details %}

  {% details %}
  {% summary %}
  **imageSearch**: `ImageSearch`

  Optional. Enables image search. Image bytes are returned.

  {% /summary %}

  {% /details %}

  {% /details %}

  {% /details %}

  {% details %}
  {% summary %}
  **computerUse**: `ComputerUse`

  Optional. Tool to support the model interacting directly with the computer. If enabled, it automatically populates computer-use specific Function Declarations.

  {% /summary %}

  {% details %}
  {% summary %}
  **environment**: {% mark %}required{% /mark %} `string enum`

  The environment being operated.

  {% /summary %}

  Possible values:

  - `ENVIRONMENT_UNSPECIFIED`: Defaults to browser.
  - `ENVIRONMENT_BROWSER`: Operates in a web browser.
  - `ENVIRONMENT_MOBILE`: Operates in a mobile environment.
  - `ENVIRONMENT_DESKTOP`: Operates in a desktop environment.

  {% /details %}

  **excludedPredefinedFunctions**: `string array`

  Optional. By default, predefined functions are included in the final model call. Some of them can be explicitly excluded from being automatically included. This can serve two purposes: 1. Using a more restricted / different action space. 2. Improving the definitions / instructions of predefined functions.

  **enablePromptInjectionDetection**: `boolean`

  Optional. Whether enable the prompt injection detection check on computer-use request.

  {% details %}
  {% summary %}
  **disabledSafetyPolicies**: `string enum array`

  Optional. Disabled safety policies for computer use.

  {% /summary %}

  Possible values:

  - `SAFETY_POLICY_UNSPECIFIED`: Unspecified safety policy.
  - `FINANCIAL_TRANSACTIONS`: Safety policy for financial transactions.
  - `SENSITIVE_DATA_MODIFICATION`: Safety policy for sensitive data modification.
  - `COMMUNICATION_TOOL`: Safety policy for communication tools (e.g. Gmail, Chat, Meet).
  - `ACCOUNT_CREATION`: Safety policy for account creation.
  - `DATA_MODIFICATION`: Safety policy for data modification.
  - `USER_CONSENT_MANAGEMENT`: Safety policy for user consent management.
  - `LEGAL_TERMS_AND_AGREEMENTS`: Safety policy for legal terms and agreements.

  {% /details %}

  {% /details %}

  {% details %}
  {% summary %}
  **urlContext**: `UrlContext`

  Optional. Tool to support URL context retrieval.

  {% /summary %}

  {% /details %}

  {% details %}
  {% summary %}
  **fileSearch**: `FileSearch`

  Optional. FileSearch tool type. Tool to retrieve knowledge from Semantic Retrieval corpora.

  {% /summary %}

  **fileSearchStoreNames**: {% mark %}required{% /mark %} `string array`

  The names of the file_search_stores to retrieve from. Example: `fileSearchStores/my-file-search-store-123`

  **topK**: `integer`

  Optional. The number of semantic retrieval chunks to retrieve.

  **metadataFilter**: `string`

  Optional. Metadata filter to apply to the semantic retrieval documents and chunks.

  {% /details %}

  {% details %}
  {% summary %}
  **mcpServers**: `McpServer array`

  Optional. MCP Servers to connect to.

  {% /summary %}

  {% details %}
  {% summary %}
  **transport**: `Union type`

  The transport to use to connect to the MCPServer.

  {% /summary %}

  Union field `transport` can be only one of the following:

  {% details %}
  {% summary %}
  **streamableHttpTransport**: `StreamableHttpTransport`

  A transport that can stream HTTP requests and responses.

  {% /summary %}

  **url**: `string`

  The full URL for the MCPServer endpoint. Example: "https://api.example.com/mcp"

  **headers**: `object`

  Optional: Fields for authentication headers, timeouts, etc., if needed.

  **timeout**: `string`

  HTTP timeout for regular operations.

  **sseReadTimeout**: `string`

  Timeout for SSE read operations.

  **terminateOnClose**: `boolean`

  Whether to close the client session when the transport closes.

  {% /details %}

  {% /details %}

  **name**: `string`

  The name of the MCPServer.

  {% /details %}

  {% details %}
  {% summary %}
  **googleMaps**: `GoogleMaps`

  Optional. Tool that allows grounding the model's response with geospatial context related to the user's query.

  {% /summary %}

  **enableWidget**: `boolean`

  Optional. Whether to return a widget context token in the GroundingMetadata of the response. Developers can use the widget context token to render a Google Maps widget with geospatial context related to the places that the model references in the response.

  {% /details %}

  {% /details %}

  {% details %}
  {% summary %}
  **toolConfig**: `ToolConfig`

  Optional. Tool configuration for any `Tool` specified in the request. Refer to the [Function calling guide](/docs/function-calling#function_calling_modes) for a usage example.

  {% /summary %}

  {% details %}
  {% summary %}
  **functionCallingConfig**: `FunctionCallingConfig`

  Optional. Function calling config.

  {% /summary %}

  {% details %}
  {% summary %}
  **mode**: `string enum`

  Optional. Specifies the mode in which function calling should execute. If unspecified, the default value will be set to AUTO.

  {% /summary %}

  Possible values:

  - `MODE_UNSPECIFIED`: Unspecified function calling mode. This value should not be used.
  - `AUTO`: Default model behavior, model decides to predict either a function call or a natural language response.
  - `ANY`: Model is constrained to always predicting a function call only. If "allowed_function_names" are set, the predicted function call will be limited to any one of "allowed_function_names", else the predicted function call will be any one of the provided "function_declarations".
  - `NONE`: Model will not predict any function call. Model behavior is same as when not passing any function declarations.
  - `VALIDATED`: Model decides to predict either a function call or a natural language response, but will validate function calls with constrained decoding. If "allowed_function_names" are set, the predicted function call will be limited to any one of "allowed_function_names", else the predicted function call will be any one of the provided "function_declarations".

  {% /details %}

  **allowedFunctionNames**: `string array`

  Optional. A set of function names that, when provided, limits the functions the model will call. This should only be set when the Mode is ANY or VALIDATED. Function names should match [FunctionDeclaration.name]. When set, model will predict a function call from only allowed function names.

  {% /details %}

  {% details %}
  {% summary %}
  **retrievalConfig**: `RetrievalConfig`

  Optional. Retrieval config.

  {% /summary %}

  {% details %}
  {% summary %}
  **latLng**: `LatLng`

  Optional. The location of the user.

  {% /summary %}

  **latitude**: `number`

  The latitude in degrees. It must be in the range [-90.0, +90.0].

  **longitude**: `number`

  The longitude in degrees. It must be in the range [-180.0, +180.0].

  {% /details %}

  **languageCode**: `string`

  Optional. The language code of the user. Language code for content. Use language tags defined by [BCP47](https://www.rfc-editor.org/rfc/bcp/bcp47.txt).

  {% /details %}

  **includeServerSideToolInvocations**: `boolean`

  Optional. If true, the API response will include the server-side tool calls and responses within the `Content` message. This allows clients to observe the server's tool interactions.

  {% /details %}

  {% details %}
  {% summary %}
  **safetySettings**: `SafetySetting array`

  Optional. A list of unique `SafetySetting` instances for blocking unsafe content. This will be enforced on the `GenerateContentRequest.contents` and `GenerateContentResponse.candidates`. There should not be more than one setting for each `SafetyCategory` type. The API will block any contents and responses that fail to meet the thresholds set by these settings. This list overrides the default settings for each `SafetyCategory` specified in the safety_settings. If there is no `SafetySetting` for a given `SafetyCategory` provided in the list, the API will use the default safety setting for that category. Harm categories HARM_CATEGORY_HATE_SPEECH, HARM_CATEGORY_SEXUALLY_EXPLICIT, HARM_CATEGORY_DANGEROUS_CONTENT, HARM_CATEGORY_HARASSMENT, HARM_CATEGORY_CIVIC_INTEGRITY, HARM_CATEGORY_JAILBREAK are supported. Refer to the [guide](/docs/safety-settings) for detailed information on available safety settings. Also refer to the [Safety guidance](/docs/safety-guidance) to learn how to incorporate safety considerations in your AI applications.

  {% /summary %}

  {% details %}
  {% summary %}
  **category**: {% mark %}required{% /mark %} `HarmCategory`

  The category for this setting.

  {% /summary %}

  Possible values:

  - `HARM_CATEGORY_UNSPECIFIED`: Category is unspecified.
  - `HARM_CATEGORY_HARASSMENT`: **Gemini** - Harassment content.
  - `HARM_CATEGORY_HATE_SPEECH`: **Gemini** - Hate speech and content.
  - `HARM_CATEGORY_SEXUALLY_EXPLICIT`: **Gemini** - Sexually explicit content.
  - `HARM_CATEGORY_DANGEROUS_CONTENT`: **Gemini** - Dangerous content.
  - `HARM_CATEGORY_JAILBREAK`: **Gemini** - Prompts attempting to bypass or subvert the model's safety guidelines (jailbreak attempts).

  {% /details %}

  {% details %}
  {% summary %}
  **threshold**: {% mark %}required{% /mark %} `string enum`

  Controls the probability threshold at which harm is blocked.

  {% /summary %}

  Possible values:

  - `HARM_BLOCK_THRESHOLD_UNSPECIFIED`: Threshold is unspecified.
  - `BLOCK_LOW_AND_ABOVE`: Content with NEGLIGIBLE will be allowed.
  - `BLOCK_MEDIUM_AND_ABOVE`: Content with NEGLIGIBLE and LOW will be allowed.
  - `BLOCK_ONLY_HIGH`: Content with NEGLIGIBLE, LOW, and MEDIUM will be allowed.
  - `BLOCK_NONE`: All content will be allowed.
  - `OFF`: Turn off the safety filter.

  {% /details %}

  {% /details %}

  {% details %}
  {% summary %}
  **generationConfig**: `GenerationConfig`

  Optional. Configuration options for model generation and outputs.

  {% /summary %}

  **candidateCount**: `integer`

  Optional. Number of generated responses to return. If unset, this will default to 1. Please note that this doesn't work for previous generation models (Gemini 1.0 family)

  **stopSequences**: `string array`

  Optional. The set of character sequences (up to 5) that will stop output generation. If specified, the API will stop at the first appearance of a `stop_sequence`. The stop sequence will not be included as part of the response.

  **maxOutputTokens**: `integer`

  Optional. The maximum number of tokens to include in a response candidate. Note: The default value varies by model, see the `Model.output_token_limit` attribute of the `Model` returned from the `getModel` function.

  **temperature**: `number`

  Optional. Controls the randomness of the output. Note: The default value varies by model, see the `Model.temperature` attribute of the `Model` returned from the `getModel` function. Values can range from [0.0, 2.0]. This field is no longer supported and is silently ignored on Gemini 3.6 Flash and newer models.

  **topP**: `number`

  Optional. The maximum cumulative probability of tokens to consider when sampling. The model uses combined Top-k and Top-p (nucleus) sampling. Tokens are sorted based on their assigned probabilities so that only the most likely tokens are considered. Top-k sampling directly limits the maximum number of tokens to consider, while Nucleus sampling limits the number of tokens based on the cumulative probability. Note: The default value varies by `Model` and is specified by the`Model.top_p` attribute returned from the `getModel` function. An empty `top_k` attribute indicates that the model doesn't apply top-k sampling and doesn't allow setting `top_k` on requests. This field is no longer supported and is silently ignored on Gemini 3.6 Flash and newer models.

  **topK**: `integer`

  Optional. The maximum number of tokens to consider when sampling. Gemini models use Top-p (nucleus) sampling or a combination of Top-k and nucleus sampling. Top-k sampling considers the set of `top_k` most probable tokens. Models running with nucleus sampling don't allow top_k setting. Note: The default value varies by `Model` and is specified by the`Model.top_p` attribute returned from the `getModel` function. An empty `top_k` attribute indicates that the model doesn't apply top-k sampling and doesn't allow setting `top_k` on requests. This field is no longer supported and is silently ignored on Gemini 3.6 Flash and newer models.

  **seed**: `integer`

  Optional. Seed used in decoding. If not set, the request uses a randomly generated seed.

  **responseMimeType**: `string`

  Optional. MIME type of the generated candidate text. Supported MIME types are: `text/plain`: (default) Text output. `application/json`: JSON response in the response candidates. `text/x.enum`: ENUM as a string response in the response candidates. Refer to the [docs](/docs/file-input-methods#text-file-types) for a list of all supported text MIME types.

  **responseJsonSchema**: `object`

  Optional. An internal detail. Use `responseJsonSchema` rather than this field.

  **presencePenalty**: `number`

  Optional. Presence penalty applied to the next token's logprobs if the token has already been seen in the response. This penalty is binary on/off and not dependant on the number of times the token is used (after the first). Use frequency_penalty for a penalty that increases with each use. A positive penalty will discourage the use of tokens that have already been used in the response, increasing the vocabulary. A negative penalty will encourage the use of tokens that have already been used in the response, decreasing the vocabulary.

  **frequencyPenalty**: `number`

  Optional. Frequency penalty applied to the next token's logprobs, multiplied by the number of times each token has been seen in the respponse so far. A positive penalty will discourage the use of tokens that have already been used, proportional to the number of times the token has been used: The more a token is used, the more difficult it is for the model to use that token again increasing the vocabulary of responses. Caution: A *negative* penalty will encourage the model to reuse tokens proportional to the number of times the token has been used. Small negative values will reduce the vocabulary of a response. Larger negative values will cause the model to start repeating a common token until it hits the max_output_tokens limit.

  **responseLogprobs**: `boolean`

  Optional. If true, export the logprobs results in response.

  **logprobs**: `integer`

  Optional. Only valid if response_logprobs=True. This sets the number of top logprobs, including the chosen candidate, to return at each decoding step in the Candidate.logprobs_result. The number must be in the range of [0, 20].

  **enableEnhancedCivicAnswers**: `boolean`

  Optional. Enables enhanced civic answers. It may not be available for all models.

  {% details %}
  {% summary %}
  **responseModalities**: `string enum array`

  Optional. The requested modalities of the response. Represents the set of modalities that the model can return, and should be expected in the response. This is an exact match to the modalities of the response. A model may have multiple combinations of supported modalities. If the requested modalities do not match any of the supported combinations, an error will be returned. An empty list is equivalent to requesting only text.

  {% /summary %}

  Possible values:

  - `MODALITY_UNSPECIFIED`: Default value.
  - `TEXT`: Indicates the model should return text.
  - `IMAGE`: Indicates the model should return images.
  - `AUDIO`: Indicates the model should return audio.

  {% /details %}

  {% details %}
  {% summary %}
  **thinkingConfig**: `ThinkingConfig`

  Optional. Config for thinking features. An error will be returned if this field is set for models that don't support thinking.

  {% /summary %}

  **includeThoughts**: `boolean`

  Indicates whether to include thoughts in the response. If true, thoughts are returned only when available.

  **thinkingBudget**: `integer`

  The number of thoughts tokens that the model should generate.

  {% details %}
  {% summary %}
  **thinkingLevel**: `string enum`

  Optional. Controls the maximum depth of the model's internal reasoning process before it produces a response. The default value is model-dependent. Refer to the [Thinking levels guide](/docs/thinking#thinking-levels) for more details. Recommended for Gemini 3 or later models. Use with earlier models results in an error.

  {% /summary %}

  Possible values:

  - `THINKING_LEVEL_UNSPECIFIED`: Default value.
  - `MINIMAL`: Little to no thinking.
  - `LOW`: Low thinking level.
  - `MEDIUM`: Medium thinking level.
  - `HIGH`: High thinking level.
  - `XHIGH`: XHigh thinking level.

  {% /details %}

  {% /details %}

  {% details %}
  {% summary %}
  **imageConfig**: `ImageConfig`

  Optional. Config for image generation. An error will be returned if this field is set for models that don't support these config options.

  {% /summary %}

  **aspectRatio**: `string`

  Optional. The aspect ratio of the image to generate. Supported aspect ratios: `1:1`, `1:4`, `4:1`, `1:8`, `8:1`, `2:3`, `3:2`, `3:4`, `4:3`, `4:5`, `5:4`, `9:16`, `16:9`, or `21:9`. If not specified, the model will choose a default aspect ratio based on any reference images provided.

  **imageSize**: `string`

  Optional. Specifies the size of generated images. Supported values are `512`, `1K`, `2K`, `4K`. If not specified, the model will use default value `1K`.

  {% /details %}

  {% details %}
  {% summary %}
  **mediaResolution**: `string enum`

  Optional. If specified, the media resolution specified will be used.

  {% /summary %}

  Possible values:

  - `MEDIA_RESOLUTION_UNSPECIFIED`: Media resolution has not been set.
  - `MEDIA_RESOLUTION_LOW`: Media resolution set to low (64 tokens).
  - `MEDIA_RESOLUTION_MEDIUM`: Media resolution set to medium (256 tokens).
  - `MEDIA_RESOLUTION_HIGH`: Media resolution set to high (zoomed reframing with 256 tokens).

  {% /details %}

  {% details %}
  {% summary %}
  **responseFormat**: `ResponseFormatConfig`

  Optional. Configuration for the response output format. Allows specifying output configuration per modality (text, audio, image) in a flat structure.

  {% /summary %}

  {% details %}
  {% summary %}
  **text**: `TextResponseFormat`

  Optional. Text output format configuration.

  {% /summary %}

  {% details %}
  {% summary %}
  **mimeType**: `string enum`

  Optional. The MIME type of the text output.

  {% /summary %}

  Possible values:

  - `MIME_TYPE_UNSPECIFIED`: Default value. This value is unused.
  - `APPLICATION_JSON`: JSON output format.
  - `TEXT_PLAIN`: Plain text output format.

  {% /details %}

  **schema**: `object`

  Optional. The JSON schema that the output should conform to. Only applicable when mime_type is APPLICATION_JSON.

  {% /details %}

  {% details %}
  {% summary %}
  **audio**: `AudioResponseFormat`

  Optional. Audio output format configuration.

  {% /summary %}

  {% details %}
  {% summary %}
  **mimeType**: `string enum`

  Optional. The MIME type of the audio output.

  {% /summary %}

  Possible values:

  - `MIME_TYPE_UNSPECIFIED`: Default value. This value is unused.
  - `AUDIO_MP3`: MP3 audio format.
  - `AUDIO_OGG_OPUS`: OGG Opus audio format.
  - `AUDIO_L16`: Raw PCM (L16) audio format.
  - `AUDIO_WAV`: WAV audio format.
  - `AUDIO_ALAW`: A-law audio format.
  - `AUDIO_MULAW`: Mu-law audio format.

  {% /details %}

  {% details %}
  {% summary %}
  **delivery**: `string enum`

  Optional. The delivery mode for the audio output.

  {% /summary %}

  Possible values:

  - `DELIVERY_UNSPECIFIED`: Default value. This value is unused.
  - `INLINE`: Audio data is returned inline in the response.
  - `URI`: Audio data is returned as a URI.

  {% /details %}

  **sampleRate**: `integer`

  Optional. Sample rate in Hz.

  **bitRate**: `integer`

  Optional. Bit rate in bits per second (bps). Only applicable for compressed formats (MP3, Opus).

  {% /details %}

  {% details %}
  {% summary %}
  **image**: `ImageResponseFormat`

  Optional. Image output format configuration.

  {% /summary %}

  {% details %}
  {% summary %}
  **mimeType**: `string enum`

  Optional. The MIME type of the image output.

  {% /summary %}

  Possible values:

  - `MIME_TYPE_UNSPECIFIED`: Default value. This value is unused.
  - `IMAGE_JPEG`: JPEG image format.

  {% /details %}

  {% details %}
  {% summary %}
  **delivery**: `string enum`

  Optional. The delivery mode for the image output.

  {% /summary %}

  Possible values:

  - `DELIVERY_UNSPECIFIED`: Default value. This value is unused.
  - `INLINE`: Image data is returned inline in the response.
  - `URI`: Image data is returned as a URI.

  {% /details %}

  {% details %}
  {% summary %}
  **aspectRatio**: `string enum`

  Optional. The aspect ratio for the image output.

  {% /summary %}

  Possible values:

  - `ASPECT_RATIO_UNSPECIFIED`: Default value. This value is unused.
  - `ASPECT_RATIO_ONE_BY_ONE`: 1:1 aspect ratio.
  - `ASPECT_RATIO_TWO_BY_THREE`: 2:3 aspect ratio.
  - `ASPECT_RATIO_THREE_BY_TWO`: 3:2 aspect ratio.
  - `ASPECT_RATIO_THREE_BY_FOUR`: 3:4 aspect ratio.
  - `ASPECT_RATIO_FOUR_BY_THREE`: 4:3 aspect ratio.
  - `ASPECT_RATIO_FOUR_BY_FIVE`: 4:5 aspect ratio.
  - `ASPECT_RATIO_FIVE_BY_FOUR`: 5:4 aspect ratio.
  - `ASPECT_RATIO_NINE_BY_SIXTEEN`: 9:16 aspect ratio.
  - `ASPECT_RATIO_SIXTEEN_BY_NINE`: 16:9 aspect ratio.
  - `ASPECT_RATIO_TWENTY_ONE_BY_NINE`: 21:9 aspect ratio.
  - `ASPECT_RATIO_ONE_BY_EIGHT`: 1:8 aspect ratio.
  - `ASPECT_RATIO_EIGHT_BY_ONE`: 8:1 aspect ratio.
  - `ASPECT_RATIO_ONE_BY_FOUR`: 1:4 aspect ratio.
  - `ASPECT_RATIO_FOUR_BY_ONE`: 4:1 aspect ratio.

  {% /details %}

  {% details %}
  {% summary %}
  **imageSize**: `string enum`

  Optional. The size of the image output.

  {% /summary %}

  Possible values:

  - `IMAGE_SIZE_UNSPECIFIED`: Default value. This value is unused.
  - `IMAGE_SIZE_FIVE_TWELVE`: 512px image size.
  - `IMAGE_SIZE_ONE_K`: 1K image size.
  - `IMAGE_SIZE_TWO_K`: 2K image size.
  - `IMAGE_SIZE_FOUR_K`: 4K image size.

  {% /details %}

  {% /details %}

  {% /details %}

  {% /details %}

  {% details %}
  {% summary %}
  **serviceTier**: `ServiceTier`

  Optional. The service tier of the request.

  {% /summary %}

  Possible values:

  - `unspecified`: Default service tier, which is standard.
  - `standard`: Standard service tier.
  - `flex`: Flex service tier.
  - `priority`: Priority service tier.

  {% /details %}

  **store**: `boolean`

  Optional. Configures the logging behavior for a given request. If set, it takes precedence over the project-level logging config.

  **continuationToken**: `string`

  Optional. An opaque continuation token used to resume generation from a previous `GenerateContent` or `StreamGenerateContent` response that stopped with `finish_reason` set to `CONTINUATION`. Note that `contents` is mandatory and must identically replicate the prompt and conversation history from the initial request when continuation token is provided in a request.

  {% /details %}

  {% /section %}

  {% /left-column %}

  {% right-column class="sticky" %}
  **Example request** {% class="example-heading" %}

  {% tabs %}
  {% tab label="Text" %}
  {% tabs %}
  {% tab label="REST" %}
  ```sh
  curl https://generativelanguage.googleapis.com/v1/models/gemini-3.8-flash:countTokens?key=$GEMINI_API_KEY \
      -H 'Content-Type: application/json' \
      -X POST \
      -d '{
        "contents": [{
          "parts":[{
            "text": "The quick brown fox jumps over the lazy dog."
            }],
          }],
        }'
  ```

  {% /tab %}

  {% tab label="Python" %}
  ```python
  from google import genai
  
  client = genai.Client()
  prompt = "The quick brown fox jumps over the lazy dog."
  
  # Count tokens using the new client method.
  total_tokens = client.models.count_tokens(
      model="gemini-3.8-flash", contents=prompt
  )
  print("total_tokens: ", total_tokens)
  # ( e.g., total_tokens: 10 )
  
  response = client.models.generate_content(
      model="gemini-3.8-flash", contents=prompt
  )
  
  # The usage_metadata provides detailed token counts.
  print(response.usage_metadata)
  # ( e.g., prompt_token_count: 11, candidates_token_count: 73, total_token_count: 84 )
  ```

  {% /tab %}

  {% tab label="JavaScript" %}
  ```javascript
  // Make sure to include the following import:
  // import {GoogleGenAI} from '@google/genai';
  const ai = new GoogleGenAI({apiKey: process.env.GEMINI_API_KEY});
  const prompt = 'The quick brown fox jumps over the lazy dog.';
  const countTokensResponse = await ai.models.countTokens({
    model: 'gemini-3.8-flash',
    contents: prompt,
  });
  console.log(countTokensResponse.totalTokens);
  
  const generateResponse = await ai.models.generateContent({
    model: 'gemini-3.8-flash',
    contents: prompt,
  });
  console.log(generateResponse.usageMetadata);
  ```

  {% /tab %}

  {% tab label="Go" %}
  ```go
  ctx := context.Background()
  client, err := genai.NewClient(ctx, &genai.ClientConfig{
      APIKey:  os.Getenv("GEMINI_API_KEY"),
      Backend: genai.BackendGeminiAPI,
  })
  if err != nil {
      log.Fatal(err)
  }
  prompt := "The quick brown fox jumps over the lazy dog."
  
  // Convert prompt to a slice of *genai.Content using the helper.
  contents := []*genai.Content{
      genai.NewContentFromText(prompt, genai.RoleUser),
  }
  countResp, err := client.Models.CountTokens(ctx, "gemini-3.8-flash", contents, nil)
  if err != nil {
      return err
  }
  fmt.Println("total_tokens:", countResp.TotalTokens)
  
  response, err := client.Models.GenerateContent(ctx, "gemini-3.8-flash", contents, nil)
  if err != nil {
      log.Fatal(err)
  }
  usageMetadata, err := json.MarshalIndent(response.UsageMetadata, "", "  ")
  if err != nil {
      log.Fatal(err)
  }
  fmt.Println(string(usageMetadata))
  ```

  {% /tab %}

  {% /tabs %}

  {% /tab %}

  {% tab label="Chat" %}
  {% tabs %}
  {% tab label="REST" %}
  ```sh
  curl https://generativelanguage.googleapis.com/v1/models/gemini-3.8-flash:countTokens?key=$GEMINI_API_KEY \
      -H 'Content-Type: application/json' \
      -X POST \
      -d '{
        "contents": [
          {"role": "user",
          "parts": [{"text": "Hi, my name is Bob."}],
          },
          {"role": "model",
           "parts":[{"text": "Hi Bob"}],
          },
        ],
        }'
  ```

  {% /tab %}

  {% tab label="Python" %}
  ```python
  from google import genai
  from google.genai import types
  
  client = genai.Client()
  
  chat = client.chats.create(
      model="gemini-3.8-flash",
      history=[
          types.Content(
              role="user", parts=[types.Part(text="Hi my name is Bob")]
          ),
          types.Content(role="model", parts=[types.Part(text="Hi Bob!")]),
      ],
  )
  # Count tokens for the chat history.
  print(
      client.models.count_tokens(
          model="gemini-3.8-flash", contents=chat.get_history()
      )
  )
  # ( e.g., total_tokens: 10 )
  
  response = chat.send_message(
      message=(
          "In one sentence, explain how a computer works to a young child."
      )
  )
  print(response.usage_metadata)
  # ( e.g., prompt_token_count: 25, candidates_token_count: 21, total_token_count: 46 )
  
  # You can count tokens for the combined history and a new message.
  extra = types.UserContent(
      parts=[
          types.Part(
              text="What is the meaning of life?",
          )
      ]
  )
  history = chat.get_history()
  history.append(extra)
  print(
      client.models.count_tokens(model="gemini-3.8-flash", contents=history)
  )
  # ( e.g., total_tokens: 56 )
  ```

  {% /tab %}

  {% tab label="JavaScript" %}
  ```javascript
  // Make sure to include the following import:
  // import {GoogleGenAI} from '@google/genai';
  const ai = new GoogleGenAI({apiKey: process.env.GEMINI_API_KEY});
  // Initial chat history.
  const history = [
    {role: 'user', parts: [{text: 'Hi my name is Bob'}]},
    {role: 'model', parts: [{text: 'Hi Bob!'}]},
  ];
  const chat = ai.chats.create({
    model: 'gemini-3.8-flash',
    history: history,
  });
  
  // Count tokens for the current chat history.
  const countTokensResponse = await ai.models.countTokens({
    model: 'gemini-3.8-flash',
    contents: chat.getHistory(),
  });
  console.log(countTokensResponse.totalTokens);
  
  const chatResponse = await chat.sendMessage({
    message: 'In one sentence, explain how a computer works to a young child.',
  });
  console.log(chatResponse.usageMetadata);
  
  // Add an extra user message to the history.
  const extraMessage = {
    role: 'user',
    parts: [{text: 'What is the meaning of life?'}],
  };
  const combinedHistory = chat.getHistory();
  combinedHistory.push(extraMessage);
  const combinedCountTokensResponse = await ai.models.countTokens({
    model: 'gemini-3.8-flash',
    contents: combinedHistory,
  });
  console.log(
      'Combined history token count:',
      combinedCountTokensResponse.totalTokens,
  );
  ```

  {% /tab %}

  {% tab label="Go" %}
  ```go
  ctx := context.Background()
  client, err := genai.NewClient(ctx, &genai.ClientConfig{
      APIKey:  os.Getenv("GEMINI_API_KEY"),
      Backend: genai.BackendGeminiAPI,
  })
  if err != nil {
      log.Fatal(err)
  }
  
  // Initialize chat with some history.
  history := []*genai.Content{
      {Role: genai.RoleUser, Parts: []*genai.Part{{Text: "Hi my name is Bob"}}},
      {Role: genai.RoleModel, Parts: []*genai.Part{{Text: "Hi Bob!"}}},
  }
  chat, err := client.Chats.Create(ctx, "gemini-3.8-flash", nil, history)
  if err != nil {
      log.Fatal(err)
  }
  
  firstTokenResp, err := client.Models.CountTokens(ctx, "gemini-3.8-flash", chat.History(false), nil)
  if err != nil {
      log.Fatal(err)
  }
  fmt.Println(firstTokenResp.TotalTokens)
  
  resp, err := chat.SendMessage(ctx, genai.Part{
      Text: "In one sentence, explain how a computer works to a young child."},
  )
  if err != nil {
      log.Fatal(err)
  }
  fmt.Printf("%#v\n", resp.UsageMetadata)
  
  // Append an extra user message and recount.
  extra := genai.NewContentFromText("What is the meaning of life?", genai.RoleUser)
  hist := chat.History(false)
  hist = append(hist, extra)
  
  secondTokenResp, err := client.Models.CountTokens(ctx, "gemini-3.8-flash", hist, nil)
  if err != nil {
      log.Fatal(err)
  }
  fmt.Println(secondTokenResp.TotalTokens)
  ```

  {% /tab %}

  {% /tabs %}

  {% /tab %}

  {% tab label="Inline Media" %}
  {% tabs %}
  {% tab label="REST" %}
  ```sh
  curl "https://generativelanguage.googleapis.com/v1/models/gemini-3.8-flash:countTokens?key=$GEMINI_API_KEY" \
      -H 'Content-Type: application/json' \
      -X POST \
      -d '{
        "contents": [{
          "parts":[
              {"text": "Tell me about this instrument"},
              {
                "inline_data": {
                  "mime_type":"image/jpeg",
                  "data": "'$(base64 $B64FLAGS $IMG_PATH)'"
                }
              }
          ]
          }]
         }' 2> /dev/null
  ```

  {% /tab %}

  {% tab label="Python" %}
  ```python
  from google import genai
  import PIL.Image
  
  client = genai.Client()
  prompt = "Tell me about this image"
  your_image_file = PIL.Image.open(media / "organ.jpg")
  
  # Count tokens for combined text and inline image.
  print(
      client.models.count_tokens(
          model="gemini-3.8-flash", contents=[prompt, your_image_file]
      )
  )
  # ( e.g., total_tokens: 263 )
  
  response = client.models.generate_content(
      model="gemini-3.8-flash", contents=[prompt, your_image_file]
  )
  print(response.usage_metadata)
  # ( e.g., prompt_token_count: 264, candidates_token_count: 80, total_token_count: 345 )
  ```

  {% /tab %}

  {% tab label="JavaScript" %}
  ```javascript
  // Make sure to include the following import:
  // import {GoogleGenAI} from '@google/genai';
  const ai = new GoogleGenAI({apiKey: process.env.GEMINI_API_KEY});
  const prompt = 'Tell me about this image';
  const imageBuffer = fs.readFileSync(path.join(media, 'organ.jpg'));
  
  // Convert buffer to base64 string.
  const imageBase64 = imageBuffer.toString('base64');
  
  // Build contents using createUserContent and createPartFromBase64.
  const contents = createUserContent([
    prompt,
    createPartFromBase64(imageBase64, 'image/jpeg'),
  ]);
  
  const countTokensResponse = await ai.models.countTokens({
    model: 'gemini-3.8-flash',
    contents: contents,
  });
  console.log(countTokensResponse.totalTokens);
  
  const generateResponse = await ai.models.generateContent({
    model: 'gemini-3.8-flash',
    contents: contents,
  });
  console.log(generateResponse.usageMetadata);
  ```

  {% /tab %}

  {% /tabs %}

  {% /tab %}

  {% tab label="Video" %}
  {% tabs %}
  {% tab label="REST" %}
  ```sh
  MIME_TYPE=$(file -b --mime-type "${VIDEO_PATH}")
  NUM_BYTES=$(wc -c < "${VIDEO_PATH}")
  DISPLAY_NAME=VIDEO_PATH
  
  # Initial resumable request defining metadata.
  # The upload url is in the response headers dump them to a file.
  curl "${BASE_URL}/upload/v1/files?key=${GEMINI_API_KEY}" \
    -D upload-header.tmp \
    -H "X-Goog-Upload-Protocol: resumable" \
    -H "X-Goog-Upload-Command: start" \
    -H "X-Goog-Upload-Header-Content-Length: ${NUM_BYTES}" \
    -H "X-Goog-Upload-Header-Content-Type: ${MIME_TYPE}" \
    -H "Content-Type: application/json" \
    -d "{'file': {'display_name': '${DISPLAY_NAME}'}}" 2> /dev/null
  
  upload_url=$(grep -i "x-goog-upload-url: " "${tmp_header_file}" | cut -d" " -f2 | tr -d "\r")
  rm "${tmp_header_file}"
  
  # Upload the actual bytes.
  curl "${upload_url}" \
    -H "Content-Length: ${NUM_BYTES}" \
    -H "X-Goog-Upload-Offset: 0" \
    -H "X-Goog-Upload-Command: upload, finalize" \
    --data-binary "@${VIDEO_PATH}" 2> /dev/null > file_info.json
  
  file_uri=$(jq ".file.uri" file_info.json)
  
  state=$(jq ".file.state" file_info.json)
  
  name=$(jq ".file.name" file_info.json)
  
  while [[ "($state)" = *"PROCESSING"* ]];
  do
    echo "Processing video..."
    sleep 5
    # Get the file of interest to check state
    curl https://generativelanguage.googleapis.com/v1/files/$name > file_info.json
    state=$(jq ".file.state" file_info.json)
  done
  
  curl "https://generativelanguage.googleapis.com/v1/models/gemini-3.8-flash:countTokens?key=$GEMINI_API_KEY" \
      -H 'Content-Type: application/json' \
      -X POST \
      -d '{
        "contents": [{
          "parts":[
            {"text": "Describe this video clip"},
            {"file_data":{"mime_type": "video/mp4", "file_uri": '$file_uri'}}]
          }]
         }'
  ```

  {% /tab %}

  {% tab label="Python" %}
  ```python
  from google import genai
  import time
  
  client = genai.Client()
  prompt = "Tell me about this video"
  your_file = client.files.upload(file=media / "Big_Buck_Bunny.mp4")
  
  # Poll until the video file is completely processed (state becomes ACTIVE).
  while not your_file.state or your_file.state.name != "ACTIVE":
    print("Processing video...")
    print("File state:", your_file.state)
    time.sleep(5)
    your_file = client.files.get(name=your_file.name)
  
  print(
      client.models.count_tokens(
          model="gemini-3.8-flash", contents=[prompt, your_file]
      )
  )
  # ( e.g., total_tokens: 300 )
  
  response = client.models.generate_content(
      model="gemini-3.8-flash", contents=[prompt, your_file]
  )
  print(response.usage_metadata)
  # ( e.g., prompt_token_count: 301, candidates_token_count: 60, total_token_count: 361 )
  ```

  {% /tab %}

  {% tab label="JavaScript" %}
  ```javascript
  // Make sure to include the following import:
  // import {GoogleGenAI} from '@google/genai';
  const ai = new GoogleGenAI({apiKey: process.env.GEMINI_API_KEY});
  const prompt = 'Tell me about this video';
  let videoFile = await ai.files.upload({
    file: path.join(media, 'Big_Buck_Bunny.mp4'),
    config: {mimeType: 'video/mp4'},
  });
  
  // Poll until the video file is completely processed (state becomes ACTIVE).
  while (!videoFile.state || videoFile.state.toString() !== 'ACTIVE') {
    console.log('Processing video...');
    console.log('File state: ', videoFile.state);
    await sleep(5000);
    videoFile = await ai.files.get({name: videoFile.name});
  }
  
  const countTokensResponse = await ai.models.countTokens({
    model: 'gemini-3.8-flash',
    contents: createUserContent([
      prompt,
      createPartFromUri(videoFile.uri, videoFile.mimeType),
    ]),
  });
  console.log(countTokensResponse.totalTokens);
  
  const generateResponse = await ai.models.generateContent({
    model: 'gemini-3.8-flash',
    contents: createUserContent([
      prompt,
      createPartFromUri(videoFile.uri, videoFile.mimeType),
    ]),
  });
  console.log(generateResponse.usageMetadata);
  ```

  {% /tab %}

  {% tab label="Go" %}
  ```go
  ctx := context.Background()
  client, err := genai.NewClient(ctx, &genai.ClientConfig{
      APIKey:  os.Getenv("GEMINI_API_KEY"),
      Backend: genai.BackendGeminiAPI,
  })
  if err != nil {
      log.Fatal(err)
  }
  
  file, err := client.Files.UploadFromPath(
      ctx,
      filepath.Join(getMedia(), "Big_Buck_Bunny.mp4"),
      &genai.UploadFileConfig{
          MIMEType: "video/mp4",
      },
  )
  if err != nil {
      log.Fatal(err)
  }
  
  // Poll until the video file is completely processed (state becomes ACTIVE).
  for file.State == genai.FileStateUnspecified || file.State != genai.FileStateActive {
      fmt.Println("Processing video...")
      fmt.Println("File state:", file.State)
      time.Sleep(5 * time.Second)
  
      file, err = client.Files.Get(ctx, file.Name, nil)
      if err != nil {
          log.Fatal(err)
      }
  }
  
  parts := []*genai.Part{
      genai.NewPartFromText("Tell me about this video"),
      genai.NewPartFromURI(file.URI, file.MIMEType),
  }
  contents := []*genai.Content{
      genai.NewContentFromParts(parts, genai.RoleUser),
  }
  
  tokenResp, err := client.Models.CountTokens(ctx, "gemini-3.8-flash", contents, nil)
  if err != nil {
      log.Fatal(err)
  }
  fmt.Println("Multimodal video/audio token count:", tokenResp.TotalTokens)
  response, err := client.Models.GenerateContent(ctx, "gemini-3.8-flash", contents, nil)
  if err != nil {
      log.Fatal(err)
  }
  usageMetadata, err := json.MarshalIndent(response.UsageMetadata, "", "  ")
  if err != nil {
      log.Fatal(err)
  }
  fmt.Println(string(usageMetadata))
  ```

  {% /tab %}

  {% /tabs %}

  {% /tab %}

  {% tab label="Pdf" %}
  {% tabs %}
  {% tab label="Python" %}
  ```python
  from google import genai
  
  client = genai.Client()
  sample_pdf = client.files.upload(file=media / "test.pdf")
  token_count = client.models.count_tokens(
      model="gemini-3.8-flash",
      contents=["Give me a summary of this document.", sample_pdf],
  )
  print(f"{token_count=}")
  
  response = client.models.generate_content(
      model="gemini-3.8-flash",
      contents=["Give me a summary of this document.", sample_pdf],
  )
  print(response.usage_metadata)
  ```

  {% /tab %}

  {% /tabs %}

  {% /tab %}

  {% tab label="System Instruction" %}
  {% tabs %}
  {% tab label="Go" %}
  ```go
  ctx := context.Background()
  client, err := genai.NewClient(ctx, &genai.ClientConfig{
      APIKey:  os.Getenv("GEMINI_API_KEY"),
      Backend: genai.BackendGeminiAPI,
  })
  if err != nil {
      log.Fatal(err)
  }
  
  // Construct the user message contents.
  contents := []*genai.Content{
      genai.NewContentFromText("Good morning! How are you?", genai.RoleUser),
  }
  
  // Set the system instruction as a *genai.Content.
  config := &genai.GenerateContentConfig{
      SystemInstruction: genai.NewContentFromText("You are a cat. Your name is Neko.", genai.RoleUser),
  }
  
  response, err := client.Models.GenerateContent(ctx, "gemini-3.8-flash", contents, config)
  if err != nil {
      log.Fatal(err)
  }
  printResponse(response)
  ```

  {% /tab %}

  {% /tabs %}

  {% /tab %}

  {% /tabs %}

  {% /right-column %}

{% /two-column-layout %}

## Response  {% class="hide-from-toc" id="response" %}

{% section class="api-ref-property-list" %}
  **totalTokens**: `integer`

  The number of tokens that the `Model` tokenizes the `prompt` into. Always non-negative.

  {% hr /%}

  {% details %}
  {% summary %}
  **promptTokensDetails**: `ModalityTokenCount array`

  Output only. List of modalities that were processed in the request input.

  {% /summary %}

  {% details %}
  {% summary %}
  **modality**: `GenerativeLanguageModality`

  The modality associated with this token count.

  {% /summary %}

  Possible values:

  - `MODALITY_UNSPECIFIED`: Unspecified modality.
  - `TEXT`: Plain text.
  - `IMAGE`: Image.
  - `VIDEO`: Video.
  - `AUDIO`: Audio.
  - `DOCUMENT`: Document, e.g. PDF.

  {% /details %}

  **tokenCount**: `integer`

  Number of tokens.

  {% /details %}

  {% hr /%}

  {% details %}
  {% summary %}
  **cacheTokensDetails**: `ModalityTokenCount array`

  Output only. List of modalities that were processed in the cached content.

  {% /summary %}

  {% details %}
  {% summary %}
  **modality**: `GenerativeLanguageModality`

  The modality associated with this token count.

  {% /summary %}

  Possible values:

  - `MODALITY_UNSPECIFIED`: Unspecified modality.
  - `TEXT`: Plain text.
  - `IMAGE`: Image.
  - `VIDEO`: Video.
  - `AUDIO`: Audio.
  - `DOCUMENT`: Document, e.g. PDF.

  {% /details %}

  **tokenCount**: `integer`

  Number of tokens.

  {% /details %}

{% /section %}

