टोकन को समझें और उनकी गिनती करें

Gemini और अन्य जनरेटिव एआई मॉडल, इनपुट और आउटपुट को टोकन नाम की एक यूनिट में प्रोसेस करते हैं.

Gemini मॉडल के लिए, एक टोकन का मतलब करीब चार वर्ण होता है. 100 टोकन, अंग्रेज़ी के करीब 60 से 80 शब्दों के बराबर होते हैं.

टोकन के बारे में जानकारी

टोकन, z जैसे सिंगल वर्ण या cat जैसे पूरे शब्द हो सकते हैं. लंबे शब्दों को कई टोकन में बांटा जाता है. मॉडल में इस्तेमाल किए गए सभी टोकन के सेट को शब्दावली कहा जाता है. साथ ही, टेक्स्ट को टोकन में बांटने की प्रोसेस को टोकनाइज़ेशन कहा जाता है.

बिलिंग की सुविधा चालू होने पर, Gemini API को कॉल करने की लागत का कुछ हिस्सा, इनपुट और आउटपुट टोकन की संख्या से तय होता है. इसलिए, टोकन की गिनती करने का तरीका जानना आपके लिए फ़ायदेमंद हो सकता है.

टोकन गिनें

Gemini API को दिए गए सभी इनपुट और उससे मिले सभी आउटपुट को टोकन में बदला जाता है. इनमें टेक्स्ट, इमेज फ़ाइलें, और बिना टेक्स्ट वाले अन्य मोडल शामिल हैं.

टोकन को इन तरीकों से गिना जा सकता है:

  • अनुरोध के इनपुट के साथ count_tokens को कॉल करें. यह फ़ंक्शन, सिर्फ़ इनपुट में मौजूद टोकन की कुल संख्या दिखाता है. अपने अनुरोधों का साइज़ देखने के लिए, इनपुट भेजने से पहले यह कॉल करें.

  • बातचीत के जवाब में मौजूद usage का इस्तेमाल करें. यह इनपुट (total_input_tokens), आउटपुट (total_output_tokens), सोच-विचार (total_thought_tokens), कैश मेमोरी में सेव किया गया कॉन्टेंट (total_cached_tokens), टूल का इस्तेमाल (total_tool_use_tokens), और कुल (total_tokens) के लिए टोकन की संख्या दिखाता है.

टेक्स्ट टोकन की गिनती करना

Python

# This will only work for SDK newer than 2.0.0
from google import genai

client = genai.Client()
prompt = "The quick brown fox jumps over the lazy dog."

# Count tokens before sending
total_tokens = client.models.count_tokens(
    model="gemini-3.8-flash",
    contents=prompt
)
print("total_tokens:", total_tokens.total_tokens)

# Get usage from interaction
interaction = client.interactions.create(
    model="gemini-3.8-flash",
    input=prompt
)
print(interaction.usage)

JavaScript

// This will only work for SDK newer than 2.0.0
import { GoogleGenAI } from '@google/genai';

const client = new GoogleGenAI({});
const prompt = "The quick brown fox jumps over the lazy dog.";

// Count tokens before sending
const countResponse = await client.models.countTokens({
    model: "gemini-3.8-flash",
    contents: prompt,
});
console.log(countResponse.totalTokens);

// Get usage from interaction
const interaction = await client.interactions.create({
    model: "gemini-3.8-flash",
    input: prompt,
});
console.log(interaction.usage);

Java

import com.google.genai.Client;
import com.google.genai.gaos.models.interactions.CreateModelInteraction;
import com.google.genai.gaos.models.interactions.Interaction;
import com.google.genai.gaos.models.interactions.InteractionsInput;
import com.google.genai.gaos.models.interactions.Model;
import com.google.genai.gaos.models.interactions.Usage;
import com.google.genai.gaos.models.operations.CreateInteractionRequestBody;

Client client = new Client();

CreateModelInteraction params =
    CreateModelInteraction.builder()
        .model(Model.of("gemini-3.8-flash"))
        .input(InteractionsInput.of("Calculate tokens for this message."))
        .build();

Interaction interaction =
    client.interactions.create(CreateInteractionRequestBody.of(params)).interaction().get();

if (interaction.usage().isPresent()) {
  Usage usage = interaction.usage().get();
  System.out.println("Input tokens: " + usage.totalInputTokens().orElse(0));
  System.out.println("Output tokens: " + usage.totalOutputTokens().orElse(0));
}

REST

