Nodes/ComfyUI-ZMongo/05 Gemini Count Tokens
ComfyUI Node

05 Gemini Count Tokens

Checking prompt size before it bites

By CentralFloridaAttorney·Created 7 months ago·Updated 4 months ago· 2
05 Gemini Count Tokens
  • session
  • json
  • token_count
  • success
◄prompt►
◄modelgemini-2.5-flash►
◄gemini_prefix/gemini►
◄refresh_token►

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 (default gemini-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_count is 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.

CategoryZMongo/06 Gemini

Inputs (5)

NameTypeDefaultDescription
sessionZMONGO_API_SESSION—
promptSTRING—
modelSTRINGgemini-2.5-flash—
gemini_prefixoptSTRING/gemini—
refresh_tokenoptSTRING—

Outputs (3)

NameTypeDescription
jsonSTRING—
token_countINT—
successBOOLEAN—