Nodes/Kinburg-Nodes/Context Sizer (GGUF)
ComfyUI Node

Context Sizer (GGUF)

Stop guessing how big your LLM's context window should be

By Kinburg·Created 3 months ago·Updated 6 days ago· 1
Context Sizer (GGUF)
  • config
  • image
  • text_tokens
  • image_tokens
  • total_tokens
  • suggested_n_ctx
  • info
text_1
margin64
unload_after_runconfig default

Run a local GGUF LLM inside ComfyUI and the first question is always the same: how big should n_ctx be? Set it too small and long prompts truncate mid-thought. Set it too big and you're burning VRAM on a KV cache you'll never fill - on a card that also has to hold the diffusion model. Context Sizer answers the question instead of guessing. It measures how many tokens a request actually needs, then hands you a suggested_n_ctx sized for exactly that.

The clever part is how it counts. The pack's LLM nodes run a real GGUF model in a worker process, but this node does something closer to a calculation: text is counted with the vocab-only tokenizer and images with mtmd_tokenize on the clip/mmproj - no LLM weights loaded, no forward pass. It's lean by design, so you can drop it in front of a generation without it hogging the card. Image tokens are the part people get wrong (they depend on both the model and the image resolution), and this counts them for real rather than assuming, with a safe fallback to a full-model prefill if your build can't tokenize weight-free.

The inputs are refreshingly minimal:

  • config - required; a Local LLM Settings (GGUF) node. Its model (and mmproj, for image sizing) and max_tokens are what get used.
  • text_1 - your prompt text; more text_* slots appear as you wire them and are joined with newlines. The config's system prompt is counted automatically.
  • image - optional; a batch is fine, the node reports the largest image's token cost.
  • margin - safety headroom (default 64) added on top of input + output when suggesting n_ctx.
  • unload_after_run - lets this node free the model from VRAM after it runs without touching the shared config's setting.

Outputs: text_tokens, image_tokens, total_tokens, suggested_n_ctx (= input + the config's max_tokens + margin, rounded up), and info with the breakdown. Wire suggested_n_ctx into the Settings node's context field and you've sized the KV cache to the request.

Installing

Part of Kinburg-Nodes. ComfyUI Manager → search "Kinburg-Nodes", or clone into custom_nodes and restart. This one sits on the pack's LLM infrastructure, which needs llama-cpp-python - Manager runs the pack's install.py automatically, which picks a prebuilt CUDA wheel matched to your torch's CUDA major (the source notes a cu124 wheel on a CUDA-13 torch is exactly the kind of mismatch that breaks a fresh install). By hand:

<ComfyUI>/.venv/Scripts/python.exe <ComfyUI>/custom_nodes/Kinburg-Nodes/install.py

Gotchas

If you feed it an image but the config has no mmproj wired (no Vision Settings), image sizing can't happen - the pack's own docs say image token counting needs the vision model. And remember the output is a suggestion: it sizes to the request plus margin, which is the right call for a workflow that reuses one context. If the same model serves wildly different-sized requests, the lean counting means it's cheap enough to just run it and read the number every time.

CategoryKinburg-Nodes/LLM

Inputs (5)

NameTypeDefaultDescription
configKINBURG_LLM_CONFIGA 'Local LLM Settings (GGUF)' node — its model (and mmproj, for image sizing) and max_tokens are used.
text_1optSTRINGPrompt text to size (e.g. your rubric / prompt). Connect-only; more text_* slots appear as you wire them and are joined with newlines. The config's system prompt is counted automatically.
imageoptIMAGEOptional image(s) to size for — needs an mmproj on the config (Vision Settings). A batch is fine; the node reports the largest image's token cost.
marginoptINT640–8192Safety headroom added on top of input + output when suggesting n_ctx.
unload_after_runoptCOMBOconfig defaultFree the model from VRAM after THIS node runs, without touching the shared config. 'config default' follows the Settings node; 'unload after run' frees VRAM (a different model runs next); 'keep loaded' stays warm (the same model works next).

Outputs (5)

NameTypeDescription
text_tokensINT
image_tokensINT
total_tokensINT
suggested_n_ctxINT
infoSTRING