OverpayingForAIPricing desk

Lesson 7 of 8 · 11 min read · Beginner → Intermediate

API quickstart

Call a Llama model through an OpenAI-compatible hosted endpoint with curl and Python, then run the same request against a local runner. Understand tokens before you pay for them.

In this lesson you will

  • Decide when a per-token API beats a flat subscription for your usage
  • Send a chat completion to a hosted Llama model and read the token usage in the response
  • Point the same code at a local runner with no per-token cost

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.

Tokens and the context windowA token is roughly ¾ of an English word. The context window is the maximum tokens the model can hold at once — everything you send plus everything it writes.System prompt + instructionsConversation history + documentsNew answerfreeWhy it costs moneyEvery turn re-sends the whole history. A 40-message chat with a pasted PDF bills that PDF 40 times unless the provider caches it.“1M context” is a ceiling, not a target. Filling it on every request is the fastest way to overpay.rule of thumb: 1,000 tokens ≈ 750 words ≈ 1.5 pages
Figure 1.You are billed on tokens, not words or requests. Input tokens are everything you send, including the system prompt and history. Output tokens are the reply.

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.

bashHosted API via OpenRouter (OpenAI-compatible endpoint).
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

pythonThe same request with the OpenAI Python SDK pointed at OpenRouter.
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_tokens

Local: 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.

bashLocal runner via Ollama's OpenAI-compatible endpoint. Port and model tag are the runner's defaults; check its documentation.
# 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?

Lesson FAQ

Does Meta offer an official Llama API?

Llama is designed to be served by hosts and by you. Hosts such as OpenRouter and cloud platforms expose OpenAI-compatible endpoints, which is what this lesson uses. Check Meta's official Llama site for any first-party offerings.

Can I use the OpenAI SDK with Llama?

Yes. Point the SDK's base_url at a compatible host or a local runner and use that host's model id. The rest of the code stays the same.

Does Llama support prompt caching?

Caching is a host feature rather than a model feature. Some hosts offer it for some models. Check your host's documentation and pricing page.

Finished reading?

Mark it done to track your progress through the course.

Compare, calculate, decide — for Llama

If our calculators helped you cut down on hidden AI wallet leaks, consider buying us a coffee. A tiny fraction of your savings keeps our pricing indexes updated daily.

Not sure which AI is cheapest for your use case? Find out in 30 seconds — no signup required.

AI cost intelligence

Stop overpaying for AI tools

Join the OverpayingForAI list for pricing updates, cheaper alternatives, and practical buying guidance.

Now tracking 50+ AI tools, models, platforms, subscriptions, coding tools, and automation products.

We use your email only for OverpayingForAI updates. Unsubscribe anytime.