统计 Gemini 模型的词元数和计费字符数

生成式模型会将数据细分为单元(称为“词元”)以进行处理。每个模型具有在提示和回答中可以处理的词元数上限

本页介绍了如何使用 Count Tokens API 估算Gemini 模型发出的请求的 token 数和计费字符数。目前没有用于获取响应中词元估计数量的 API。

请注意,Count Tokens API 无法用于 Imagen 模型。

统计信息中会提供哪些信息?

请注意以下有关计算词元数和计费字符数的事项:

  • 统计总 token 数

    • 此计数有助于确保您的请求不会超出允许的上下文窗口。

    • 令牌数量将反映作为请求输入内容提供的所有文件(例如图片)的大小。它不会统计图片数量或视频中的秒数。

    • 对于所有 Gemini 模型,一个 token 相当于大约 4 个字符。100 个词元大约相当于 60-80 个英文单词。

  • 计算总计费字符数

    • 此数量有助于您了解和控制费用,因为对于 Vertex AI,字符数是价格计算的一部分。

    • 可结算的字符数将反映作为请求输入内容的一部分提供的文本中的字符数。

对于较旧的 Gemini 模型,token 参与价格计算;不过,对于 Gemini 2.0Gemini 2.5 模型,token 会参与价格计算。 详细了解各模型的令牌限制各模型的定价

计算 token 和计费字符数的价格和配额

使用 CountTokens API 无需付费或配额限制。CountTokens API 的最大配额为每分钟 3000 个请求 (RPM)。

代码示例

纯文字输入

Swift

let response = try await model.countTokens("Write a story about a magic backpack.")
print("Total Tokens: \(response.totalTokens)")
print("Total Billable Characters: \(response.totalBillableCharacters)")

Kotlin

val response = generativeModel.countTokens("Write a story about a magic backpack.")
println("Total Tokens: ${response.totalTokens}")
println("Total Billable Characters: ${response.totalBillableCharacters}")

Java

Content prompt = new Content.Builder()
        .addText("Write a story about a magic backpack.")
        .build();

GenerativeModelFutures modelFutures = GenerativeModelFutures.from(model);
ListenableFuture countTokensResponse =
        modelFutures.countTokens(prompt);

Futures.addCallback(countTokensResponse, new FutureCallback() {
    @Override
    public void onSuccess(CountTokensResponse response) {
        System.out.println("Total Tokens = " + response.getTotalTokens());
        System.out.println("Total Billable Characters: = " +
                response.getTotalBillableCharacters());
    }

    @Override
    public void onFailure(Throwable t) {
        t.printStackTrace();
    }
}, executor);

Web

const { totalTokens, totalBillableCharacters } = await model.countTokens("Write a story about a magic backpack.");
console.log(`Total tokens: ${totalTokens}, total billable characters: ${totalBillableCharacters}`);

Dart

final tokenCount = await model.countTokens(Content.text("Write a story about a magic backpack."));
print('Token count: ${tokenCount.totalTokens}, billable characters: ${tokenCount.totalBillableCharacters}');

Unity

var response = await model.CountTokensAsync("Write a story about a magic backpack.");
UnityEngine.Debug.Log($"Total Tokens: {response.TotalTokens}");
UnityEngine.Debug.Log($"Total Billable Characters: {response.TotalBillableCharacters}");

多模态输入

Swift

let response = try await model.countTokens(image, "What's in this picture?")
print("Total Tokens: \(response.totalTokens)")
print("Total Billable Characters: \(response.totalBillableCharacters)")

Kotlin

val prompt = content {
  image(bitmap)
  text("What's in this picture?")
}
val response = generativeModel.countTokens(prompt)
println("Total Tokens: ${response.totalTokens}")
println("Total Billable Characters: ${response.totalBillableCharacters}")

Java

Content prompt = new Content.Builder()
        .addImage(bitmap)
        .addText("What's in this picture?")
        .build();

GenerativeModelFutures modelFutures = GenerativeModelFutures.from(model);
ListenableFuture countTokensResponse =
        modelFutures.countTokens(prompt);

Futures.addCallback(countTokensResponse, new FutureCallback() {
    @Override
    public void onSuccess(CountTokensResponse response) {
        System.out.println("Total Tokens = " + response.getTotalTokens());
        System.out.println("Total Billable Characters: = " +
                response.getTotalBillableCharacters());
    }

    @Override
    public void onFailure(Throwable t) {
        t.printStackTrace();
    }
}, executor);

Web

const prompt = "What's in this picture?";
const imagePart = { inlineData: { mimeType: 'image/jpeg', data: imageAsBase64 }};

const { totalTokens, totalBillableCharacters } = await model.countTokens([prompt, imagePart]);
console.log(`Total tokens: ${totalTokens}, total billable characters: ${totalBillableCharacters}`);

Dart

final prompt = TextPart("What's in the picture?");
final tokenCount = await model.countTokens([
  Content.multi([prompt, imagePart])
]);
print('Token count: ${tokenCount.totalTokens}, billable characters: ${tokenCount.totalBillableCharacters}');

Unity

var response = await model.CountTokensAsync(new [] {
  ModelContent.Text("What's in this picture?"),
  ModelContent.InlineData("image/png", imageData)
});
UnityEngine.Debug.Log($"Total Tokens: {response.TotalTokens}");
UnityEngine.Debug.Log($"Total Billable Characters: {response.TotalBillableCharacters}");