Learning objectives
After reading this article you will be able to:
- Estimate a model's RAM or VRAM from its parameter count
- Compare the fixed cost of hardware with an API's per-token cost
- Switch between a local model and an API by changing only environment variables
On this page
With tools like Ollama, downloading an open model and chatting with it on your own computer takes a few minutes. The appeal is obvious: no per-token bill and no data leaving the building. On the other side, an API gives you bigger models with one line of code and charges for what you use.
The choice comes down to three questions: does the model that fits your hardware handle the task, does your volume pay for a machine, and can your data leave the house?
What is each option?#
A local LLM is an open-weight model (from families such as Llama, Mistral, Gemma and Qwen) running on a machine you own or rent. The two most used tools:
- Ollama: downloads and runs models with one command (
ollama pull,ollama run), works on macOS, Windows, Linux and Docker, and serves an HTTP API at127.0.0.1:11434. - llama.cpp: an MIT-licensed inference engine in C/C++ that runs models in the GGUF format and has its own HTTP server. It lets you choose how many of the model's layers stay in VRAM and sends the rest to the CPU.
An API is a model hosted by a provider: you send the prompt over HTTPS and pay per input and output token. Billing is covered in how much an LLM API costs, and the concept in what is an LLM.
How much hardware does a local model need?#
Hugging Face's documentation gives the rule of thumb for the weights: loading a model with X billion parameters takes roughly 2 × X GB of memory at 16 bits (bfloat16 or float16), the precision almost every model is trained in. Quantization lowers the precision of the weights so they fit in less memory. In the documentation's own example, a 15.5-billion-parameter model went from 31 GB to about 15 GB at 8 bits and 9.5 GB at 4 bits.
Applying that rule (and the example's ratio for 4 bits), the weights alone take:
| Model size | 16 bits | 8 bits | 4 bits (approx.) |
|---|---|---|---|
| 1 to 3 billion | 2 to 6 GB | 1 to 3 GB | 1 to 2 GB |
| 7 to 8 billion | 14 to 16 GB | 7 to 8 GB | 4 to 5 GB |
| 13 to 14 billion | 26 to 28 GB | 13 to 14 GB | 8 to 9 GB |
| 30 to 34 billion | 60 to 68 GB | 30 to 34 GB | 18 to 21 GB |
| 70 billion | 140 GB | 70 GB | 43 GB |
Add the memory for the context. To generate each token, the model keeps a cache of the previous steps (the KV cache), which grows with the context. In Hugging Face's example, with standard attention, it would reach about 15 GB at 16,000 tokens, half the model's own weight; with multi-query attention, which that same model uses, it drops below 400 MB, and grouped-query attention keeps almost all of that saving. In Ollama, each parallel request adds context: 2,000 tokens with 4 parallel requests become 8,000.
On a GPU, the model has to fit in VRAM. When it doesn't, Ollama and llama.cpp push some layers to the CPU and performance drops (ollama ps shows the split). Ollama supports NVIDIA GPUs, AMD through ROCm, Apple silicon Macs through Metal and others through Vulkan.
How much does each cost?#
An API is a variable cost; local is a fixed one. The numbers below are hypothetical, only to show the math; real prices are on each provider's pricing page, such as OpenAI's pricing.
API (hypothetical example): a small model at $0.50 per million input tokens and $2 per million output tokens. With 1,000 requests a day, each with 1,500 input tokens and 500 output tokens, that is 1.5 million input tokens ($0.75) and 500,000 output tokens ($1) a day: about $52 a month. At 100 requests a day, about $5.
Local (hypothetical example): a $1,500 GPU machine spread over 24 months comes to $62.50 a month. If it draws 300 W running all month, that is 216 kWh; at $0.20 per kWh, another $43 or so. Total: about $105 a month, whether you use it a lot or a little. A rented GPU at $1 an hour costs about $730 a month running nonstop.
With these numbers, your own machine starts paying off above roughly 2,000 requests a day, and the rented GPU only near 14,000. But the comparison only holds if the local model has the quality you need, and your maintenance time never shows up on any invoice. On the API side, prompt caching and smaller models bring the bill down (how to reduce LLM token costs).
What about latency, quality and privacy?#
Latency. A local model has no round trip over the internet and shares no queue with other customers, but it depends on your hardware: fully on the GPU, it responds well; split with the CPU, it slows down. An API adds the network. In chat, users feel the time to the first token, so stream in both cases (streaming AI responses).
Quality. The models that fit on a regular machine are the smallest in the open families. They do well on narrow tasks such as classification, extraction and short summaries; for long reasoning, test with your real questions against a bigger model (which AI model to choose for your chatbot). Quantization has a price too: according to Hugging Face, it trades memory for accuracy and sometimes for speed.
Privacy. With a local model, the prompt never leaves the machine: Ollama's documentation says that when you run locally, they don't see your prompts or data (Ollama's cloud models are a different case). APIs have their own policies: OpenAI, for example, says data sent to its API doesn't train its models without opt-in and keeps abuse monitoring logs for up to 30 days. Read your provider's policy and contract.
Which one takes more maintenance?#
With an API, you maintain the code and the key. With a local model, the list grows:
- Drivers and versions: the GPU driver (Ollama requires a recent NVIDIA driver), CUDA or ROCm, and the tool itself.
- Models: downloading, testing, switching when a better one comes out and checking each one's license.
- Concurrency: Ollama serves a limited number of requests per model at once (
OLLAMA_NUM_PARALLEL), queues the excess and rejects what overflows the queue (OLLAMA_MAX_QUEUE); each parallel request uses memory. - Availability: if the machine goes down, so does the feature. By default, Ollama unloads the model after 5 minutes idle, and the next request waits for it to load (tune it with
OLLAMA_KEEP_ALIVE). - Exposure: Ollama listens only on
127.0.0.1by default. When you open it to the network withOLLAMA_HOST, put an authenticating proxy in front.
How do you switch between local and an API without rewriting code?#
Ollama exposes an OpenAI-compatible endpoint at http://localhost:11434/v1/, and the llama.cpp server offers compatible routes too. The same code works for both; only the base URL, the key and the model change.
pip install openai python-dotenv
ollama pull model-name# Local (Ollama): the SDK requires a key, but Ollama ignores it
LLM_BASE_URL=http://localhost:11434/v1/
LLM_API_KEY=ollama
LLM_MODEL=model-name
# A provider's API (example: OpenAI)
# LLM_BASE_URL=https://api.openai.com/v1
# LLM_API_KEY=your-key
# LLM_MODEL=model-idimport os
from dotenv import load_dotenv
from openai import OpenAI
load_dotenv()
client = OpenAI(
base_url=os.environ["LLM_BASE_URL"],
api_key=os.environ["LLM_API_KEY"],
)
response = client.chat.completions.create(
model=os.environ["LLM_MODEL"],
messages=[
{"role": "system", "content": "Answer in one sentence."},
{"role": "user", "content": "What is model quantization?"},
],
)
print(response.choices[0].message.content)The local model's name comes from Ollama's model library (or the GGUF file you downloaded), and an API model's ID comes from the provider's models page. That way you develop locally and compare against an API by changing three lines of .env.
When should you choose each one?#
Choose a local model when the data can't leave your infrastructure, volume is high and steady, the system has to work offline or the task is narrow and a small model is enough.
Choose an API when you need the best quality available, have low or irregular volume, don't want to maintain GPUs and drivers or need a large context window, which costs memory locally.
There is no universal winner. A common pattern: prototypes and sensitive data on the local model, production with variable traffic on the API. To estimate the bill for a whole product, see how much an AI chatbot costs.
How to do this on Square Cloud#
On Square Cloud, your application (a bot, an API, a website) runs in an isolated, always-on container and can call a model's API over HTTPS. The Python runtime ships prebuilt torch and transformers wheels, but a local model with torch does not fit in the 1 GB Hobby plan, and the plan's RAM is a pool shared by all your applications.
From the Standard plan (R$44.99 a month), the AI Gateway is an OpenAI-compatible API for using the cubic model in your own products. The code in the previous section works with it by changing the .env: LLM_BASE_URL=https://api.squarecloud.app/v2/ai, an account API key with the ai:chat scope in LLM_API_KEY, and LLM_MODEL=cubic. There is no extra cost within the plan's limits: up to 2.5 million tokens a day and up to a 64k-token context on Standard, up to 5 million and 128k on Pro, with up to 4 and 8 concurrent requests. Beyond that, there are AI credit packs, billed in Brazilian reais with a Brazilian tax invoice.
The gateway is text only, supports streaming and function calling and keeps no state: send the history with every request. It does not generate embeddings or do fine-tuning. See the limits and examples on AI Gateway.
AI
Host AI apps in always-on containers
FAQs
Can I run an LLM without a GPU?
Yes. Ollama and llama.cpp run on the CPU with regular RAM, but generation is slower. For the best performance, Ollama's documentation recommends avoiding offloading part of the model from the GPU to the CPU.
How much memory does a 7-billion-parameter model need?
By Hugging Face's rule of thumb, about 2 GB per billion parameters at 16 bits: around 14 GB. Quantized to 4 bits, it comes down to roughly 4 to 5 GB, plus the memory for the context.
Is a local LLM more private than an API?
The prompt never leaves your machine, which simplifies contracts and sensitive data. Major providers also publish API policies: OpenAI, for example, says data sent to its API is not used to train its models unless you opt in, and keeps abuse monitoring logs for up to 30 days.
Does Ollama work with the OpenAI SDK?
Yes. Ollama exposes a compatible endpoint at http://localhost:11434/v1/, and the SDK requires an API key that Ollama ignores. The llama.cpp server also has OpenAI-compatible routes.
More in AI
See all 31 articles- How much does an AI chatbot cost to build and run?Running an AI chatbot costs model tokens, hosting, a database and, with RAG, extra context per message. See how to estimate the cost per conversation.
- How much does a RAG system cost? Embeddings, database and LLMA RAG system has three costs: embeddings, a vector database and the LLM. See the math for 1,000 questions a day, with hypothetical prices, and where the bill grows.
- RAG vs fine-tuning: which to useRAG puts your data in the prompt at question time; fine-tuning changes the model's weights with examples. Compare cost, updates, latency and when to use each.
- pgvector vs a dedicated vector database: which to usepgvector stores vectors in PostgreSQL, next to your data; a dedicated vector database focuses on scale and filtering. Compare indexes, operations and migration.