How to count Gemini tokens locally

Practical guide to counting Gemini tokens locally. Learn multimodal tokenization, Python examples, and how to estimate costs offline. Improve your

miércoles, 1 de julio de 2026 • 3 min read • Q2BSTUDIO Team

Complete guide to local tokenization in Gemini

In today's AI ecosystem, understanding how information is quantified before being sent to a model is a strategic skill. Multimodal models like Gemini do not directly process text, images, or audio; instead, they transform each input into discrete units called tokens. This process, known as tokenization, acts as a semantic compression codec that allows the model to interpret and generate content efficiently. Counting these tokens locally, without relying on an API call, offers significant advantages in terms of latency, privacy, and cost control.

Gemini employs specific tokenizers depending on the modality: a text tokenizer based on the Gemma vocabulary, another for images that assigns tokens based on the configured resolution, and an audio tokenizer that consumes approximately 25 tokens per second. For videos, both are combined: frames are sampled at a given frequency (default 1 FPS) and each is tokenized with a maximum budget according to the resolution level (low, medium, high). PDF documents are processed as images page by page, with token limits ranging from 280 to 1,120 tokens per page. Knowing these rules allows developers to estimate consumption before making a request, thus optimizing performance and budget.

The Python library google-genai includes a LocalTokenizer that exactly replicates the server-side tokenization logic. Initializing it requires downloading about 30 MB of data once, after which it can operate completely offline. This approach is ideal for applications handling large volumes of sensitive data, as it avoids sending information over the network just to count tokens. Additionally, it allows routing requests based on context size, choosing lighter models for small inputs or models with a larger context window when necessary. The ability to audit token counts offline reinforces data sovereignty and reduces dependence on external services.

Beyond text, multimodal tokenization introduces important nuances. For example, an image of a cat can consume 1,080 tokens at high resolution, but only 264 at low resolution, saving 75% of the cost if visual detail is not critical. For audio, a 30-second file requires approximately 770 tokens. For a one-minute video, the sum of frames (1,080 tokens at medium resolution) and audio (1,475 tokens) totals about 2,555 tokens, a figure that doubles when increasing the sampling rate to 2 FPS. These values are not fixed: the API returns precise metadata in usage_metadata, which breaks down the count by modality (TEXT, IMAGE, VIDEO, AUDIO, DOCUMENT). This information is the source of truth for billing and allows dynamically adjusting the configuration of each request.

For companies developing AI-based solutions, mastering local tokenization translates into better cost planning and more efficient integration into existing workflows. For example, an application processing PDF invoices can estimate token consumption based on the number of pages and chosen resolution, and decide whether it is worth scaling to a more powerful model. Similarly, a customer service system using AI agents can measure the impact of adding system instructions or conversation history, avoiding unexpectedly exceeding context limits. These optimizations are key to offering custom applications that maintain a balance between accuracy and operational cost.

At Q2BSTUDIO, a company specialized in custom software development, we integrate this knowledge into our artificial intelligence and AWS and Azure cloud services solutions. The ability to count tokens locally aligns with our philosophy of providing auditable, secure, and efficient tools. Whether for deploying AI agents that interact with business documents or building Power BI dashboards that monitor cognitive resource consumption, granular control over tokenization allows our clients to scale their operations with confidence. Furthermore, the use of usage metadata facilitates the implementation of cybersecurity and data governance policies, by not relying exclusively on external calls for measurement tasks.

To delve deeper into how to apply these techniques in real projects, we recommend exploring our AI solutions for businesses, where we combine intelligent tokenization, multimodal models, and cloud architectures to transform data into decisions. Local tokenization is not just a technical capability: it is a pillar for building responsible, predictable, and scalable systems in the era of generative artificial intelligence.

A BREAK?

Play for a moment before you go

OUR SERVICES

How we can help you

Do you have a project in mind?

Tell us your vision and we'll turn it into a software solution. Whatever the scope, we make your idea real.