# Specifies the API revision to avoid breaking changes when they become default
curl -X POST "https://generativelanguage.googleapis.com/v1beta/models/gemini-3.8-flash:countTokens" \
  -H "x-goog-api-key: $GEMINI_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{"contents": [{"parts": [{"text": "The quick brown fox."}]}]}'

सिलसिलेवार बातचीत के टोकन की गिनती करना

previous_interaction_id का इस्तेमाल करके, बातचीत के इतिहास में मौजूद टोकन की संख्या गिनें:

Python

# This will only work for SDK newer than 2.0.0
# First interaction
interaction1 = client.interactions.create(
    model="gemini-3.8-flash",
    input="Hi, my name is Bob"
)

# Second interaction continues the conversation
interaction2 = client.interactions.create(
    model="gemini-3.8-flash",
    input="What's my name?",
    previous_interaction_id=interaction1.id
)

# Usage includes tokens from both turns
print(f"Input tokens: {interaction2.usage.total_input_tokens}")
print(f"Output tokens: {interaction2.usage.total_output_tokens}")
print(f"Total tokens: {interaction2.usage.total_tokens}")

JavaScript

// This will only work for SDK newer than 2.0.0
// First interaction
const interaction1 = await client.interactions.create({
    model: "gemini-3.8-flash",
    input: "Hi, my name is Bob"
});

// Second interaction continues the conversation
const interaction2 = await client.interactions.create({
    model: "gemini-3.8-flash",
    input: "What's my name?",
    previous_interaction_id: interaction1.id
});

console.log(`Input tokens: ${interaction2.usage.total_input_tokens}`);
console.log(`Output tokens: ${interaction2.usage.total_output_tokens}`);

Java

import com.google.genai.Client;
import com.google.genai.gaos.models.interactions.CreateModelInteraction;
import com.google.genai.gaos.models.interactions.Interaction;
import com.google.genai.gaos.models.interactions.InteractionsInput;
import com.google.genai.gaos.models.interactions.Model;
import com.google.genai.gaos.models.interactions.Usage;
import com.google.genai.gaos.models.operations.CreateInteractionRequestBody;

Client client = new Client();

CreateModelInteraction params =
    CreateModelInteraction.builder()
        .model(Model.of("gemini-3.8-flash"))
        .input(InteractionsInput.of("Calculate tokens for this message."))
        .build();

Interaction interaction =
    client.interactions.create(CreateInteractionRequestBody.of(params)).interaction().get();

if (interaction.usage().isPresent()) {
  Usage usage = interaction.usage().get();
  System.out.println("Input tokens: " + usage.totalInputTokens().orElse(0));
  System.out.println("Output tokens: " + usage.totalOutputTokens().orElse(0));
}

मल्टीमॉडल टोकन की गिनती करना

Gemini API को दिए गए सभी इनपुट को टोकन में बदला जाता है. इनमें इमेज, वीडियो, और ऑडियो शामिल हैं. टोकनाइज़ेशन के बारे में अहम बातें:

  • इमेज: दोनों डाइमेंशन में ≤384 पिक्सल वाली इमेज को 258 टोकन के तौर पर गिना जाता है. बड़ी इमेज को 768x768 पिक्सल वाली टाइल में बांटा जाता है. हर टाइल को 258 टोकन के तौर पर गिना जाता है.
  • वीडियो: हर सेकंड 263 टोकन (यह स्टैटिक प्रोसेसिंग पर लागू होता है). एजेंटिक प्रोसेसिंग के लिए, टोकन का इस्तेमाल अलग-अलग होता है. प्रोसेसिंग मोड के हिसाब से वीडियो टोकन के इस्तेमाल की जानकारी देखें.
  • ऑडियो: हर सेकंड 32 टोकन

इमेज टोकन

Python

# This will only work for SDK newer than 2.0.0
uploaded_file = client.files.upload(file="path/to/image.jpg")

# Count tokens for image + text
total_tokens = client.models.count_tokens(
    model="gemini-3.8-flash",
    contents=["Tell me about this image", uploaded_file]
)
print(f"Total tokens: {total_tokens}")

# Generate with image
interaction = client.interactions.create(
    model="gemini-3.8-flash",
    input=[
        {"type": "text", "text": "Tell me about this image"},
        {"type": "image", "uri": uploaded_file.uri, "mime_type": uploaded_file.mime_type}
    ]
)
print(interaction.usage)

JavaScript

// This will only work for SDK newer than 2.0.0
const uploadedFile = await client.files.upload({
    file: "path/to/image.jpg",
    config: { mimeType: "image/jpeg" }
});

