Nodes/ComfyUI-textnodes/Prompt Truncate
ComfyUI Node

Prompt Truncate

It says 'tokens,' but it cuts tags — read this first

By akierson·Created 2 years ago·Updated 2 years ago· 0
Prompt Truncate
    • string
    number_of_tokens0
    string

    This is the second node in akierson's tiny ComfyUI-textnodes pack, and it looks like the answer to a very common question: "my prompt is too long for CLIP's 77-token window, how do I cut it down?" It isn't that. The name is a lie, the default setting wipes your prompt, and once you know both of those things it's a genuinely handy utility. Let me save you the debugging session.

    What it actually does

    You give it a string and a number; it splits the string on commas, keeps the first N pieces, and rejoins them with ", ". If number_of_tokens is 5 and your string is 1girl, long hair, red dress, park, sunlight, grass, you get 1girl, long hair, red dress, park, sunlight. That's the whole mechanism: strip surrounding whitespace, split(','), slice [:n], ', '.join(...), then re-wrap any BREAK tokens just like its sibling node. Pure Python stdlib, no dependencies.

    The catch is right there in the parameter name. "number_of_tokens" is really "number of comma-separated tags." A booru tag is not a token - "long hair" alone can be two or more tokens to the CLIP tokenizer. Truncating to 75 here is not truncating to 75 tokens. If your goal is to squeeze under a 77-token boundary, this node won't get you there, and assuming it will is the main way people get confused.

    The inputs (and the trap)

    • string - a STRING prompt or tag list. It's a plain single-line field, not multiline, so for anything long you'll want to wire it in from a text node (like the pack's own Tidy Tags) rather than type into the box.
    • number_of_tokens - an INT, default 0.

    The default is the trap. tags[:0] is an empty list, so with the defaults this node feeds an empty string to whatever's downstream. Leave it untouched and your CLIP Text Encode gets "" - congratulations, you've generated from the empty prompt. Set it to a positive number explicitly, always. If you wanted "no truncation," don't use this node at all.

    The output is string, a STRING you can feed into a CLIP Text Encode prompt input or chain further along your text graph.

    When to reach for it

    Where it earns its keep: pinning a prompt to a fixed number of concepts so a batch stays consistent, or keeping only the top-N important tags. Since earlier tags get stronger attention on the SDXL lineage, the first tags are usually the ones you actually want to keep. But if your real problem is token length for a 77-token CLIP window, reach for a node that actually counts tokens - or just write a shorter prompt.

    Installing it

    Same pack, same trivial install as Tidy Tags. ComfyUI Manager → Install Custom Nodes → search "ComfyUI-textnodes", or:

    cd ComfyUI/custom_nodes
    git clone https://github.com/akierson/ComfyUI-textnodes
    

    Restart and you're done. No requirements.txt, no models, nothing to break.

    Categoryutils

    Inputs (2)

    NameTypeDefaultDescription
    number_of_tokensINT0
    stringSTRING

    Outputs (1)

    NameTypeDescription
    stringSTRING