Nodes/Fearnworks Nodes/✂️ FW Trim To Tokens
ComfyUI Node

✂️ FW Trim To Tokens

Automatically cut a prompt down to a token limit

By fearnworks·Created 3 years ago·Updated 2 years ago· 26
✂️ FW Trim To Tokens
  • clip
  • STRING
text
max_tokens

CLIP-based checkpoints - SD 1.5, SDXL, Illustrious, Pony, that whole lineage - truncate your prompt at 77 tokens with zero warning. You can find out you're over budget with CountTokens, but then you're stuck manually deleting words and re-checking until the number drops low enough. TrimToTokens skips that loop: give it text and a token ceiling, and it hands back a string already cut to fit.

Where this earns its keep is anywhere you can't eyeball the prompt before it hits your sampler - a batch job, prompts pulled from a dataset or generated by an LLM, anything where the text is unpredictable in length. Instead of a run silently truncating mid-sentence somewhere you didn't choose, TrimToTokens makes the cutoff deliberate and guaranteed.

How it works

It tokenizes your text with the CLIP tokenizer tied to the model you wire in - same tokenizer, same counts you'd get from CountTokens - and trims the string down until it fits within your specified token count, then returns the trimmed text.

One thing the README doesn't spell out: which end it trims from. If it's cutting from the back, your subject and opening description (usually the most important part) survive and the tail gets dropped; if it's the reverse, that matters a lot for how you should structure a prompt you plan to trim automatically. Worth a quick test on a piece of text with an obvious marker at each end before you rely on it for anything you care about.

The inputs and outputs that matter

  • clip (CLIP) - the CLIP model already in your graph.
  • text (STRING) - the prompt to trim.
  • max_tokens (INT) - the ceiling you want to trim down to. Set this to 77 to guarantee you fit inside a single CLIP chunk, or lower if you're deliberately leaving headroom.

The output is a single STRING - your trimmed prompt. Wire it straight into CLIPTextEncode in place of your raw text.

How to install it

Same repo, same steps as the rest of the pack:

  • ComfyUI Manager - search "Fearnworks Nodes" and install.
  • Manual:
    cd ComfyUI/custom_nodes
    git clone https://github.com/fearnworks/ComfyUI_FearnworksNodes
    
    then restart. No pip requirements or model downloads beyond the CLIP model you're already loading elsewhere in your workflow.

Common issues & troubleshooting

Confirm it actually landed under budget. Because this node changes your prompt rather than just reporting on it, a mistake here is more consequential than with CountTokens. Run the output back through CountTokens once to confirm the trimmed text is really at or under max_tokens - cheap insurance for one extra node.

SDXL 1.0 is the only tested target. The pack's README is explicit that it was validated on SDXL 1.0 and that SD 1.x needs "an additional switch" it doesn't currently ship. If you're trimming for an SD 1.5 checkpoint, treat the result as unverified rather than assumed-correct, and spot-check it.

Doesn't apply to LLM-encoder models. Flux, Z-Image, and similar checkpoints don't have the same 77-token chunk truncation, so there's no equivalent ceiling to trim toward - this node is specifically for the CLIP-encoded lineage.

It's a trim, not a rewrite. If the resulting prompt reads awkwardly because it was cut mid-phrase, that's expected - the node is optimizing for token count, not for grammar. If wording quality matters more than exact token counts, this is a blunt instrument by design; use TokenCountRanker first to see what's expensive and edit it by hand instead.

CategoryFearnworks/Text/Tokens

Inputs (3)

NameTypeDefaultDescription
clipCLIP
textSTRING
max_tokensINT

Outputs (1)

NameTypeDescription
STRINGSTRING