A suite of production-grade custom nodes for ComfyUI designed for workflows that rely on external remote APIs (Kling 3.0, Magnific, Banana.dev, RunPod, etc.).
When your compute moves to the cloud, your bottlenecks shift from VRAM limitations to API Costs, Latency, and Serialization. This custom node pack solves these problems natively inside ComfyUI.
Acts as a circuit-breaker for your wallet. Pass your prompt or image through this node before it hits your API node.
- Budget Enforcement: Set a
$ Budget Limitand$ Cost Per Run. A persistent ledger tracks all charges. If the next run would exceed the budget, execution halts before the API is charged. - Precise Arithmetic: Uses
decimal.Decimalinternally — no floating-point drift on large batch runs. - Transaction Audit Log: Every charge is appended to
api_transactions.jsonlwith timestamps for full traceability. - Safe Resets: Resetting the budget archives the previous ledger instead of discarding it.
- Concurrent-Safe: File locking prevents corruption when multiple ComfyUI instances share the same output directory.
ComfyUI's native caching often breaks with external API nodes (dynamic timestamps, non-deterministic seeds). The Hash Vault is an aggressive disk-caching layer that strictly hashes your prompt, parameters, and input tensors.
- Hash Vault (Check Cache): Hashes your inputs and checks local disk for a cached result.
- Lazy API Switch: Uses ComfyUI's
{"lazy": True}evaluation engine. On a cache hit, this switch physically prevents the upstream API node from executing — saving money and time. - Hash Vault (Save Result): Writes new API outputs to the vault for future cache hits.
- Full-Content Tensor Hashing: Hashes the complete byte representation of tensors (including dtype and shape metadata) — no lossy approximations.
- Recursive Hashing: Correctly handles nested data structures (dicts, lists, tuples) common in ComfyUI latents and conditioning.
- Cache TTL: Optional time-to-live for cache entries. Expired entries are automatically removed and treated as cache misses. Set to
0for entries that never expire. - Device-Portable Caching: All tensors are saved to CPU and loaded with
map_location="cpu", so cache files work regardless of GPU configuration. - Atomic Writes: Cache files are written to a temp file first, then atomically replaced — preventing corruption from interrupted writes.
- Concurrent-Safe: File locking on every cache read/write operation.
To properly bypass an API node, sandwich it with the vault nodes:
- Connect your Prompt/Image to Check Cache.
- Connect the
is_cachedoutput to the Lazy API Switch. - Connect your Prompt/Image to your actual API Node.
- Connect the output of your API Node to Save Result (using the
hash_keyfrom step 1). - Connect both the
cached_data(from step 1) and theapi_data(from step 4) to the Lazy API Switch.
┌─────────────┐
┌───────────►│ API Node ├──► 💾 Save Result ──┐
│ └─────────────┘ │
Prompt/Image─┤ │
│ ┌─────────────┐ ▼
└───────────►│ 🔍 Check ├──────────────► 🔀 Lazy Switch ──► Output
│ Cache │ is_cached ▲
└──┬──────────┘ │
│ cached_data ────────────────────┘
All data is stored under your ComfyUI output directory:
| Path | Description |
|---|---|
output/api_metrics/api_costs.json |
Current cost ledger (per-provider totals) |
output/api_metrics/api_transactions.jsonl |
Append-only audit log with timestamps |
output/api_metrics/api_costs_archive_*.json |
Archived ledgers from budget resets |
output/hash_vault/*.pt |
Cached API outputs (PyTorch format) |
Clone this repository into your ComfyUI/custom_nodes/ directory:
cd ComfyUI/custom_nodes/
git clone https://github.com/jeremieLouvaert/ComfyUI-API-Optimizer.git
pip install -r ComfyUI-API-Optimizer/requirements.txtRestart ComfyUI.
- PyTorch — already present in any ComfyUI installation
- filelock — typically already installed as a transitive dependency of PyTorch/HuggingFace. If not,
pip install filelock.