Run AI on a VPS / Call it via API
Call Ollama on your VPS from your apps with the OpenAI-compatible API
Ollama has an endpoint that accepts the same format as the OpenAI API, so programs written for OpenAI often work just by changing the address to your server. This page shows how to call Ollama on your VPS from your own PC. First complete the Ollama install steps and download a model.
1Connect from your PC through a tunnel
Keep Ollama closed to the outside and borrow an SSH connection. Keep this running on your PC:
ssh -N -L 11434:localhost:11434 USER@SERVER_IPNow http://localhost:11434 on your PC reaches Ollama on the VPS.
2Try it with curl
Send to /v1/chat/completions, the same format as OpenAI. Use the name of a model you have downloaded.
curl http://localhost:11434/v1/chat/completions \
-H "Content-Type: application/json" \
-d '{
"model": "llama3.2:3b",
"messages": [{ "role": "user", "content": "What is a VPS? Answer in one sentence." }]
}'/v1/models (downloaded models), /v1/embeddings (turning text into numbers for search and questions over your documents) and /v1/completions also work.
3Call it from Python (with the OpenAI library)
Install the official OpenAI library.
pip install openaiJust change base_url to the tunnel. api_key is a required argument, but Ollama ignores its value.
from openai import OpenAI
client = OpenAI(
base_url="http://localhost:11434/v1/",
api_key="ollama", # required argument, ignored by Ollama
)
reply = client.chat.completions.create(
model="llama3.2:3b",
messages=[{"role": "user", "content": "What is a VPS? Answer in one sentence."}],
)
print(reply.choices[0].message.content)4Using it with existing apps
- Change the address: set the app's "API base URL" to
http://localhost:11434/v1/. - Apps with a fixed model name: if the app expects a name like
gpt-3.5-turbo, give a downloaded model that alias.ollama cp llama3.2:3b gpt-3.5-turbo - Changing context length: the OpenAI format has no setting for it. Create an aliased model from a Modelfile with
num_ctxand call that. Longer context uses more memory. - Unsupported features:
tool_choice,logprobs, image URLs and others are not supported. Unsupported values return a 400 error instead of being ignored.
5Concurrent use and reading the logs
By default Ollama processes one request at a time per model; others wait. To process more at once, set OLLAMA_NUM_PARALLEL. Memory needed grows with the number of parallel requests and the context length (Ollama FAQ).
sudo systemctl edit ollama.service
# In the editor, add these two lines and save
[Service]
Environment="OLLAMA_NUM_PARALLEL=2"
# Apply
sudo systemctl daemon-reload
sudo systemctl restart ollamaIf calls from your app don't come back as expected, read Ollama's log on the VPS.
journalctl -e -u ollamaWith Ollama's own API (/api/chat and so on), adding "keep_alive": 0 unloads the model from memory right after the reply. Useful when you call it only occasionally and want memory free.
FAQ
What should I use as the API key?
OpenAI libraries require a value, but Ollama ignores it. The official examples use 'ollama'. Anything works, and it is not authentication.
Are all OpenAI features available?
Some are not. For example, tool_choice, logprobs and image URLs (Base64 images work) are not supported. Unsupported values are not ignored; they return a 400 error.
Can I expose it to the internet?
We do not recommend it. Ollama has no authentication. Connect from your PC through an SSH tunnel (step 5 of the Ollama install guide).
How many people can use it at once?
By default a model processes one request at a time and the rest wait in line (per the official docs). OLLAMA_NUM_PARALLEL raises this, but uses more memory. We do not measure how many users it handles comfortably.
Plans that suit this use
The steps use Llama 3.2 3B. As an API, the memory needed is still decided by the model size.
Handling several requests at once adds memory for holding conversations. For use from apps, choose a plan with headroom.
Llama 3.2 3B
~3.5 GB memory · ollama run llama3.2:3b
- KAGOYA CLOUD VPS4 GB · ¥1,760/mo
- ABLENET VPS4 GB · ¥2,167/mo
- ConoHa VPS4 GB · ¥2,189/mo
Llama 3.1 8B
~6.4 GB memory · ollama run llama3.1:8b
- ABLENET VPS8 GB · ¥2,936/mo
- KAGOYA CLOUD VPS8 GB · ¥3,410/mo
- ConoHa VPS12 GB · ¥4,389/mo
Cheapest first. This compares fit and monthly price only, not speed. Affiliate programs have no effect on the order.
Based on Ollama's official documentation (OpenAI compatibility). Support may change between versions.