Gemini modelleri için jetonları ve faturalandırılabilir karakterleri sayma

Üretken modeller, verileri işleme için token adı verilen birimlere ayırır. Her modelin, istem ve yanıtta işleyebileceği maksimum jeton sayısı vardır.

Bu sayfada, Gemini modeline yapılan bir istek için jeton sayısının ve faturalandırılabilir karakter sayısının tahminini almak üzere Count Tokens API'nin nasıl kullanılacağı gösterilmektedir. Yanıt içindeki jeton sayısını tahmin etmek için bir API yoktur.

Count Tokens API'nin Imagen modelleri için kullanılamayacağını unutmayın.

Sayımlarda hangi bilgiler sağlanır?

Jetonları ve faturalandırılabilir karakterleri sayma hakkında aşağıdakileri unutmayın:

  • Toplam jeton sayısını sayma

    • Bu sayı, isteklerinizin izin verilen bağlam penceresini aşmamasını sağlamanıza yardımcı olur.

    • Jeton sayısı, istekte sağlanan tüm dosyaların (ör. resimler) boyutunu yansıtır. Videodaki resim veya saniye sayısını saymaz.

    • Tüm Gemini modellerinde bir jeton yaklaşık 4 karaktere eşdeğerdir. 100 jeton yaklaşık 60-80 İngilizce kelimedir.

  • Faturalandırılabilir toplam karakter sayısını sayma

    • Vertex AI için karakter sayısı fiyatlandırma hesaplamasının bir parçası olduğundan bu sayı, maliyetlerinizi anlamanıza ve kontrol etmenize yardımcı olur.

    • Faturalandırılabilir karakter sayısı, request girişi kapsamında sağlanan text içindeki karakter sayısını yansıtır.

Eski Gemini modellerinde jetonlar fiyatlandırma hesaplamasının bir parçası değildir. Ancak Gemini 2.0 ve Gemini 2.5 modellerinde fiyatlandırma hesaplamasında jetonlar kullanılır. Model başına jeton sınırları ve model başına fiyatlandırma hakkında daha fazla bilgi edinin.

Jeton ve faturalandırılabilir karakter sayımı için fiyatlandırma ve kota

CountTokens API'yi kullanmak için ücret veya kota kısıtlaması yoktur. CountTokens API'nin maksimum kotası dakikada 3.000 istektir (RPM).

Kod örnekleri

Yalnızca metin girişi

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

Çok modlu giriş

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