Skip to main content

Resource Management (cross-cutting)

Local services (Ollama, Imagies, talkies, vllm, llamacpp, audiolla, flickies, predictalot, decidealot) share limited hardware. Aigate unloads competing models before starting another request on the same hardware.

Idle auto-unload​

Ollama CUDA uses a q8_0 KV cache by default, controlled by OLLAMA_CUDA_KV_CACHE. Changing this setting to f16 increases cache memory and can push a tight GPU fit into CPU offload or a load failure. Context length and concurrent requests also affect memory usage. Dolphin Phi is available only through its CPU alias; the CUDA uncensored aliases use Qwen and Gemma abliterated models.

Every local service unloads models after a period of inactivity:

ServiceDefault idle timeoutConfigurable via
Ollama (CPU/CUDA)5 minutesOllama's built-in keep_alive
Imagies CPU5 minutesIMAGIES_IDLE_TIMEOUT
Imagies CUDA5 minutesIMAGIES_CUDA_IDLE_TIMEOUT
talkies CPU (ASR + Kokoro TTS)10 minutesTALKIES_MODEL_TTL (wrapper idle sweeper); resource manager also triggers DELETE /api/ps/{model}
talkies CUDA (ASR + Kokoro TTS + Qwen3-TTS)10 minutesTALKIES_CUDA_MODEL_TTL (wrapper idle sweeper); resource manager also triggers DELETE /api/ps/{model}
vllm-cuda10 minutesVLLM_CUDA_MODEL_TTL (wrapper idle sweeper); resource manager also triggers DELETE /api/ps/{model}
vllm (CPU)10 minutesVLLM_MODEL_TTL (wrapper idle sweeper); resource manager also triggers DELETE /api/ps/{model}
llamacpp CUDA600 secondsLLAMACPP_CUDA_MODEL_TTL (wrapper idle sweeper); resource manager also triggers DELETE /api/ps/{model}
llamacpp (CPU)600 secondsLLAMACPP_MODEL_TTL (wrapper idle sweeper); resource manager also triggers DELETE /api/ps/{model}
predictalot (CPU/CUDA)30 minutesPREDICTALOT_MODEL_IDLE_TIMEOUT; resource manager also triggers POST /v1/models/unload
decidealot (CPU/CUDA)600 secondsDECIDEALOT_PROVIDER_IDLE_UNLOAD_SECONDS; resource manager also triggers POST /v1/models/unload

Auto-load on demand​

Models load automatically when a request arrives. Send a chat completion to local-ollama-cuda-qwen3-8b and Ollama pulls/loads it. Send an image generation to local-imagies-cuda-flux-schnell and the Imagies spawns the generation worker with that model. No pre-loading required.

Hardware locks​

A LiteLLM callback (resource_manager.py) allows one job at a time per hardware class:

  • CUDA lock: one CUDA job at a time across LiteLLM's CUDA groups, predictalot MCP tools, and Aigate's Decidealot Laya/Von provider acquisitions.
  • CPU lock: the same for the CPU groups.

The locks live in Redis (hardware_lock.py), so they hold across all of LiteLLM's worker processes (LITELLM_WORKERS, 4 by default). Before v6.0.0 each worker had its own in-memory lock, so up to four jobs could run on the GPU at once.

When a request arrives for a local model:

  1. The resource manager identifies which group it belongs to (e.g. local-imagies-cuda-flux-schnell → cuda-img)
  2. It takes the hardware lock and waits while another job holds it
  3. It unloads all competing groups on the same hardware (e.g. unloads cuda-llm, cuda-stt-talkies, cuda-vllm)
  4. For Imagies (cuda-img / cpu-img) only: it sends PUT /v1/generation/engines/sdcpp/model with the full model ID and blocks until the backend has the requested model loaded. See "Imagies pre-load" below for why.
  5. The request proceeds
  6. On completion (success or failure), the lock is released

The holder refreshes the lock while the job runs. A worker that crashes stops refreshing, and its lock expires after RESOURCE_LOCK_TTL_SECONDS (60 by default), so other requests go ahead. A lock whose release was missed stops being refreshed after RESOURCE_LOCK_MAX_HOLD_SECONDS (86400 by default, the same as the inference timeout). If Redis is down, requests for local models fail instead of running unlocked.

LiteLLM connects to Redis as the litellm ACL user, which can only touch the lock keys (aigate:hwlock:*) and its response cache keys (aigate:cache:*). Its password is LITELLM_REDIS_PASSWORD, falling back to REDIS_PASSWORD. proxq connects as the proxq user with REDIS_PASSWORD, limited to its queue and cache keys. The default user is disabled.

Imagies pre-load (image generation only)​

The first request for an unloaded Imagies model takes about 5 to 20 seconds to load it. Imagies holds the model lock until loading finishes. Any concurrent call inside that window returns 503 another load or generation in progress. LiteLLM's image-gen path reacts to a 503 by retrying (num_retries: 3) and walking the fallback chain. Every retry hits the same lock, every retry 503s, and the entire fallback chain ends with LiteLLM returning HTTP 200 with an EMPTY data array (a router-side bug where image-gen fallback exhaustion masquerades as success).

