LazyModelManager — The Core
Manages a single GGUF model in memory. Key behaviors:
- Lazy loading: Model is loaded only on first inference request (~2-5 seconds)
- Stay resident: Once loaded, stays in memory for fast subsequent calls
- Auto-unload: After N seconds of inactivity, frees memory
- Metal GPU: Uses Apple Silicon GPU automatically if available
Token Streaming
llama.cpp generates tokens one by one via C callback. The streaming bridge:
- llama.cpp generates a token (C/C++ land)
- Callback sends it through a
tokio::mpscchannel - Async Rust receives it and forwards to the client
The Batch Fix
Critical setting: n_batch = n_ctx. Without this, the model crashes on inputs
longer than the default batch size. This ensures the entire context window can be processed
in a single batch.