টোকেনগুলি বুঝুন এবং গণনা করুন

জেমিনি এবং অন্যান্য জেনারেটিভ এআই মডেলগুলো টোকেন নামক একটি সূক্ষ্ম স্তরে ইনপুট ও আউটপুট প্রক্রিয়াকরণ করে।

জেমিনি মডেলের ক্ষেত্রে, একটি টোকেন প্রায় ৪টি অক্ষরের সমান। ১০০টি টোকেন প্রায় ৬০-৮০টি ইংরেজি শব্দের সমান।

টোকেন সম্পর্কে

টোকেনগুলো z মতো একক অক্ষর অথবা cat মতো সম্পূর্ণ শব্দ হতে পারে। দীর্ঘ শব্দগুলোকে কয়েকটি টোকেনে বিভক্ত করা হয়। মডেল দ্বারা ব্যবহৃত সমস্ত টোকেনের সেটকে শব্দভান্ডার বলা হয় এবং টেক্সটকে টোকেনে বিভক্ত করার প্রক্রিয়াকে টোকেনাইজেশন বলা হয়।

যখন বিলিং চালু থাকে, তখন জেমিনি এপিআই-তে একটি কলের খরচ আংশিকভাবে ইনপুট এবং আউটপুট টোকেনের সংখ্যার উপর নির্ভর করে নির্ধারিত হয়, তাই টোকেন গণনা করার পদ্ধতি জানা সহায়ক হতে পারে।

টোকেন গণনা করুন

জেমিনি এপিআই-এর সমস্ত ইনপুট এবং আউটপুট টোকেনাইজ করা হয়, যার মধ্যে টেক্সট, ইমেজ ফাইল এবং অন্যান্য নন-টেক্সট মাধ্যম অন্তর্ভুক্ত।

আপনি নিম্নলিখিত উপায়ে টোকেন গণনা করতে পারেন:

  • অনুরোধের ইনপুট দিয়ে count_tokens কল করুন। এটি শুধুমাত্র ইনপুটে থাকা মোট টোকেন সংখ্যা ফেরত দেবে। আপনার অনুরোধের আকার পরীক্ষা করার জন্য ইনপুট পাঠানোর আগে এই কলটি করুন।

  • ইন্টারঅ্যাকশন রেসপন্সে এর usage ব্যবহার করুন। এটি ইনপুট ( total_input_tokens ), আউটপুট ( total_output_tokens ), চিন্তাভাবনা ( total_thought_tokens ), ক্যাশ করা কন্টেন্ট ( total_cached_tokens ), টুল ব্যবহার ( total_tool_use_tokens ), এবং মোট ( total_tokens ) টোকেনের সংখ্যা ফেরত দেয়।

টেক্সট টোকেন গণনা করুন

পাইথন

# 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.5-flash",
    contents=prompt
)
print("total_tokens:", total_tokens.total_tokens)

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

জাভাস্ক্রিপ্ট

// 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.5-flash",
    contents: prompt,
});
console.log(countResponse.totalTokens);

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

বিশ্রাম

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

একাধিক পালা টোকেন গণনা করুন

previous_interaction_id ব্যবহার করে কথোপকথনের ইতিহাস জুড়ে টোকেন গণনা করুন:

পাইথন

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

# Second interaction continues the conversation
interaction2 = client.interactions.create(
    model="gemini-3.5-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}")

জাভাস্ক্রিপ্ট

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

// Second interaction continues the conversation
const interaction2 = await client.interactions.create({
    model: "gemini-3.5-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}`);

মাল্টিমোডাল টোকেন গণনা করুন

জেমিনি এপিআই-তে দেওয়া সমস্ত ইনপুট, যার মধ্যে ছবি, ভিডিও এবং অডিও অন্তর্ভুক্ত, টোকেনাইজ করা হয়। টোকেনাইজেশন সম্পর্কে গুরুত্বপূর্ণ বিষয়গুলো হলো:

  • ছবি : উভয় দিকে ৩৮৪ পিক্সেল বা তার কম আকারের ছবি ২৫৮টি টোকেন হিসেবে গণ্য হয়। এর চেয়ে বড় ছবিগুলোকে ৭৬৮x৭৬৮ পিক্সেলের টাইলে ভাগ করা হয়, যার প্রতিটি ২৫৮টি টোকেন হিসেবে গণ্য হয়।
  • ভিডিও : প্রতি সেকেন্ডে ২৬৩ টোকেন
  • অডিও : প্রতি সেকেন্ডে ৩২টি টোকেন

ছবির টোকেন

পাইথন

# 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.5-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.5-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)

জাভাস্ক্রিপ্ট

// 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.5-flash",
    contents: [
        { text: "Tell me about this image" },
        { fileData: { fileUri: uploadedFile.uri, mimeType: uploadedFile.mimeType } }
    ]
});
console.log(countResponse.totalTokens);

ইনলাইন ডেটার উদাহরণ:

পাইথন

# 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.5-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)

ভিডিও টোকেন

পাইথন

# 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 263 * 60 = 15,780 tokens
total_tokens = client.models.count_tokens(
    model="gemini-3.5-flash",
    contents=["Summarize this video", video_file]
)
print(f"Total tokens: {total_tokens}")

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

অডিও টোকেন

পাইথন

# 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.5-flash",
    contents=["Transcribe this audio", audio_file]
)
print(f"Total tokens: {total_tokens}")

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

সিস্টেম নির্দেশ টোকেন গণনা করুন

সিস্টেম নির্দেশাবলী ইনপুট টোকেনের অংশ হিসেবে গণনা করা হয়:

পাইথন

# This will only work for SDK newer than 2.0.0
interaction = client.interactions.create(
    model="gemini-3.5-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}")

টুল টোকেন গণনা করুন

টুলস (ফাংশন, কোড এক্সিকিউশন, গুগল সার্চ)ও গণনা করা হয়:

পাইথন

# 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.5-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}")

প্রসঙ্গ উইন্ডো

প্রতিটি জেমিনি মডেলের একটি সর্বোচ্চ সংখ্যক টোকেন ধারণ করার ক্ষমতা থাকে। কনটেক্সট উইন্ডোটি ইনপুট এবং আউটপুট টোকেনের সম্মিলিত সীমা নির্ধারণ করে।

প্রোগ্রাম্যাটিকভাবে কনটেক্সট উইন্ডোর আকার পান

পাইথন

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

জাভাস্ক্রিপ্ট

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

মডেল পেজে কনটেক্সট উইন্ডোর সাইজগুলো খুঁজুন।

এরপর কী?