Nodes/ComfyUI API Toolkit/Gemini Token Counter
ComfyUI Node

Gemini Token Counter

Know your prompt's token cost before you spend a cent

By IxMxAMAR·Created 5 months ago·Updated 2 months ago· 1
Gemini Token Counter
    • token_count
    api_key
    modelgemini-2.5-flash
    text
    custom_model

    Gemini charges by the token, and a prompt that looks short can be surprisingly expensive once a model with a big context window gets hold of it. This node answers the boring-but-vital question before you queue a real generation: how many tokens does this text cost for this model? It calls the Gemini token-counting endpoint, which is free - the source notes it doesn't charge against your quota - and returns a single integer.

    Inputs: api_key, model (a 35-entry dropdown spanning the Gemini 3 previews, the 2.5 and 2.0 lines, the latest aliases, and the Gemma family - pick the model you'll actually run), and text (the prompt you're about to send). There's also custom_model if you need an ID that's not in the list - for anything new Google ships before the pack updates.

    One output: token_count (INT).

    How you'd actually use it

    The straightforward use is budgeting: before a batch of prompt-generation calls, check what a long prompt costs, then decide whether to trim it or switch to a flash model. The smarter use is gating: wire token_count into a comparison node and refuse to run when a prompt exceeds your target context or budget. If you're chaining an LLM prompt-refinement step in front of an image generator, this is the node that tells you whether the refiner's output is about to cost as much as the image it's feeding. Token counts also differ by model, so counting with the exact model you'll run matters - "gemini-3-pro-preview" and "gemini-2.5-flash" won't return the same number for the same text.

    Installing it

    Part of the ComfyUI API Toolkit pack. Manager: search "API Toolkit". Manual:

    cd ComfyUI/custom_nodes
    git clone https://github.com/IxMxAMAR/ComfyUI-API-Toolkit
    cd ComfyUI-API-Toolkit
    pip install -r requirements.txt
    

    Restart. This one needs the google-genai>=0.8.0 dependency; without it the whole Gemini service disables itself at startup with a [!!] gemini disabled message.

    Gotchas

    • Counts are exact for the model you pick, but Google's tokenizer can change with a model update, so treat the number as a good estimate rather than a contract.
    • It's a live API call every queue (the pack sets IS_CHANGED on all API nodes). It's free, but it does round-trip, so it's not the node to spam in a tight loop.
    • When a model is deprecated and leaves the dropdown, use custom_model with the ID - the dropdown is static until you restart ComfyUI.
    CategoryAPI Toolkit/Gemini/Config

    Inputs (4)

    NameTypeDefaultDescription
    api_keySTRING
    modelCOMBOgemini-2.5-flash35 options: gemini-pro-latest, gemini-flash-latest, gemini-flash-lite-latest, gemini-3-pro-preview, gemini-3-flash-preview, gemini-3.1-pro-preview, +29
    textSTRING
    custom_modeloptSTRING

    Outputs (1)

    NameTypeDescription
    token_countINT