ComfyUI Node

Dropout by MaxToken Node

Dropping LLM text down to a token budget (and the bug that ships with it)

By AhBumm·Created 2 years ago·Updated 3 months ago· 11
Dropout by MaxToken Node
    • processed_text
    input_text
    max_tokens300
    dropout_method

    Dropout by MaxToken is a text-trimming utility that lives inside an API-calling pack. The idea: you're about to send text to an LLM API node, or you just got a wall of text back from one, and it's too long for your token budget. This node counts tokens the way the API does and cuts the text down to fit. In a workflow full of API wrappers it's a handy little guardrail - at least in theory.

    Here's the honest part, and you should read this before you get invested: as shipped, this node doesn't actually run on current ComfyUI. It's broken out of the box. Details below, including a one-line fix if you want it.

    How it's supposed to work

    The node uses tiktoken - OpenAI's tokenizer library - with the gpt-3.5-turbo encoding (which is the cl100k_base tokenizer behind gpt-3.5-turbo, gpt-4, and gpt-4o). That matters: it means the token count you see here is the token count the API will actually bill you for, not an approximation from splitting on words.

    If the input is already under max_tokens, the text passes through untouched. If it's over, the node splits the text into sentences (regex on . ! ? followed by a space) and drops sentences one at a time until the count fits. Which ones it drops is the dropout_method:

    • random - removes random sentences. Good for generating variations, since every run trims differently.
    • tail - drops from the end, keeping the beginning. Best when the important context is up front.
    • head - drops from the start, keeping the ending. Use it when the tail of the text is what matters.

    One edge case worth knowing: if a single sentence on its own is still longer than max_tokens, the node gives up and returns an empty string. So don't set the budget absurdly low and expect graceful truncation - it'll nuke the whole text instead.

    The bug

    Look at the code (billbum_modified.py, class BillBum_Modified_DropoutToken_Node). The input is named dropout_method in INPUT_TYPES, but the function signature is:

    def limit_tokens(self, input_text, max_tokens, reduction_method):
    

    ComfyUI calls node functions by keyword argument - it executes f(**inputs), where the keys are exactly your input names. So it passes dropout_method=... and the function has no parameter by that name. Result: a TypeError the moment the node executes. This isn't a settings mistake on your part; the shipped code just can't run.

    If you want to use it anyway, patch one line and restart ComfyUI:

    def limit_tokens(self, input_text, max_tokens, dropout_method):
    

    Rename the parameter to match the input (or rename the input to reduction_method in INPUT_TYPES - either works), then save and restart. It's a two-minute fix if you're comfortable editing a file in your custom_nodes folder.

    What it wires into

    Output is a single string, processed_text. Feed it into any of the pack's LLM API nodes as the prompt, or use it to shrink an API response before passing it further down the graph. The use case is real - keeping a long context within a model's limit is a genuine problem when you're chaining LLM calls - it's just unfortunate that this particular implementation needs the fix above to do it.

    Installing it

    It ships in ComfyUI_BillBum_Nodes. ComfyUI Manager → search billbum → install, or:

    cd ComfyUI/custom_nodes
    git clone https://github.com/AhBumm/ComfyUI_BillBum_APIset_Nodes.git
    pip install -r requirements.txt
    

    Nothing heavy here - tiktoken is a small download, and there are no model files. Restart ComfyUI and the node appears under text_processing.

    Troubleshooting

    • TypeError on execute - that's the parameter-name bug above, not something you caused. Patch and restart, or skip the node.
    • Empty output with a low max_tokens - expected behavior; a single sentence exceeded the budget. Raise the limit or pre-truncate your text.
    • Token counts look off vs. the API - you're both using the same cl100k_base tokenizer, so if they disagree, one of you changed model families. It's exact for gpt-3.5/gpt-4 class models, not for something like a Llama tokenizer.
    Categorytext_processing

    Inputs (3)

    NameTypeDefaultDescription
    input_textSTRING
    max_tokensINT300
    dropout_methodCOMBO3 options: random, tail, head

    Outputs (1)

    NameTypeDescription
    processed_textSTRING