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 Tokenanzahl und der Anzahl der abrechenbaren Zeichen für eine Anfrage an ein Gemini-Modell abrufen. Es gibt keine API, mit der die geschätzte Anzahl der Tokens in einer Antwort abgerufen werden kann.
Die Count Tokens API kann nicht für Imagen-Modelle verwendet werden.
Welche Informationen werden in der Zählung angegeben?
Beachten Sie Folgendes zum Zählen von Tokens und abrechenbaren Zeichen:
Anzahl der Tokens insgesamt zählen
Dieser Wert ist hilfreich, damit Ihre Anfragen das zulässige Kontextfenster nicht überschreiten.
Die Tokenanzahl entspricht der Größe aller Dateien (z. B. Bilder), die als Teil der Anfrage bereitgestellt werden. Die Anzahl der Bilder oder die Anzahl der Sekunden in einem Video werden nicht gezählt.
Bei allen Gemini-Modellen entspricht ein Token etwa vier Zeichen. 100 Tokens entsprechen etwa 60–80 englischen Wörtern.
Abrechenbare Zeichen insgesamt zählen
Diese Angabe ist hilfreich, um Ihre Kosten zu verstehen und zu kontrollieren, da die Anzahl der Zeichen bei Vertex AI Teil der Preisberechnung ist.
Die abrechenbare Zeichenanzahl 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 sie jedoch verwendet. Weitere Informationen zu Tokenlimits pro Modell und Preisen pro Modell
Preise und Kontingente für die Zählung 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 eingeben
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}");