Now that we've run our completion, let's free the model and tear down the worker.
unloadModel is the symmetric counterpart to loadModel. We pass it the modelId and it frees the loaded model from device memory. With autoClose: true, the underlying worker process is also torn down.
autoClose: true shuts down the worker (frees the OS process); autoClose: false only frees the weights. We can fully unload the session like so:
await unloadModel({ modelId, autoClose: true });
console.log("▸ model unloaded");If you skip this call, the model stays in memory until the process exits. For long-running workloads (servers, desktop apps, mobile sessions) unload the moment the work is done. The model takes hundreds of megabytes, and the GPU or CPU stays warm as long as the worker is alive.
Note: calling
unloadModel()mid-stream would cut tokens off. Always wait for the events loop to finish before unloading.
unloadModel({ modelId, autoClose: true })."▸ model unloaded".$ Run your code to see results
$