Most Llama hosts, and most local runners, expose an OpenAI-compatible chat endpoint. That means one piece of code can talk to a hosted model today and a local one tomorrow by changing a URL and a model name. This lesson shows exactly that.
When the API beats a subscription
Llama has no subscription, so the real comparison is against a $20-class plan from another vendor. A metered Llama API wins when your usage is light or spiky, when you want to pick the model per task, or when you are building something for other people. It loses on convenience: there is no polished app, and you are responsible for your own interface. Our subscription vs API comparison works through the break-even.
Hosted: curl
The example below uses OpenRouter's endpoint and the model id meta-llama/llama-4-maverick. Swap in meta-llama/llama-4-scout for a cheaper run. Set OPENROUTER_API_KEY in your environment first.
curl https://openrouter.ai/api/v1/chat/completions \
-H "Authorization: Bearer $OPENROUTER_API_KEY" \
-H "Content-Type: application/json" \
-d '{
"model": "meta-llama/llama-4-maverick",
"max_tokens": 300,
"messages": [
{"role": "system", "content": "You are a concise assistant. Answer in plain English."},
{"role": "user", "content": "Explain open-weight models in three sentences."}
]
}'Look at the usage object in the JSON response. It reports prompt_tokens and completion_tokens. Multiply those by the per-million rates on the price ladder and you have the exact cost of that call.
Do that arithmetic once by hand so the scale sinks in. A call with 60 input tokens and 120 output tokens on Llama 4 Maverick, at $0.20 and $0.696 per million respectively at the time of writing, costs well under a hundredth of a cent. You would need to make tens of thousands of such calls before the bill reached a dollar. That is why hosted Llama is a good fit for experiments and side projects.
Three practical notes for hosted calls. First, the system message counts as input tokens on every request, so keep it short. Second, if you send conversation history, every previous turn is billed again each time; trim it. Third, hosts apply rate limits and may return a 429 status when you exceed them, so add a short retry with backoff rather than hammering the endpoint.
Hosted: Python
import os
from openai import OpenAI
client = OpenAI(
base_url="https://openrouter.ai/api/v1",
api_key=os.environ["OPENROUTER_API_KEY"],
)
resp = client.chat.completions.create(
model="meta-llama/llama-4-maverick",
max_tokens=300,
messages=[
{"role": "system", "content": "You are a concise assistant. Answer in plain English."},
{"role": "user", "content": "Explain open-weight models in three sentences."},
],
)
print(resp.choices[0].message.content)
print(resp.usage) # prompt_tokens, completion_tokensLocal: the same request against Ollama
Ollama serves an OpenAI-compatible endpoint on your machine. Change the base URL and model name, keep everything else. The API key is a placeholder because there is no billing.
# In another terminal: ollama run llama3.3
curl http://localhost:11434/v1/chat/completions \
-H "Content-Type: application/json" \
-d '{
"model": "llama3.3",
"max_tokens": 300,
"messages": [
{"role": "user", "content": "Explain open-weight models in three sentences."}
]
}'That portability is the practical payoff of open weights. Develop against a cheap hosted model, switch to local for private data, or move hosts if one raises prices, all without rewriting your code.
Knowledge check
You ran the curl example and the response reports prompt_tokens: 60 and completion_tokens: 120. Which side of the bill is larger?