Ollama Settings: Keep Prompts Local, GPU & Storage
In this article
- What Does “Keeping Prompts Local” Mean in Ollama?
- How to Set Environment Variables for Ollama
- Configuring GPU Context (Context Window Size)
- Configuring Storage: Where Models Are Saved
- Common Problems and Troubleshooting
- GPU vs. CPU: What Actually Happens
- Safety and Security Considerations
- Frequently Asked Questions
To keep prompts local and configure GPU context and storage in Ollama, set the environment variable OLLAMA_HOST=127.0.0.1 (ensuring it only listens on localhost), adjust OLLAMA_CONTEXT_LENGTH (or use the num_ctx parameter in the API) to control the context window size, and set OLLAMA_MODELS to a custom directory for storage. Below is the full breakdown of how these settings work, how to apply them on different operating systems, and what each variable actually does.
What Does “Keeping Prompts Local” Mean in Ollama?
By default, Ollama runs as a local server on your machine, and all inference (the processing of your prompts) happens on your hardware. However, the server can be configured to listen on a network interface, which would allow other devices on your LAN to send prompts. To keep everything strictly local, you set the host to 127.0.0.1 (loopback) so that only your own computer can connect. This is important for privacy—no other device can send prompts or receive responses.
Additionally, Ollama does not send your prompts to any cloud service by default; the models are downloaded and run locally. But if you have previously changed the host to 0.0.0.0 (all interfaces) or have exposed it via a reverse proxy, you may inadvertently be allowing remote access. Resetting to localhost closes that door.
How to Set Environment Variables for Ollama
Ollama reads several environment variables at startup. You can set them in your shell profile or system settings, depending on your OS.
- Windows: Open System Properties → Environment Variables, add a new user variable (e.g.,
OLLAMA_MODELS) with the desired path, and restart Ollama. - macOS: Add
export OLLAMA_MODELS="/path/to/folder"to your~/.zshrcfile, then runsource ~/.zshrcand restart Ollama. - Linux: Add the same export line to
~/.bashrcor~/.profile, then source it.
After changing any variable, you must restart the Ollama service (or the app) for changes to take effect.
Configuring GPU Context (Context Window Size)
The “context” in Ollama refers to the number of tokens (words or subwords) the model can “see” at once. A larger context allows the model to remember more of the conversation but uses more GPU memory (VRAM). Ollama uses a default context length of 2048 tokens for most models, but you can change it per request or globally.
To set a global default, use the OLLAMA_CONTEXT_LENGTH environment variable (e.g., export OLLAMA_CONTEXT_LENGTH=8192). This affects all models unless overridden.
For per-request control, when using the API, pass the num_ctx parameter in the options object:
curl http://localhost:11434/api/generate -d '{
"model": "llama3",
"prompt": "Hello",
"options": {
"num_ctx": 4096
}
}'
If you set num_ctx too high for your GPU’s VRAM, Ollama will either spill to CPU (slower) or fail with an out-of-memory error. You can check your GPU’s VRAM usage with tools like nvidia-smi (NVIDIA) or radeontop (AMD).
Configuring Storage: Where Models Are Saved
By default, Ollama stores models in ~/.ollama/models on Linux/macOS and C:\Users\<username>\.ollama\models on Windows. To change this, set the OLLAMA_MODELS environment variable to a new directory. For example:
export OLLAMA_MODELS="/mnt/ssd/ollama_models"
This is useful if your system drive is small or if you want to keep models on a separate SSD/HDD. After changing, restart Ollama; existing models will not move automatically—you’ll need to re-download or manually copy them.
Common Problems and Troubleshooting
- Ollama still accessible from other devices: Check your firewall and ensure
OLLAMA_HOSTis set to127.0.0.1. Also, verify withnetstat -an | grep 11434(Linux/macOS) ornetstat -an | findstr 11434(Windows) that it’s listening only on loopback. - Out of memory when increasing context: Lower the context length or use a smaller model. You can also try
OLLAMA_GPU_LAYERSto control how many layers are offloaded to the GPU (not a direct context setting, but affects VRAM usage). - Changes to environment variables not taking effect: Ensure you restarted Ollama completely. On Windows, check that the variable is set for the user, not just the system.
- Model storage path not recognized: Double-check the path exists and is writable. Ollama will create it if it doesn’t, but permissions can block that.
GPU vs. CPU: What Actually Happens
Ollama uses GPU acceleration when available. It offloads as many layers of the model to the GPU as fit in VRAM, and the rest run on the CPU. The OLLAMA_GPU_LAYERS variable lets you force a specific number of layers to GPU (e.g., export OLLAMA_GPU_LAYERS=35). Setting it too high can cause memory errors; setting it to 0 forces CPU-only inference.
Context size directly impacts VRAM usage: a larger context requires more memory for the key-value cache. If you have a 8GB GPU, a 7B model with 2048 context might fit, but 8192 context may not. You can monitor VRAM usage while running a prompt to see how close you are to the limit.
Safety and Security Considerations
Keeping Ollama local is a security measure. If you expose it to the network without authentication, anyone on your LAN could send prompts, potentially consuming your GPU resources or accessing sensitive data you feed it. Always set OLLAMA_HOST=127.0.0.1 unless you have a specific reason to allow remote access, and if you do, use a reverse proxy with authentication (though that is beyond the scope of this article).
Also, be aware that models themselves are not sandboxed; a malicious model could attempt to exploit vulnerabilities, but that is rare. Keep your Ollama version updated to receive security patches.
Frequently Asked Questions
How do I change the context length for a specific model permanently?
You can create a custom Modelfile with the PARAMETER num_ctx 4096 line and then create the model with ollama create mymodel -f Modelfile. Then use mymodel instead of the base model.
Will increasing context length slow down inference?
Yes, generally larger context increases computation time and memory usage, especially if the model has to process more tokens. The slowdown is more pronounced when the context exceeds VRAM and CPU fallback occurs.
Can I store models on an external drive?
Yes, set OLLAMA_MODELS to a path on the external drive. Ensure the drive is mounted before starting Ollama, otherwise it may fail to find models.
Does Ollama send any data to the internet?
Ollama only contacts the internet to download models and check for updates. Your prompts and responses are processed locally and are not sent to any server.
How do I know if my GPU is being used?
Run ollama ps while a model is loaded; it shows the GPU memory usage and which layers are on GPU. Alternatively, use system monitoring tools like nvidia-smi to see GPU utilization.