05 Gemini Count Tokens
Checking prompt size before it bites
- session
- json
- token_count
- success
05 Gemini Count Tokens returns the token count for a prompt against a given model, without generating anything. It's the pack's measuring stick: how big is this prompt, how close am I to a context window, does this fit?
If you're assembling prompts dynamically - which is exactly what this pack enables, especially Gemini Prompt from ZMongo Doc (where a document's text gets stuffed into a prompt) - token counts stop being a curiosity and become a failure mode. A huge document will happily blow past a model's context limit, and you'll get truncation or a hard error. Counting first turns that into a decision: trim, split, or switch models.
How it works
Required inputs:
session- from the API Key Session node.prompt- the text to measure.model- which model's tokenizer to use (defaultgemini-2.5-flash). This matters: token counts are model-specific, so measure against the same model you'll actually call.
Optional gemini_prefix and refresh_token as usual. It POSTs to /gemini/api/count-tokens. Outputs:
json- the backend payload.token_count- the integer count, pulled from the response (total_tokens/token_count/tokens, whatever the server returned).success- boolean.
Notes
- Token counting runs server-side through the ZMongo backend, like every Gemini node here. It's a real API round-trip, though a cheap one.
token_countis a genuine INT - you can wire it into math nodes or a comparison to gate the real call: "only send to Gemini if token_count < threshold." That's the pattern for keeping document-driven prompts safe.- Count your entire final prompt. The node measures what you give it; if you're prefixing/suffixing content (as the from-doc node does), count the assembled string, not just the raw document text.
Install
ComfyUI Manager → ComfyUI-ZMongo, or:
cd ComfyUI/custom_nodes
git clone https://github.com/CentralFloridaAttorney/ComfyUI-ZMongo
restart. Another thin HTTP client - the API session does the work, no local model weights involved.
Inputs (5)
| Name | Type | Default | Description |
|---|---|---|---|
| session | ZMONGO_API_SESSION | — | |
| prompt | STRING | — | |
| model | STRING | gemini-2.5-flash | — |
| gemini_prefixopt | STRING | /gemini | — |
| refresh_tokenopt | STRING | — |
Outputs (3)
| Name | Type | Description |
|---|---|---|
| json | STRING | — |
| token_count | INT | — |
| success | BOOLEAN | — |