Nodes/lf-nodes/Sort tags
ComfyUI Node

Sort tags

Put the quality tags first, before CLIP cuts them off

By lucafoscili·Created 2 years ago·Updated 6 days ago· 35
Sort tags
  • clip
  • ui_widget
  • string
  • string_list
caption
desired_ordermasterpiece, best quality
clip_limit75

Here's the thing nobody warns you about with danbooru-style prompting: CLIP truncates. On SD 1.5 and SDXL the text encoder only really reads the first 75 tokens, so if your caption leads with a dozen niche tags and buries "masterpiece, best quality" at the end, those quality tags may never survive the trip to the sampler. LF_SortTags fixes the ordering - you tell it which tags must lead, it moves them to the front, and it shows you the actual CLIP token count so you can see when you're about to get cut off. It's the prompt-hygiene node from the lf-nodes logic family.

One honest caveat before you build a whole workflow on it: this is a CLIP-era tool. It pays off on SD 1.5, SDXL, Illustrious, NoobAI and Pony - models whose CLIP encoder has a real 77-token window. On a 2026 model with an LLM text encoder (Flux, Klein, Anima and friends), your prompt is an instruction to a general-purpose LLM and there's no token boundary to trip over; sorting tags there is cosmetic rather than load-bearing. Know which side your checkpoint is on.

How it works

The node takes your caption, splits it into individual tags, and re-orders them: everything on your desired_order list moves to the front (keeping that list's order), everything else follows in its original relative order. So masterpiece, best quality, 1girl, blue hair stays as-is, but 1girl, masterpiece, blue hair becomes masterpiece, 1girl, blue hair.

The part that makes it more than a sort is that it actually tokenizes the result with the CLIP model you hand it and counts the tokens. If the count exceeds your clip_limit (default 75 - the classic CLIP window), it prints a ⚠️ TRUNCATES warning in its output widget, with a full token-by-token breakdown so you can see exactly what's falling off the end. That's genuinely useful information: truncation is silent in most workflows, and this surfaces it.

The inputs that matter

  • clip (CLIP) - any CLIP model. It's only used for tokenization/counting, not generation, so it doesn't need to be the model you'll sample with - but wiring the real one keeps the count honest.
  • caption (STRING) - the caption(s) to reorder. One per line or comma-separated; each line gets sorted independently.
  • desired_order (STRING, default masterpiece, best quality) - the leading order, comma-separated. Tags not present in the caption are simply skipped.
  • clip_limit (INT, default 75) - your warning threshold for token count. Set it to the real window of the model you're using.

Outputs are string (the final sorted caption) and string_list (the same, as a list).

Install

Part of lf-nodes:

  • ComfyUI Manager: search "LF Nodes", install, restart.
  • Manual: cd ComfyUI/custom_nodes && git clone https://github.com/lucafoscili/lf-nodes.git, restart.

No models to download - the CLIP you pass in is one you already load.

Common issues

  • "It didn't change my caption." Check your tags actually match desired_order exactly - this is a plain string match, so Masterpiece with a capital M won't match masterpiece.
  • The warning fires even under 75. The token count uses the real CLIP tokenizer, and BOS/EOS tokens count too. If you set clip_limit to 75, a caption near the cap can legitimately warn - that's the node being honest about the window, not a bug.
  • It reorders, it doesn't truncate. The node warns you that something would be cut off; the actual cut happens downstream in the CLIP encoder. If you see the warning, prune tags or shorten them - don't assume the node fixed the problem.
  • Multi-caption gotchas. Each line is sorted independently, so a batch of captions stays one-per-line. Semicolons inside a caption are treated as part of a tag; this node splits on commas and newlines only.
Category✨ LF Nodes/Logic

Inputs (5)

NameTypeDefaultDescription
clipCLIPCLIP model to use for tokenization.
captionSTRINGThe caption(s) whose tags need re-ordering. One per line or comma-separated.
desired_orderSTRINGmasterpiece, best qualityComma-separated list indicating the desired leading order.
clip_limitINT75Maximum number of CLIP tokens allowed. Exceeding this will display a warning in the output widget.
ui_widgetoptLF_CODE

Outputs (2)

NameTypeDescription
stringSTRINGFinal sorted caption string.
string_listSTRINGList of all sorted caption strings (if any).