Gemini dhe modele të tjera të IA-së gjeneruese përpunojnë hyrjen dhe daljen në një shkallë të detajuar të quajtur token .
Për modelet Gemini, një token është ekuivalent me rreth 4 karaktere. 100 tokena janë të barabarta me rreth 60-80 fjalë në anglisht.
Rreth tokenëve
Shenjat mund të jenë karaktere të vetme si z ose fjalë të tëra si cat . Fjalët e gjata ndahen në disa shenja. Bashkësia e të gjitha shenjave të përdorura nga modeli quhet fjalor, dhe procesi i ndarjes së tekstit në shenja quhet tokenizim .
Kur faturimi është i aktivizuar, kostoja e një thirrjeje në Gemini API përcaktohet pjesërisht nga numri i tokenëve hyrës dhe dalës, kështu që të dish se si të numërosh tokenët mund të jetë e dobishme.
Numëroni shenjat
Të gjitha të dhënat hyrëse dhe dalëse nga Gemini API janë të tokenizuara, duke përfshirë tekstin, skedarët e imazheve dhe modalitete të tjera jo-tekstuale.
Ju mund të numëroni shenjat në mënyrat e mëposhtme:
Thirrni
count_tokensme inputin e kërkesës. Kthen numrin total të tokenëve vetëm në input . Kryeni këtë thirrje para se të dërgoni inputin për të kontrolluar madhësinë e kërkesave tuaja.Përdor
usagenë përgjigjen e ndërveprimit. Kthen numërimin e tokenëve për hyrjen (total_input_tokens), daljen (total_output_tokens), të menduarit (total_thought_tokens), përmbajtjen e ruajtur në memorien e përkohshme (total_cached_tokens), përdorimin e mjeteve (total_tool_use_tokens) dhe totalin (total_tokens).
Numëroni shenjat e tekstit
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));
}
PUSHTIM
# 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."}]}]}'
Numëroni tokenët me shumë kthesa
Numëroni tokenët në historikun e bisedave duke përdorur 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));
}
Numëroni tokenët multimodalë
Të gjitha të dhënat hyrëse në Gemini API janë të tokenizuara, duke përfshirë imazhet, videon dhe audion. Pikat kryesore rreth tokenizimit:
- Imazhet : Imazhet ≤384 piksel në të dy dimensionet llogariten si 258 tokena. Imazhet më të mëdha ndahen në pllaka me 768x768 piksel, secila duke u llogaritur si 258 tokena.
- Video : 263 tokena për sekondë (vlen për përpunimin statik). Për përpunimin agjentik, përdorimi i tokenave ndryshon. Shih Përdorimi i tokenave video sipas modalitetit të përpunimit .
- Audio : 32 tokena për sekondë
Shenjat e imazhit
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));
}
Shembull i të dhënave të integruara:
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)
Tokena videoje
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)
Përdorimi i tokenit të videos sipas modalitetit të përpunimit
Përdorimi i tokenit për video varet nga mënyra e përpunimit:
| Modaliteti i përpunimit | Llogaritja e tokenëve | Përdorimi tipik |
|---|---|---|
| Statik (parazgjedhur) | ~100 token/sekondë si parazgjedhje (rezolucion i ulët) ose ~300 token/sekondë (rezolucion i lartë). Të gjitha kuadrot u morën si mostra me 1 FPS. | I parashikueshëm, në përpjesëtim me kohëzgjatjen e videos. |
| Agjent | Varion sipas kompleksitetit të përmbajtjes. Modeli ngarkon vetëm transkriptin dhe/ose kuadrot dhe/ose audion e nevojshme për t'iu përgjigjur kërkesës. | Deri në 88% më pak tokena për përmbajtje të gjatë. |
Me përpunimin agjentik, një leksion 1-orësh që do të përdorte ~1.08 milion tokena në modalitetin statik mund të përdorë ~108 mijë tokena, në varësi të kërkesës dhe përmbajtjes.
Për të kontrolluar përdorimin aktual të tokenit për një kërkesë, inspektoni interaction.usage . Tokenët e videos agjente raportohen në fushat e mëposhtme:
- Kërkesa fillestare (referencë videoje + kërkesë përdoruesi):
total_input_tokens - Të menduarit e navigimit :
total_thought_tokens - Transkripti, kornizat dhe audioja ngarkohen sipas kërkesës :
total_tool_use_tokens - Përgjigja përfundimtare :
total_output_tokens
Tokena audio
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)
Shenjat e udhëzimeve të sistemit të numërimit
Udhëzimet e sistemit llogariten si pjesë e tokenëve të hyrjes:
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}")
Numëroni shenjat e mjeteve
Mjetet (funksionet, ekzekutimi i kodit, Kërkimi në Google) llogariten gjithashtu:
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}")
Dritarja e kontekstit
Çdo model Gemini ka një numër maksimal të tokenëve që mund të trajtojë. Dritarja e kontekstit përcakton limitin e kombinuar të tokenëve hyrës dhe dalës.
Merrni madhësinë e dritares së kontekstit në mënyrë programore
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));
}
Gjeni madhësitë e dritares së kontekstit në faqen e modeleve .
Çfarë vjen më pas
- Gjenerimi i tekstit : Bazat e gjenerimit
- Ruajtja në memorje : Zvogëloni kostot me ruajtjen në memorje
- Çmimet : Kuptoni kostot
Gemini dhe modele të tjera të IA-së gjeneruese përpunojnë hyrjen dhe daljen në një shkallë të detajuar të quajtur token .
Për modelet Gemini, një token është ekuivalent me rreth 4 karaktere. 100 tokena janë të barabarta me rreth 60-80 fjalë në anglisht.
Rreth tokenëve
Shenjat mund të jenë karaktere të vetme si z ose fjalë të tëra si cat . Fjalët e gjata ndahen në disa shenja. Bashkësia e të gjitha shenjave të përdorura nga modeli quhet fjalor, dhe procesi i ndarjes së tekstit në shenja quhet tokenizim .
Kur faturimi është i aktivizuar, kostoja e një thirrjeje në Gemini API përcaktohet pjesërisht nga numri i tokenëve hyrës dhe dalës, kështu që të dish se si të numërosh tokenët mund të jetë e dobishme.
Numëroni shenjat
Të gjitha të dhënat hyrëse dhe dalëse nga Gemini API janë të tokenizuara, duke përfshirë tekstin, skedarët e imazheve dhe modalitete të tjera jo-tekstuale.
Ju mund të numëroni shenjat në mënyrat e mëposhtme:
Thirrni
count_tokensme inputin e kërkesës. Kthen numrin total të tokenëve vetëm në input . Kryeni këtë thirrje para se të dërgoni inputin për të kontrolluar madhësinë e kërkesave tuaja.Përdor
usagenë përgjigjen e ndërveprimit. Kthen numërimin e tokenëve për hyrjen (total_input_tokens), daljen (total_output_tokens), të menduarit (total_thought_tokens), përmbajtjen e ruajtur në memorien e përkohshme (total_cached_tokens), përdorimin e mjeteve (total_tool_use_tokens) dhe totalin (total_tokens).
Numëroni shenjat e tekstit
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));
}
PUSHTIM
# 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."}]}]}'
Numëroni tokenët me shumë kthesa
Numëroni tokenët në historikun e bisedave duke përdorur 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));
}
Numëroni tokenët multimodalë
Të gjitha të dhënat hyrëse në Gemini API janë të tokenizuara, duke përfshirë imazhet, videon dhe audion. Pikat kryesore rreth tokenizimit:
- Imazhet : Imazhet ≤384 piksel në të dy dimensionet llogariten si 258 tokena. Imazhet më të mëdha ndahen në pllaka me 768x768 piksel, secila duke u llogaritur si 258 tokena.
- Video : 263 tokena për sekondë (vlen për përpunimin statik). Për përpunimin agjentik, përdorimi i tokenave ndryshon. Shih Përdorimi i tokenave video sipas modalitetit të përpunimit .
- Audio : 32 tokena për sekondë
Shenjat e imazhit
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));
}
Shembull i të dhënave të integruara:
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)
Tokena videoje
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)
Përdorimi i tokenit të videos sipas modalitetit të përpunimit
Përdorimi i tokenit për video varet nga mënyra e përpunimit:
| Modaliteti i përpunimit | Llogaritja e tokenëve | Përdorimi tipik |
|---|---|---|
| Statik (parazgjedhur) | ~100 token/sekondë si parazgjedhje (rezolucion i ulët) ose ~300 token/sekondë (rezolucion i lartë). Të gjitha kuadrot u morën si mostra me 1 FPS. | I parashikueshëm, në përpjesëtim me kohëzgjatjen e videos. |
| Agjent | Varion sipas kompleksitetit të përmbajtjes. Modeli ngarkon vetëm transkriptin dhe/ose kuadrot dhe/ose audion e nevojshme për t'iu përgjigjur kërkesës. | Deri në 88% më pak tokena për përmbajtje të gjatë. |
Me përpunimin agjentik, një leksion 1-orësh që do të përdorte ~1.08 milion tokena në modalitetin statik mund të përdorë ~108 mijë tokena, në varësi të kërkesës dhe përmbajtjes.
Për të kontrolluar përdorimin aktual të tokenit për një kërkesë, inspektoni interaction.usage . Tokenët e videos agjente raportohen në fushat e mëposhtme:
- Kërkesa fillestare (referencë videoje + kërkesë përdoruesi):
total_input_tokens - Të menduarit e navigimit :
total_thought_tokens - Transkripti, kornizat dhe audioja ngarkohen sipas kërkesës :
total_tool_use_tokens - Përgjigja përfundimtare :
total_output_tokens
Tokena audio
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)
Shenjat e udhëzimeve të sistemit të numërimit
Udhëzimet e sistemit llogariten si pjesë e tokenëve të hyrjes:
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}")
Numëroni shenjat e mjeteve
Mjetet (funksionet, ekzekutimi i kodit, Kërkimi në Google) llogariten gjithashtu:
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}")
Dritarja e kontekstit
Çdo model Gemini ka një numër maksimal të tokenëve që mund të trajtojë. Dritarja e kontekstit përcakton limitin e kombinuar të tokenëve hyrës dhe dalës.
Merrni madhësinë e dritares së kontekstit në mënyrë programore
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));
}
Gjeni madhësitë e dritares së kontekstit në faqen e modeleve .
Çfarë vjen më pas
- Gjenerimi i tekstit : Bazat e gjenerimit
- Ruajtja në memorje : Zvogëloni kostot me ruajtjen në memorje
- Çmimet : Kuptoni kostot