Anzahl der Tokens und abrechenbaren Zeichen für Gemini-Modelle zählen

Generative Modelle zerlegen Daten zur Verarbeitung in Einheiten, die als Tokens bezeichnet werden. Jedes Modell hat eine maximale Anzahl von Tokens, die es in einem Prompt und einer Antwort verarbeiten kann.

Auf dieser Seite erfahren Sie, wie Sie mit der Count Tokens API eine Schätzung der Anzahl der Tokens und der Anzahl der abrechenbaren Zeichen für eine Anfrage an ein Gemini-Modell abrufen. Es gibt keine API, um die geschätzte Anzahl von Tokens in einer Antwort abzurufen.

Die Count Tokens API kann nicht für Imagen-Modelle verwendet werden.

Welche Informationen sind in der Anzahl enthalten?

Beachten Sie Folgendes zum Zählen von Tokens und abrechenbaren Zeichen:

  • Gesamtzahl der Tokens zählen

    • Diese Anzahl ist hilfreich, um sicherzustellen, dass Ihre Anfragen das zulässige Kontextfenster nicht überschreiten.

    • Die Anzahl der Tokens entspricht der Größe aller Dateien (z. B. Bilder), die als Teil der Anfrage-Eingabe bereitgestellt werden. Die Anzahl der Bilder oder die Anzahl der Sekunden in einem Video wird nicht gezählt.

    • Bei allen Gemini-Modellen entspricht ein Token etwa vier Zeichen. 100 Tokens entsprechen etwa 60–80 Wörtern.

  • Gesamtzahl der abrechenbaren Zeichen zählen

    • Diese Anzahl ist hilfreich, um Ihre Kosten nachzuvollziehen und zu kontrollieren, da die Anzahl der Zeichen für Vertex AI Teil der Preisberechnung ist.

    • Die abrechenbare Anzahl von Zeichen entspricht der Anzahl der Zeichen im Text, der als Teil der Anfrage-Eingabe bereitgestellt wird.

Bei älteren Gemini-Modellen sind Tokens nicht Teil der Preisberechnung. Bei Gemini 2.0- und Gemini 2.5-Modellen werden Tokens jedoch in der Preisberechnung verwendet. Weitere Informationen zu Tokenlimits pro Modell und Preisen pro Modell

Preise und Kontingente für das Zählen von Tokens und abrechenbaren Zeichen

Für die Verwendung der CountTokens API fallen keine Gebühren an und es gibt keine Kontingentbeschränkungen. Das maximale Kontingent für die CountTokens API beträgt 3.000 Anfragen pro Minute.

Codebeispiele

Nur-Text-Eingabe

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}');

Einheit

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}");

Multimodale Eingabe

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}');

Einheit

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}");