{"id":49028368,"url":"https://github.com/jeremielouvaert/comfyui-api-optimizer","last_synced_at":"2026-04-19T08:02:41.396Z","repository":{"id":345756241,"uuid":"1187236338","full_name":"jeremieLouvaert/ComfyUI-API-Optimizer","owner":"jeremieLouvaert","description":"Production-grade ComfyUI custom nodes for optimizing external API workflows — cost tracking, deterministic caching, and lazy execution bypass.","archived":false,"fork":false,"pushed_at":"2026-03-20T13:51:56.000Z","size":7,"stargazers_count":1,"open_issues_count":0,"forks_count":0,"subscribers_count":0,"default_branch":"master","last_synced_at":"2026-03-21T06:15:22.017Z","etag":null,"topics":["ai","api-optimization","caching","cloud-api","comfyui","cost-tracking","custom-nodes","generative-ai","python","pytorch","stable-diffusion","workflow-automation"],"latest_commit_sha":null,"homepage":null,"language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/jeremieLouvaert.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null,"notice":null,"maintainers":null,"copyright":null,"agents":null,"dco":null,"cla":null}},"created_at":"2026-03-20T13:51:33.000Z","updated_at":"2026-03-20T16:05:46.000Z","dependencies_parsed_at":null,"dependency_job_id":null,"html_url":"https://github.com/jeremieLouvaert/ComfyUI-API-Optimizer","commit_stats":null,"previous_names":["jeremielouvaert/comfyui-api-optimizer"],"tags_count":null,"template":false,"template_full_name":null,"purl":"pkg:github/jeremieLouvaert/ComfyUI-API-Optimizer","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jeremieLouvaert%2FComfyUI-API-Optimizer","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jeremieLouvaert%2FComfyUI-API-Optimizer/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jeremieLouvaert%2FComfyUI-API-Optimizer/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jeremieLouvaert%2FComfyUI-API-Optimizer/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/jeremieLouvaert","download_url":"https://codeload.github.com/jeremieLouvaert/ComfyUI-API-Optimizer/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jeremieLouvaert%2FComfyUI-API-Optimizer/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":31999174,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-04-18T20:23:30.271Z","status":"online","status_checked_at":"2026-04-19T02:00:07.110Z","response_time":55,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["ai","api-optimization","caching","cloud-api","comfyui","cost-tracking","custom-nodes","generative-ai","python","pytorch","stable-diffusion","workflow-automation"],"created_at":"2026-04-19T08:02:40.181Z","updated_at":"2026-04-19T08:02:41.380Z","avatar_url":"https://github.com/jeremieLouvaert.png","language":"Python","funding_links":[],"categories":[],"sub_categories":[],"readme":"# ComfyUI API Optimizer\n\nA 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.).\n\nWhen 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.\n\n## Included Nodes\n\n### 1. API Cost \u0026 Quota Tracker\n\nActs as a circuit-breaker for your wallet. Pass your prompt or image through this node before it hits your API node.\n\n- **Budget Enforcement:** Set a `$ Budget Limit` and `$ Cost Per Run`. A persistent ledger tracks all charges. If the next run would exceed the budget, execution halts *before* the API is charged.\n- **Precise Arithmetic:** Uses `decimal.Decimal` internally — no floating-point drift on large batch runs.\n- **Transaction Audit Log:** Every charge is appended to `api_transactions.jsonl` with timestamps for full traceability.\n- **Safe Resets:** Resetting the budget archives the previous ledger instead of discarding it.\n- **Concurrent-Safe:** File locking prevents corruption when multiple ComfyUI instances share the same output directory.\n\n### 2. The Deterministic Hash Vault Suite (3 Nodes)\n\nComfyUI'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.\n\n- **Hash Vault (Check Cache):** Hashes any combination of a prompt STRING and up to four `any_input` slots — wire an image, a converted-widget dropdown, a converted-widget float, whatever defines uniqueness for your API call. All inputs are optional; any subset you connect factors into the cache key. No prompt? Hash on image + widget values alone.\n- **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.\n- **Hash Vault (Save Result):** Writes new API outputs to the vault for future cache hits.\n\n#### Key Features\n\n- **Full-Content Tensor Hashing:** Hashes the complete byte representation of tensors (including dtype and shape metadata) — no lossy approximations.\n- **Recursive Hashing:** Correctly handles nested data structures (dicts, lists, tuples) common in ComfyUI latents and conditioning.\n- **Cache TTL:** Optional time-to-live for cache entries. Expired entries are automatically removed and treated as cache misses. Set to `0` for entries that never expire.\n- **Device-Portable Caching:** All tensors are saved to CPU and loaded with `map_location=\"cpu\"`, so cache files work regardless of GPU configuration.\n- **Atomic Writes:** Cache files are written to a temp file first, then atomically replaced — preventing corruption from interrupted writes.\n- **Concurrent-Safe:** File locking on every cache read/write operation.\n\n## How to Use the Hash Vault\n\nTo properly bypass an API node, sandwich it with the vault nodes:\n\n1. Connect your Prompt/Image to **Check Cache**.\n2. Connect the `is_cached` output to the **Lazy API Switch**.\n3. Connect your Prompt/Image to your actual API Node.\n4. Connect the output of your API Node to **Save Result** (using the `hash_key` from step 1).\n5. Connect both the `cached_data` (from step 1) and the `api_data` (from step 4) to the **Lazy API Switch**.\n\n### What to feed Check Cache\n\nCheck Cache has one STRING socket (`payload_string`) and four any-type sockets (`any_input`, `any_input_2`, `any_input_3`, `any_input_4`). All are optional — connect whatever defines uniqueness for your API call:\n\n- **Prompt-driven API (e.g. Gemini Image Generate):** wire the prompt STRING to `payload_string`. Done.\n- **Image + prompt API (e.g. image edit):** prompt → `payload_string`, image → `any_input`.\n- **Image-only API with widgets (e.g. Gemini Style Transfer — no prompt, but a style dropdown and strength float):** right-click the style widget → Convert Widget to Input, same for strength. Wire image → `any_input`, style → `any_input_2`, strength → `any_input_3`. All three factor into the hash; changing any of them produces a new cache key.\n\nAny subset of slots works; unused slots contribute nothing to the hash. Adding or ignoring `any_input_2/3/4` in a new workflow doesn't invalidate existing cache keys from older workflows that only used `payload_string` + `any_input`.\n\n```\n                           ┌─────────────┐\n              ┌───────────►│  API Node    ├──► 💾 Save Result ──┐\n              │            └─────────────┘                      │\n Prompt/Image─┤                                                 │\n              │            ┌─────────────┐                      ▼\n              └───────────►│ 🔍 Check    ├──────────────► 🔀 Lazy Switch ──► Output\n                           │   Cache     │  is_cached           ▲\n                           └──┬──────────┘                      │\n                              │ cached_data ────────────────────┘\n```\n\n## Example Workflows\n\n### Prompt-driven API — [`workflows/hash_vault_basic.json`](workflows/hash_vault_basic.json)\n\nThe classic pattern for an API node whose uniqueness lives in a prompt STRING (e.g. Gemini Image Generate). One `StringConstantMultiline` feeds both Hash Vault's `payload_string` and the API node's prompt. Drag it in, set your Gemini API key, press Queue twice: first run generates and caches, second run returns the cached image with zero API call.\n\n### Image-only API with widget inputs — [`workflows/hash_vault_image_only.json`](workflows/hash_vault_image_only.json)\n\nShows the v1.2.0 pattern for an API that has no prompt STRING but does have meaningful widgets, using Gemini Style Transfer (image + style dropdown + intensity dropdown). The Style Transfer Settings node owns the real dropdowns and emits `style` + `intensity` as STRING outputs. Each output fans out to BOTH the matching Style Transfer input (widget converted to input) AND a Hash Vault `any_input_N` slot. The image fans out to Style Transfer AND Hash Vault's `any_input`. All three factor into the hash; change any one of them → new cache key → API runs once.\n\nThe Settings node exists specifically to keep the dropdown UX. A raw STRING primitive driving a converted-widget input has no validation and no typeahead, so a typo silently breaks the cache (the hash is still valid, but Style Transfer errors on the bad value at runtime). The Settings node provides the same ComfyUI-native combo box the Style Transfer widget has, so the wired value is always a known-valid option.\n\n### Requires\n\nBoth workflows use [ComfyUI-Gemini-Direct](https://github.com/jeremieLouvaert/ComfyUI-Gemini-Direct) as the API node. The prompt-driven workflow uses [ComfyUI-KJNodes](https://github.com/kijai/ComfyUI-KJNodes)'s `StringConstantMultiline` as the prompt source; any STRING primitive works. The image-only workflow uses Gemini-Direct's own `Gemini Style Transfer Settings` node for the style + intensity dropdowns.\n\n## Output Files\n\nAll data is stored under your ComfyUI output directory:\n\n| Path | Description |\n|------|-------------|\n| `output/api_metrics/api_costs.json` | Current cost ledger (per-provider totals) |\n| `output/api_metrics/api_transactions.jsonl` | Append-only audit log with timestamps |\n| `output/api_metrics/api_costs_archive_*.json` | Archived ledgers from budget resets |\n| `output/hash_vault/*.pt` | Cached API outputs (PyTorch format) |\n\n## Installation\n\nClone this repository into your `ComfyUI/custom_nodes/` directory:\n\n```bash\ncd ComfyUI/custom_nodes/\ngit clone https://github.com/jeremieLouvaert/ComfyUI-API-Optimizer.git\npip install -r ComfyUI-API-Optimizer/requirements.txt\n```\n\nRestart ComfyUI.\n\n### Dependencies\n\n- **PyTorch** — already present in any ComfyUI installation\n- **filelock** — typically already installed as a transitive dependency of PyTorch/HuggingFace. If not, `pip install filelock`.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjeremielouvaert%2Fcomfyui-api-optimizer","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fjeremielouvaert%2Fcomfyui-api-optimizer","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjeremielouvaert%2Fcomfyui-api-optimizer/lists"}