Nodes/ComfyUI-API-Optimizer/πŸ’° API Cost & Quota Tracker
ComfyUI Node

πŸ’° API Cost & Quota Tracker

Your API bill called. This node is the circuit breaker.

By jeremieLouvaertΒ·Created 5 months agoΒ·Updated 15 days agoΒ· 1
πŸ’° API Cost & Quota Tracker
  • passthrough
  • passthrough
  • cost_summary
β—„api_providerKling 3.0β–Ί
β—„cost_per_run0.050β–Ί
β—„budget_limit10.0β–Ί
β—„reset_budgetfalseβ–Ί

If you run ComfyUI workflows that call paid cloud APIs - Kling 3.0, Magnific, Gemini, RunPod - you know the sinking feeling of queueing a big batch and hoping the bill doesn't spike. When your compute moves to the cloud, the bottleneck stops being VRAM and becomes API cost. This node is a wallet circuit breaker: it sits between your prompt and the API node, tracks every run in a persistent ledger, and halts the queue before the API gets charged if the next run would blow your budget.

What it actually does

Despite the name, the node makes no API call and needs no key. It's a passthrough with a conscience. Your prompt or image flows in through passthrough, and flows right back out the other side unchanged - that's why it's * type and why you can drop it anywhere in the chain. What it does in between is ledger arithmetic.

Each run, it charges cost_per_run against api_provider in a persistent ledger stored at output/api_metrics/api_costs.json. Every charge is also appended to api_transactions.jsonl with a timestamp - a real audit trail if you ever want to see where the money went. Internally everything is decimal.Decimal, not floats, so a long batch of $0.05 charges doesn't accumulate drift.

The interesting bit is the circuit breaker. Before billing, it checks current_total + cost_per_run against budget_limit. If the next run would exceed the limit, it raises an error - and because ComfyUI executes nodes sequentially, that aborts the whole queue before your API node ever runs. The charge never happens. That's the whole trick.

The inputs that matter

  • cost_per_run (default 0.05) - what one execution costs, in dollars. Set this honestly; the node tracks what you tell it, not what the provider's invoice says.
  • budget_limit (default 10.0) - the trip wire. Once the ledger total plus the next run would cross it, the queue halts.
  • api_provider (default "Kling 3.0") - just a label for the ledger, so per-provider totals stay separate. Change it to match what you're actually calling.
  • reset_budget (default false) - flip it true for one run to archive the current ledger to api_costs_archive_*.json and start fresh. Archives instead of deleting, so you keep the history.

The two outputs are passthrough (wire it on to your API node) and cost_summary, a STRING like Total: $12.30 | Remaining: $2.70 you can feed to a text display if you like having the number on screen.

Install

Everything in this pack ships together, so this installs all five nodes:

cd ComfyUI/custom_nodes/
git clone https://github.com/jeremieLouvaert/ComfyUI-API-Optimizer.git
pip install -r ComfyUI-API-Optimizer/requirements.txt

Then restart ComfyUI. The only real dependency is filelock - the requirements file is literally just that one line, and PyTorch's already there. ComfyUI Manager users can search "ComfyUI API Optimizer" instead. No model downloads, nothing heavy.

Where people get burned

The most common surprise is the BUDGET EXCEEDED error - people think it's broken. It's not; that's the node doing its job. Raise the limit or reset the budget and re-queue. Second gotcha: it's a self-discipline tool, not a billing meter. If you set cost_per_run to $0.05 but your provider actually charges $1 per call, the ledger undercounts and the breaker trips late. Err toward overestimating. And yes, the halting is deliberate and coarse - it kills the whole queue, which is exactly what you want when the alternative is an unauthorized API charge.

CategoryAPI Optimization

Inputs (5)

NameTypeDefaultDescription
passthrough*β€”
api_providerSTRINGKling 3.0β€”
cost_per_runFLOAT0.0500–1000β€”
budget_limitFLOAT10.00–10000β€”
reset_budgetBOOLEANfalseβ€”

Outputs (2)

NameTypeDescription
passthrough*β€”
cost_summarySTRINGβ€”