The resource manager fixes this by issuing the explicit PUT /v1/generation/engines/sdcpp/model blocking call inside the pre-call hook, while the hardware lock is still held. By the time LiteLLM dispatches the actual POST /v1/images/generations, the backend is fully warm and the call succeeds on attempt 1. No 503 storm, no fallback amplification, no empty-data response.

The pre-load is a no-op (~ms) when the model is already loaded. LiteLLM logs preload failures and still dispatches the request and the caller sees the real backend error rather than an indefinitely-blocked request.

Unload mechanisms​

Talkies, Audiolla, Flickies, and Imagies accept POST /v1/unload. Aigate falls back to the old route when a backend lacks the bulk endpoint. A busy backend returns 409 and keeps its models.

ServiceUnload method
OllamaPOST /api/generate {"model": "...", "keep_alive": 0}
Imagies, Talkies, Audiolla, FlickiesPOST /v1/unload stops resident model workers.
vllm-cuda, llamacpp-cudaDELETE /api/ps/{model_id} unloads one model.
predictalotPOST /v1/models/unload (every resident foundation model, plus Torch and CUDA caches). 409 while a forecast runs
decidealotPOST /v1/models/unload (the resident Laya, Von, or CLM provider, plus Torch memory). 409 while a decision runs

Direct-HTTP services in the competing-group unload​

Audiolla, flickies, predictalot, and decidealot expose their own HTTP APIs through nginx rather than routing through LiteLLM completions. No local-audiolla-* / local-flickies-* / local-predictalot-* / local-decidealot-* model aliases exist, so the resource manager never resolves a request to them as own-group. They participate ONLY as COMPETING groups. Every LiteLLM-routed call (ollama / imagies / talkies / vllm / llamacpp) evicts them before allocating VRAM. Without this coupling a LatentSync or Wav2Lip session hoarding 7+ GiB of VRAM would OOM-kill talkies-cuda or ollama-cuda the moment they tried to load a model.

predictalot and decidealot answer 409 to an unload while they are serving a request. The resource manager logs that as a skipped unload and continues, and the busy service frees its model on its own idle timer. Each unload call carries that service's own token (PREDICTALOT_AUTH_TOKEN, DECIDEALOT_AUTH_TOKEN), which LiteLLM gets with the same AIGATE_TOKEN fallback the services use.

predictalot tool calls through LiteLLM's aggregated MCP server (/mcp) take the hardware lock and evict competing groups before inference. Decidealot admission belongs to Aigate's injected provider supervisor instead: Laya/Von take the CUDA or CPU lock per item, and CUDA requests evict the local llama.cpp encoder before allocating memory. CLM embeddings take their lock in LiteLLM, not in the outer MCP tool; hosted Jev does not need a local lock. This also covers mixed Decidealot batches without recursively taking a lock for CLM.

Direct Decidealot REST and MCP requests now use the same per-provider admission as aggregated MCP. Aigate evicts the configured local encoder before CUDA Laya/Von loads; it does not add lifecycle APIs to arbitrary remote embedding URLs. Other direct routes (audiolla, flickies and predictalot) still bypass LiteLLM's admission callback. Decidealot's launcher does not currently evict every other resident GPU service, so unrelated loaded models can still exhaust VRAM; this fix specifically coordinates Qwen and Decidealot. Bypassing Aigate and calling internal inference services directly also bypasses its shared lock.

Operator-facing unload endpoints​

For manual VRAM cleanup (after a bad run, before running a benchmark, or from an oncall runbook) three HTTP endpoints fan out the eviction across every service:

Method + pathEffect
POST /v1/unload/cudaConcurrent fan-out to ollama-cuda, imagies-cuda, talkies-cuda, vllm-cuda, llamacpp-cuda, audiolla-cuda, flickies-cuda, predictalot-cuda, decidealot-cuda.
POST /v1/unload/cpuCPU counterpart, same 9 services.
POST /v1/unloadRuns the CUDA and CPU unloads in sequence.

Response shape:

{
"class": "cuda",
"results": {
"ollama-cuda": {"status": "empty", "unloaded": []},
"audiolla-cuda": {"status": "ok", "unloaded": ["htdemucs"]},
"flickies-cuda": {"status": "ok", "unloaded": ["latentsync-1.5"]},
"decidealot-cuda": {"status": "busy", "http": 409}
}
}

Per-service errors do not fail the whole call (status: "error" on the offending entry, others still evict). A service that is serving a request reports status: "busy" and keeps its model. Auth: same bearer as the rest of aigate. Implemented in mcp/server.py via @mcp.custom_route; nginx proxies /v1/unload/{cuda,cpu,''} to mcp:8000 with a 120s timeout since some unloads take a few seconds to actually release VRAM.

Non-blocking rejection​

The Imagies uses TryLock. If a generation or model swap is in progress, new requests get 503 immediately instead of queuing. Scheduling happens at the LiteLLM layer via the hardware lock, not inside individual services.