How muntelco/comfyui works
Purpose and entry points
ComfyUI is a local, Python-based node-graph execution engine for image, video, audio, and 3D generation. It serves an installed frontend plus HTTP/WebSocket APIs and can run workflows programmatically. Primary startup is main.py, which composes runtime configuration, model/node discovery, execution, and the server; request handling centers on server.py. Programmatic examples are in script_examples/basic_api_example.py and script_examples/websockets_api_example.py.
Server and frontend boundary
The backend exposes workflow submission, queueing, progress, output, and asset-facing endpoints to a separately developed, packaged frontend. server.py is the primary transport/control boundary, with protocol structures in protocol.py; the frontend lifecycle/version management is isolated in app/frontend_management.py. TLS is configured at launch rather than requiring a reverse-proxy component in this repository. openapi.yaml defines the public API contract.
Workflow graph and execution
Submitted prompts/workflows are represented, validated, and scheduled as directed node graphs. comfy_execution/graph.py and comfy_execution/validation.py establish graph semantics and input validity; execution.py drives node evaluation. Job lifecycle, cancellation, progress, and partial re-execution/caching are separated into comfy_execution/jobs.py, comfy_execution/progress.py, and comfy_execution/caching.py. Only valid output-reachable subgraphs run; caching avoids recomputation for unchanged graph portions.
Node ecosystem and extension boundary
nodes.py is the central node registry/runtime integration point. Core operations, model loaders, samplers, and output nodes are available there; broad built-in feature nodes live in comfy_extras/, such as comfy_extras/nodes_video.py and comfy_extras/nodes_controlnet.py. Third-party local extensions are loaded from custom_nodes/, while app/custom_node_manager.py governs optional manager behavior. The versioned SDK/type surface for node authors is comfy_api/latest/_io.py and comfy_api/latest/_ui.py.
Local inference runtime
PyTorch-based inference support is concentrated in comfy/: model format detection/loading (comfy/model_detection.py, comfy/sd.py), sampling (comfy/samplers.py, comfy/sample.py), and device/VRAM offloading (comfy/model_management.py, comfy/memory_management.py). Architecture-specific implementations are grouped beneath comfy/ldm/, e.g. comfy/ldm/flux/model.py and comfy/ldm/wan/model.py. This layer supports CUDA, ROCm, Intel XPU, Apple Metal, CPU, and additional PyTorch hardware backends.
Models, files, and assets
Model storage is filesystem-backed and categorized beneath models/; folder_paths.py resolves discoverable model locations, including optional external roots through extra_model_paths.yaml.example. Generated artifacts use output/; inputs use input/. A separate asset catalog provides ingestion, metadata/tagging, references, and cursor-based APIs: app/assets/api/routes.py, app/assets/services/ingest.py, and app/assets/database/models.py. Its schema evolves through Alembic migrations in alembic_db/versions/.
External services and distribution
Optional API nodes call paid remote model providers through provider adapters in comfy_api_nodes/apis/ and node wrappers in comfy_api_nodes/; examples include comfy_api_nodes/apis/openai.py and comfy_api_nodes/apis/gemini.py. Core local inference remains offline unless these nodes or frontend acquisition options are enabled. Dependencies are Python/PyTorch (requirements.txt, pyproject.toml); Windows portable launchers target NVIDIA, AMD, Intel, or CPU under .ci/windows_nvidia_base_files/ and related directories.
Main entry points: server.py HTTP/WebSocket server · nodes.py Node registry · execution.py Node graph executor · main.py Application startup
Components 21
Transport & Frontend
- HTTP/WebSocket server · Accepts workflows and exposes queue, progress, output, and asset APIs.
server.py - Protocol structures
protocol.py - Public API contract
openapi.yaml - Packaged frontend lifecycle
app/frontend_management.py
Workflow Execution
- Workflow graph semantics
comfy_execution/graph.py - Workflow validation
comfy_execution/validation.py - Node graph executor · Evaluates valid output-reachable subgraphs.
execution.py - Job lifecycle
comfy_execution/jobs.py - Progress & execution cache · Reports progress and skips unchanged graph portions.
comfy_execution/caching.py
Node Ecosystem
- Node registry · Integrates core operations, loaders, samplers, and outputs.
nodes.py - Built-in feature nodes · Video, ControlNet, and other optional built-in operations.
- Local extensions
- Node author SDK
comfy_api/latest/_io.py - Remote provider nodes · Node wrappers access paid remote model providers when enabled.
Local Inference
- Model loading & detection
comfy/sd.py - Sampling runtime
comfy/samplers.py - Device & VRAM management · Supports offloading across PyTorch hardware backends.
comfy/model_management.py
Models & Assets
- Model storage & discovery · Resolves categorized local and optional external model roots.
folder_paths.py - Asset catalog · Ingests assets and serves metadata, tags, references, and cursor APIs.
app/assets/services/ingest.py - Asset API routes
app/assets/api/routes.py
Other components
- Application startup · Composes configuration, discovery, execution, and server.
main.py
Connections 23
- Application startup → HTTP/WebSocket server: starts
- Application startup → Node registry: discovers nodes
- Application startup → Node graph executor: initializes
- HTTP/WebSocket server → Protocol structures: uses
- HTTP/WebSocket server → Public API contract: implements
- HTTP/WebSocket server → Packaged frontend lifecycle: serves/manages
- HTTP/WebSocket server → Workflow graph semantics: submits workflows
- HTTP/WebSocket server → Job lifecycle: queues jobs
- HTTP/WebSocket server → Asset API routes: mounts
- Workflow graph semantics → Workflow validation: validates
- Workflow validation → Node graph executor: permits valid graph
- Job lifecycle → Node graph executor: schedules
- Node graph executor → Progress & execution cache: updates
- Node graph executor → Node registry: evaluates nodes
- Node registry → Built-in feature nodes: loads
- Node registry → Local extensions: loads
- Node author SDK → Local extensions: authoring surface
- Node registry → Remote provider nodes: registers optional nodes
- Node registry → Model loading & detection: loads models
- Node registry → Sampling runtime: invokes
- Model loading & detection → Model storage & discovery: resolves files
- Sampling runtime → Device & VRAM management: uses devices
- Asset API routes → Asset catalog: ingests and queries