// Count tokens
const countResponse = await client.models.countTokens({
    model: "gemini-3.8-flash",
    contents: [
        { text: "Tell me about this image" },
        { fileData: { fileUri: uploadedFile.uri, mimeType: uploadedFile.mimeType } }
    ]
});
console.log(countResponse.totalTokens);

Java

import com.google.genai.Client;
import com.google.genai.gaos.models.interactions.CreateModelInteraction;
import com.google.genai.gaos.models.interactions.Interaction;
import com.google.genai.gaos.models.interactions.InteractionsInput;
import com.google.genai.gaos.models.interactions.Model;
import com.google.genai.gaos.models.interactions.Usage;
import com.google.genai.gaos.models.operations.CreateInteractionRequestBody;

Client client = new Client();

CreateModelInteraction params =
    CreateModelInteraction.builder()
        .model(Model.of("gemini-3.8-flash"))
        .input(InteractionsInput.of("Calculate tokens for this message."))
        .build();

Interaction interaction =
    client.interactions.create(CreateInteractionRequestBody.of(params)).interaction().get();

if (interaction.usage().isPresent()) {
  Usage usage = interaction.usage().get();
  System.out.println("Input tokens: " + usage.totalInputTokens().orElse(0));
  System.out.println("Output tokens: " + usage.totalOutputTokens().orElse(0));
}

इनलाइन डेटा का उदाहरण:

Python

# This will only work for SDK newer than 2.0.0
import base64

with open('image.jpg', 'rb') as f:
    image_bytes = f.read()

interaction = client.interactions.create(
    model="gemini-3.8-flash",
    input=[
        {"type": "text", "text": "Describe this image"},
        {
            "type": "image",
            "data": base64.b64encode(image_bytes).decode('utf-8'),
            "mime_type": "image/jpeg"
        }
    ]
)
print(interaction.usage)

वीडियो टोकन

Python

# This will only work for SDK newer than 2.0.0
import time

video_file = client.files.upload(file="path/to/video.mp4")

while not video_file.state or video_file.state.name != "ACTIVE":
    print("Processing video...")
    time.sleep(5)
    video_file = client.files.get(name=video_file.name)

# A 60-second video is approximately 100 * 60 = 6,000 tokens
total_tokens = client.models.count_tokens(
    model="gemini-3.8-flash",
    contents=["Summarize this video", video_file]
)
print(f"Total tokens: {total_tokens}")

# Generate with video
interaction = client.interactions.create(
    model="gemini-3.8-flash",
    input=[
        {"type": "text", "text": "Summarize this video"},
        {"type": "video", "uri": video_file.uri, "mime_type": video_file.mime_type}
    ]
)
print(interaction.usage)

प्रोसेसिंग मोड के हिसाब से वीडियो टोकन के इस्तेमाल की जानकारी

वीडियो के लिए टोकन का इस्तेमाल, प्रोसेसिंग मोड पर निर्भर करता है:

प्रोसेसिंग मोड टोकन की गिनती सामान्य इस्तेमाल
स्टैटिक (डिफ़ॉल्ट) डिफ़ॉल्ट रूप से, ~100 टोकन/सेकंड (कम रिज़ॉल्यूशन) या ~300 टोकन/सेकंड (ज़्यादा रिज़ॉल्यूशन). सभी फ़्रेम को 1 एफ़पीएस पर सैंपल किया गया है. वीडियो की अवधि के हिसाब से, अनुमान लगाया जा सकता है.
एजेंटिक कॉन्टेंट की जटिलता के हिसाब से अलग-अलग होता है. मॉडल, प्रॉम्प्ट का जवाब देने के लिए सिर्फ़ ज़रूरी ट्रांसक्रिप्ट और/या फ़्रेम और/या ऑडियो लोड करता है. लॉन्ग-फ़ॉर्म कॉन्टेंट के लिए, 88% तक कम टोकन.

एजेंटिक प्रोसेसिंग की मदद से, एक घंटे के लेक्चर में इस्तेमाल होने वाले टोकन की संख्या कम हो जाती है. जैसे, स्टैटिक मोड में ~10.8 लाख टोकन इस्तेमाल होते हैं, तो एजेंटिक प्रोसेसिंग में ~10.8 हज़ार टोकन इस्तेमाल हो सकते हैं. हालांकि, यह प्रॉम्प्ट और कॉन्टेंट पर निर्भर करता है.

