Nodes/ComfyUI-Apt_Preset/UC_ContextCache
ComfyUI Node

UC_ContextCache

A checkpoint for the context wire (the name oversells it)

By cardenluo·Created 2 years ago·Updated about 21 hours ago· 356
UC_ContextCache
  • context
  • context
◄retain_imagesfalse►
◄unload_clipfalse►

If you've ever wired the same model, clip, vae, positive, negative, latent and half a dozen sampler settings into ten different nodes, you already understand why the context object exists. Apt_Preset's UC_* stack (UC_create_context → UC_ksampler → UC_Ksampler_refine → …) bundles all of that into one RUN_CONTEXT mapping travelling down a single wire. Everything you'd normally drag across the canvas rides along inside it, and each UC node pulls off what it needs, updates what it changed, and hands the bundle on.

UC_ContextCache is the small, unglamorous node in that line. It takes the context and returns a copy of it - optionally with the decoded image stripped out.

What it actually does

Two inputs: the RUN_CONTEXT and a cache_images boolean, default false. One output: the RUN_CONTEXT back.

The implementation is a shallow dict copy with one conditional: if cache_images is off, the copy's images key is set to None. That's the entire node. Nothing is memoized, nothing is held in VRAM on your behalf, and no cheap re-use is being computed - the name is doing more work than the code is.

The reason the images key matters is that a UC sampler with VAE_Decode enabled writes the decoded IMAGE into the context on its way out. That tensor is by far the heaviest thing in the bundle, and because node outputs get cached by ComfyUI, it stays alive for anything downstream that can reach it. Dropping it at a chosen point in the graph is how you stop that from happening.

And nothing downstream breaks when you drop it. UC_Ksampler_refine, for instance, looks for an image in the context, and if there isn't one, falls back to decoding the latent directly. So clearing images costs you a VAE decode wherever the image is genuinely needed again - and saves you the resident tensor everywhere it isn't.

When you'd reach for it

Forking is the honest answer. Put a UC_ContextCache where the graph splits, leave cache_images off on the branch that goes off to do heavy sampling or tiling, and keep it on for the branch that wants to preview or refine the image you just made. One line, and one half of the graph stops carrying a pixel tensor it was never going to touch.

The other use is the opposite one: setting cache_images to true to deliberately keep the decoded image in the bundle so a chain of UC nodes can reuse it instead of re-decoding the latent at each step. That's the more common intent, honestly, and it's worth checking which way you want it before the graph gets big - the default is off.

Inputs, and one weird error message

context must be a RUN_CONTEXT from the UC stack - a plain ComfyUI wire won't do, which is the known lock-in of the context-bus pattern: every pack's bus is its own type and they don't interoperate. cache_images is the only decision you make.

If you connect something that isn't a mapping, the node raises a message calling itself AD_Ref2_ContextCache - a leftover from its earlier name. It's not a separate node and there's nothing wrong with your install; the error text just predates the rename.

The failure mode that lives here

Contexts go stale, and this node is on the path where that happens. Because the bundle carries everything, it's easy to take a copy and hand it to a branch that then unpacks a model from before the node that applied your LoRA - the wire is connected, the value inside is wrong, and nothing warns you. When you're debugging a UC graph and the output looks like the base model, that's the shape of the bug. Keep the values you're actively iterating on explicit wires near the part of the graph you're tuning; let the context carry the settled stuff.

Install

ComfyUI Manager → search Apt_Preset → install → restart, or:

cd ComfyUI/custom_nodes
git clone https://github.com/cardenluo/ComfyUI-Apt_Preset.git
cd ComfyUI-Apt_Preset && pip install -r requirements.txt

(Windows: install-ComfyUI-Portable.bat in the pack root.) You need nothing else for this node - the extra integrations the README lists, like ComfyUI-Advanced-ControlNet or ComfyUI-GGUF, are for other parts of the pack. The UC family itself lives in the same custom-node project, so UC_ContextCache shows up in the node search under Apt_Preset/unit_context as soon as the pack imports cleanly.

CategoryApt_Preset/unit_context

Inputs (3)

NameTypeDefaultDescription
contextRUN_CONTEXT—
retain_imagesBOOLEANfalse是否在输出 context 中保留图片;关闭后图片可在没有其他引用时自然回收。
unload_clipBOOLEANfalse卸载 CLIP 的显存驻留并从输出 context 移除 CLIP;已生成的条件不受影响。

Outputs (1)

NameTypeDescription
contextRUN_CONTEXT—