Getting started · Unload the model from memory4 / 5
  1. 01
  2. 02
  3. 03
  4. 05

Unload the model from memory

Example on GitHub(packages/sdk/examples/quickstart.ts)

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.

Put it to the test

  1. After the completion loop ends, call unloadModel({ modelId, autoClose: true }).
  2. Log a confirmation line, e.g., "▸ model unloaded".
index.ts

$ Run your code to see results

$