किसी अनुरोध के लिए टोकन के असल इस्तेमाल की जांच करने के लिए, interaction.usage की जांच करें. एजेंटिक वीडियो टोकन की जानकारी इन फ़ील्ड में दी जाती है:

  • शुरुआती प्रॉम्प्ट (वीडियो रेफ़रंस + उपयोगकर्ता का प्रॉम्प्ट): total_input_tokens
  • नेविगेशन के बारे में सोचना: total_thought_tokens
  • ट्रांसक्रिप्ट, फ़्रेम, और ऑडियो को ज़रूरत के हिसाब से लोड किया जाता है: total_tool_use_tokens
  • फ़ाइनल जवाब: total_output_tokens

ऑडियो टोकन

Python

# This will only work for SDK newer than 2.0.0
audio_file = client.files.upload(file="path/to/audio.mp3")

# A 60-second audio clip is approximately 32 * 60 = 1,920 tokens
total_tokens = client.models.count_tokens(
    model="gemini-3.8-flash",
    contents=["Transcribe this audio", audio_file]
)
print(f"Total tokens: {total_tokens}")

# Generate with audio
interaction = client.interactions.create(
    model="gemini-3.8-flash",
    input=[
        {"type": "text", "text": "Transcribe this audio"},
        {"type": "audio", "uri": audio_file.uri, "mime_type": audio_file.mime_type}
    ]
)
print(interaction.usage)

सिस्टम के निर्देशों वाले टोकन की संख्या गिनना

सिस्टम के निर्देशों को इनपुट टोकन के तौर पर गिना जाता है:

Python

# This will only work for SDK newer than 2.0.0
interaction = client.interactions.create(
    model="gemini-3.8-flash",
    input="Hello!",
    system_instruction="You are a helpful assistant who speaks like a pirate."
)

# system_instruction tokens included in total_input_tokens
print(f"Input tokens: {interaction.usage.total_input_tokens}")

टूल टोकन की गिनती करना

टूल (फ़ंक्शन, कोड एक्ज़ीक्यूशन, Google Search) को भी गिना जाता है:

Python

# This will only work for SDK newer than 2.0.0
tools = [
    {
        "type": "function",
        "name": "get_weather",
        "description": "Get current weather",
        "parameters": {
            "type": "object",
            "properties": {
                "location": {"type": "string"}
            }
        }
    }
]

interaction = client.interactions.create(
    model="gemini-3.8-flash",
    input="What's the weather in Tokyo?",
    tools=tools
)

print(f"Input tokens: {interaction.usage.total_input_tokens}")
print(f"Tool use tokens: {interaction.usage.total_tool_use_tokens}")

कॉन्टेक्स्ट विंडो

हर Gemini मॉडल, ज़्यादा से ज़्यादा टोकन हैंडल कर सकता है. कॉन्टेक्स्ट विंडो से, इनपुट और आउटपुट टोकन की कुल सीमा तय होती है.

कॉन्टेक्स्ट विंडो का साइज़ प्रोग्राम के हिसाब से पाना

Python

# This will only work for SDK newer than 2.0.0
model_info = client.models.get(model="gemini-3.8-flash")
print(f"Input token limit: {model_info.input_token_limit}")
print(f"Output token limit: {model_info.output_token_limit}")

JavaScript

// This will only work for SDK newer than 2.0.0
const modelInfo = await client.models.get({ model: "gemini-3.8-flash" });
console.log(`Input token limit: ${modelInfo.inputTokenLimit}`);
console.log(`Output token limit: ${modelInfo.outputTokenLimit}`);

Java

import com.google.genai.Client;
import com.google.genai.gaos.models.interactions.CreateModelInteraction;
import com.google.genai.gaos.models.interactions.Interaction;
import com.google.genai.gaos.models.interactions.InteractionsInput;
import com.google.genai.gaos.models.interactions.Model;
import com.google.genai.gaos.models.interactions.Usage;
import com.google.genai.gaos.models.operations.CreateInteractionRequestBody;

Client client = new Client();

CreateModelInteraction params =
    CreateModelInteraction.builder()
        .model(Model.of("gemini-3.8-flash"))
        .input(InteractionsInput.of("Calculate tokens for this message."))
        .build();

Interaction interaction =
    client.interactions.create(CreateInteractionRequestBody.of(params)).interaction().get();

if (interaction.usage().isPresent()) {
  Usage usage = interaction.usage().get();
  System.out.println("Input tokens: " + usage.totalInputTokens().orElse(0));
  System.out.println("Output tokens: " + usage.totalOutputTokens().orElse(0));
}

मॉडल पेज पर, कॉन्टेक्स्ट विंडो के साइज़ देखें.

आगे क्या करना है