Llama API: how to use Llama from your code
Meta closed its own hosted Llama API in July 2026, but Llama is still easy to call from code. Here is how to use it through OpenRouter or your own local server.
The short answer
To use Llama from your own code in 2026, you have two practical routes: call it through a cloud provider that hosts Llama models, or run the model yourself and use a local server. Meta itself no longer offers a hosted API for Llama. This guide explains what changed, then shows working examples using OpenRouter, a service that gives one API for many models, including Llama 4 Maverick and Scout.
What happened to Meta's Llama API
After the Llama 4 launch, Meta ran a hosted Llama API as a public preview for developers. Meta shut that preview down on July 6, 2026. Meta's newer developer offering, the Meta Model API, serves Meta's Muse models such as Muse Wonder, not Llama. Meta's own help page puts it directly: "Llama models aren't run for you through Model API." In other words, Llama is now something you download and run, or use through other providers. See Meta's note on Llama and Model API.
Muse models are not available in our chat, and their pricing is outside the scope of this page. The rest of this guide is about Llama.
Your options at a glance
- A model router such as OpenRouter. One API key, one OpenAI-style endpoint, many models. Good for trying several Llama versions and switching between them.
- A cloud provider that hosts Llama directly. Many inference companies and big cloud platforms offer Llama models. Compare prices, rate limits and data policies before choosing.
- Your own server or computer. Run Llama with LM Studio, Ollama or llama.cpp and call it over a local, OpenAI-compatible API. No per-token cost, full control of data. See run Llama locally.
Calling Llama through OpenRouter
OpenRouter's API follows the same format as OpenAI's chat completions API, so most existing code and libraries work by changing the base address and model name. Create an account and an API key on OpenRouter, store the key in an environment variable named OPENROUTER_API_KEY, and never put it in client-side code. The steps are in the OpenRouter quickstart.
curl
curl https://openrouter.ai/api/v1/chat/completions \
-H "Content-Type: application/json" \
-H "Authorization: Bearer $OPENROUTER_API_KEY" \
-d '{
"model": "meta-llama/llama-4-maverick",
"messages": [
{"role": "user", "content": "Write a haiku about open-weight models."}
]
}'
Python, with the OpenAI SDK
import os
from openai import OpenAI
client = OpenAI(
base_url="https://openrouter.ai/api/v1",
api_key=os.environ["OPENROUTER_API_KEY"],
)
response = client.chat.completions.create(
model="meta-llama/llama-4-scout",
messages=[{"role": "user", "content": "Summarize the Llama license in three bullets."}],
)
print(response.choices[0].message.content)
JavaScript, with fetch
const res = await fetch("https://openrouter.ai/api/v1/chat/completions", {
method: "POST",
headers: {
"Content-Type": "application/json",
"Authorization": `Bearer ${process.env.OPENROUTER_API_KEY}`,
},
body: JSON.stringify({
model: "meta-llama/llama-3.3-70b-instruct",
messages: [{ role: "user", content: "Give me three names for a bakery." }],
}),
});
const data = await res.json();
console.log(data.choices[0].message.content);
Run JavaScript like this on a server, not in a web page, so the key stays private. Streaming is also supported if you want the answer to appear word by word.
Sending an image to Llama 4
Llama 4 Maverick and Scout accept images. In the OpenAI-style format, the message content becomes a list with a text part and an image part:
{
"model": "meta-llama/llama-4-maverick",
"messages": [{
"role": "user",
"content": [
{"type": "text", "text": "What is in this picture?"},
{"type": "image_url", "image_url": {"url": "https://example.com/photo.jpg"}}
]
}]
}
Llama 3.3 70B and Llama 3.1 8B are text only, so send images to a Llama 4 model.
Llama model names and prices
These are the Llama models OpenRouter lists, with their model names, context windows and prices per million tokens. All prices are OpenRouter list price, Oct 2026; they can change, so check before you build a budget around them.
| Model | Model name in the API | Context (tokens) | Input / output per 1M tokens |
|---|---|---|---|
| Llama 4 Maverick | meta-llama/llama-4-maverick | 524,288 | $0.1875 / $0.6525 |
| Llama 4 Scout | meta-llama/llama-4-scout | 1,310,720 | $0.10 / $0.30 |
| Llama 3.3 70B | meta-llama/llama-3.3-70b-instruct | 131,072 | $0.10 / $0.32 |
| Llama 3.1 70B | meta-llama/llama-3.1-70b-instruct | 131,072 | $0.40 / $0.40 |
| Llama 3.1 8B | meta-llama/llama-3.1-8b-instruct | 131,072 | $0.05 / $0.08 |
| Llama 3.2 3B | meta-llama/llama-3.2-3b-instruct | 131,072 | $0.0509 / $0.335 |
| Llama 3.2 1B | meta-llama/llama-3.2-1b-instruct | 60,000 | $0.027 / $0.201 |
| Llama Guard 4 12B | meta-llama/llama-guard-4-12b | 163,840 | $0.18 / $0.18 |
Note that the context windows here are what OpenRouter's providers serve, which can be smaller than what Meta's model cards describe. Meta lists 10 million tokens for Scout and 1 million for Maverick.
Using a local Llama as an API
If you run Llama yourself, you can point the same code at your own machine. LM Studio's server uses http://localhost:1234/v1 as the base address, and llama.cpp's llama serve command also starts an OpenAI-compatible server; see the LM Studio server docs and the llama.cpp README. Ollama has its own API at http://localhost:11434/api/chat. Use the model name your local tool shows instead of the OpenRouter name.
Tips for building with Llama
- Start with Maverick or Scout for general tasks and images, and try Llama 3.1 8B where speed and cost matter more than depth.
- Set a system prompt describing the role, tone and output format you need.
- Add a safety layer for public apps. Llama Guard 4 can classify prompts and answers.
- Follow the license. Products built with Llama must show "Built with Llama" and follow Meta's acceptable use policy. Read our license guide.
- Keep knowledge cut-offs in mind. Llama 4 knows events up to August 2024; give the model fresh facts in the prompt when needed.
Common problems
- Authorization errors usually mean the key is missing, mistyped or not loaded into the environment where your code runs.
- "Model not found" usually means a typo in the model name. Copy it exactly from the table above, including the
meta-llama/prefix. - Image ignored or rejected happens when you send a picture to a text-only model. Switch to Maverick or Scout.
- Answers cut off mean the reply hit the output limit. Ask for a shorter answer or raise the maximum tokens setting.
FAQ
Does Meta still offer a Llama API?
No. Meta closed its hosted Llama API preview on July 6, 2026. Its Meta Model API serves Muse models, not Llama.
Is there a free Llama API?
Running Llama locally costs nothing per token. Hosted APIs charge per token, though small models such as Llama 3.1 8B are inexpensive.
Does Llama.Online have an API?
No. Llama.Online is a browser chat only. Use a cloud provider or a local server for code.
Can I use the OpenAI SDK with Llama?
Yes. Point the SDK at an OpenAI-compatible endpoint, such as OpenRouter or a local LM Studio server, and use a Llama model name.
Ask Llama your first question
No account and nothing to install. Open the chat, type a message and see what Llama can do.
Open Llama chat
