Getting started · Unload the model from memory4 / 7

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.

Questions

Question 1 of 2

Which of the following best describes the difference between autoClose: true and autoClose: false?

Question 2 of 2

What happens if you call unloadModel in the middle of a completion stream?

index.ts
Loading editor...

Run your code, check your answer, or ask a question. It all shows up here.