BasicRecursionFilterNode
Make the LLM read its own answer and try again, until the answer is better
- LLLM_provider
- inner_recursion_filter
- Recursion Filter
This node is a loop wearing a filter costume. BasicRecursionFilterNode builds a "recursion filter" - a callable that takes your original prompt and the model's first completion, and iteratively improves the completion by having the LLM reconsider it. It doesn't answer anything itself. It manufactures the process that makes an answer better, and hands that process to AgentNode.
If that sounds like the Claude-style "think again" trick - it basically is. The default prompt baked into the node asks the model to step back and consider why you asked, what the completion missed, and to rewrite everything using <consideration> and <completion> tags. You can replace that template with your own; that's where you customize the whole personality of the loop.
How it works
The node is wired in two stages. First you build it: connect a completion function to LLLM_provider, set how many improvement rounds you want in max_depth, and it hands you a Recursion Filter output. Then you plug that filter into AgentNode's recursion_filter input. From then on, every time the agent produces a completion, the filter calls the LLM again with your template, feeds the new completion back, and repeats for max_depth rounds. The final output is whatever the last round produced.
The template supports three placeholders:
{prompt}- the original user prompt{completion}- the current completion being improved{date}- today's date, so the model isn't confused about context
The inputs
LLLM_provider(required) - aCALLABLEthat takes a prompt and returns a completion. The normal source isLiteLLMCompletionProvider. This is the engine of the loop.max_depth(default 2) - how many improvement rounds. One round per LLM call. The cost dial.recursion_prompt(optional) - the template. The default is genuinely long and worth reading once; it's opinionated about thinking step-by-step.inner_recursion_filter(optional) - another filter applied before each improvement round. This is how you chain filters:Filter1 → inner_recursion_filter of Filter2 → AgentNode.
Output: Recursion Filter (LLLM_AGENT_RECURSION_FILTER), for AgentNode.
Installing
It's part of ComfyUI_LiteLLM - ComfyUI Manager, search "ComfyUI_LiteLLM", or:
cd ComfyUI/custom_nodes
git clone https://github.com/Hopping-Mad-Games/ComfyUI_LiteLLM
cd ComfyUI_LiteLLM
pip install -r requirements.txt
Restart, set a provider key, done. The agent nodes live under ETK/LLM/LiteLLM with the display name "Completion Enhancement Filter."
Where people get burned
The math is the trap: every filter round is a full LLM call, on top of the agent's own iterations. max_depth of 3 on a workflow you're running a hundred times is a lot of tokens for "slightly better wording." Start at 1, and only raise it if the improvement justifies the spend. Second, this filter only works when it has a real completion function - connecting a raw model object to LLLM_provider errors out, because it's expecting a callable, not a model. And one subtle thing: the default template tells the model to "completely re-write and re-tag everything" - that's aggressive, and on some models it will happily rewrite good answers into wordier ones. If your completions get longer but not better, that's the template talking. Write your own.
Inputs (4)
| Name | Type | Default | Description |
|---|---|---|---|
| max_depth | INT | 2 | — |
| LLLM_provider | CALLABLE | — | |
| recursion_promptopt | STRING | the date is {date} given the prompt: <prompt> {prompt} </prompt> and completion: <completion> {completion} </completion> Please expand on the completion by. - considering the prompt more thoroughly - why would the user have entered this as their prompt? - based on the language the user has used, what can i assume about the user in order to help them? - considering what the completion might have missed. use tags: <consideration></consideration> <completion></completion> inside the consideration tag it is important that you think step by step in at least 5 steps about how to adhere to the instructions. please understand that what you tag as completion will go directly to the user. please completely re-write and re-tag everything, but include everything useful from the given completion and prompt | — |
| inner_recursion_filteropt | LLLM_AGENT_RECURSION_FILTER | — |
Outputs (1)
| Name | Type | Description |
|---|---|---|
| Recursion Filter | LLLM_AGENT_RECURSION_FILTER | — |