← Code Recipes
Language Models

Meta Llama Free code examples in Python and JavaScript

Open-weight models you host yourself — the default when the data cannot leave.

C# Python Java JavaScript

WHAT THIS PAGE COVERS

Running the weights locally, then the same code against a hosted server.

Model llama3.3
Package openai (there is no Llama SDK)
Checked

Model ids change. If a sample returns a 404 on the model, that is the first thing to check against the official docs.

Install

There is no Llama SDK and no Llama API key, because Llama is not a service. Decide where the weights run first; everything after that is the OpenAI client with a different base URL.

# Fastest path: run it on your own machine.
#   https://ollama.com/download
ollama pull llama3.3
ollama serve                      # serves an OpenAI-compatible API on :11434

# The client is just the OpenAI SDK.
pip install openai
Calling it locally

The client requires an api_key and the local server ignores it. Name the constant rather than inlining a string — it stops the next reader wondering whether it is a real credential.

from openai import OpenAI

# Required by the client, ignored by Ollama. Not a credential.
LOCAL_KEY = "unused"

client = OpenAI(
    base_url="http://localhost:11434/v1",
    api_key=LOCAL_KEY,
)

response = client.chat.completions.create(
    model="llama3.3",                      # whatever you `ollama pull`ed
    messages=[
        {"role": "system", "content": "You are a concise technical assistant."},
        {"role": "user", "content": "Explain a database index in two sentences."},
    ],
)

print(response.choices[0].message.content)
The same prompt, hosted

When local hardware is not enough, the code does not change — only where it points. That portability is what you buy by choosing open weights, and it is worth keeping the switch in one function so it stays true.

import os
from openai import OpenAI

LOCAL_KEY = "unused"      # required by the client, ignored by Ollama

def llama_client():
    # One place decides where the weights run. Everything downstream
    # calls chat.completions.create with identical arguments.
    where = os.environ.get("LLAMA_HOST", "local")

    if where == "groq":
        return OpenAI(api_key=os.environ["GROQ_API_KEY"],
                      base_url="https://api.groq.com/openai/v1"), \
               "meta-llama/llama-4-scout-17b-16e-instruct"

    if where == "together":
        return OpenAI(api_key=os.environ["TOGETHER_API_KEY"],
                      base_url="https://api.together.xyz/v1"), \
               "meta-llama/Llama-3.3-70B-Instruct-Turbo"

    return OpenAI(base_url="http://localhost:11434/v1",
                  api_key=LOCAL_KEY), "llama3.3"

client, model = llama_client()

response = client.chat.completions.create(
    model=model,
    messages=[{"role": "user", "content": "Explain a database index."}],
)
print(response.choices[0].message.content)

# Model ids differ per host — each one names the same weights its own
# way. That is the one thing you cannot make portable.
What hosting weights actually costs you

The honest trade. None of this applies to a hosted-only model, and all of it is yours the moment you run the weights.

# 1. QUANTISATION IS A QUALITY DIAL, NOT A FREE SPEEDUP.
#    ollama pull llama3.3:70b-instruct-q4_K_M runs on far less VRAM and
#    is measurably worse at reasoning than q8. Benchmark on YOUR task
#    before accepting a smaller quant — the loss is uneven, not uniform.

# 2. CONTEXT WINDOW IS SET BY THE SERVER, NOT THE MODEL.
#    Ollama defaults to a fraction of what the weights support and
#    SILENTLY TRUNCATES the prompt. Long inputs come back with the
#    beginning missing and no error anywhere.
#    Set num_ctx explicitly (Modelfile, or the options field).

# 3. NO VENDOR MEANS NO RATE LIMIT AND NO CAPACITY EITHER.
#    One GPU serves a handful of concurrent requests. vLLM's continuous
#    batching is the difference between a demo and a service.

# 4. THE LICENCE IS NOT MIT.
#    Llama ships under Meta's community licence, which has conditions
#    on very large deployments and on naming. Read it before shipping.
Install

No Llama SDK and no Llama API key — Llama is weights, not a service. Choose where they run; the client is the OpenAI SDK either way.

# Fastest path: run it locally.
#   https://ollama.com/download
ollama pull llama3.3
ollama serve                      # OpenAI-compatible API on :11434

npm install openai
Calling it locally

apiKey is required by the client and ignored by the server. Nothing leaves the machine.

import OpenAI from "openai";

// Required by the client, ignored by Ollama. Not a credential.
const LOCAL_KEY = "unused";

const client = new OpenAI({
  baseURL: "http://localhost:11434/v1",
  apiKey: LOCAL_KEY,
});

const response = await client.chat.completions.create({
  model: "llama3.3",                     // whatever you `ollama pull`ed
  messages: [
    { role: "system", content: "You are a concise technical assistant." },
    { role: "user", content: "Explain a database index in two sentences." },
  ],
});

console.log(response.choices[0].message.content);
The same prompt, hosted

Only the base URL and the model id move. Keep the switch in one function so the local and hosted paths cannot drift.

import OpenAI from "openai";

const LOCAL_KEY = "unused";   // required by the client, ignored by Ollama

function llamaClient() {
  switch (process.env.LLAMA_HOST) {
    case "groq":
      return [new OpenAI({
        apiKey: process.env.GROQ_API_KEY,
        baseURL: "https://api.groq.com/openai/v1",
      }), "meta-llama/llama-4-scout-17b-16e-instruct"];

    case "together":
      return [new OpenAI({
        apiKey: process.env.TOGETHER_API_KEY,
        baseURL: "https://api.together.xyz/v1",
      }), "meta-llama/Llama-3.3-70B-Instruct-Turbo"];

    default:
      return [new OpenAI({
        baseURL: "http://localhost:11434/v1",
        apiKey: LOCAL_KEY,
      }), "llama3.3"];
  }
}

const [client, model] = llamaClient();

const response = await client.chat.completions.create({
  model,
  messages: [{ role: "user", content: "Explain a database index." }],
});
console.log(response.choices[0].message.content);
What hosting weights actually costs you

The honest trade, none of which applies to a hosted-only model.

// 1. QUANTISATION IS A QUALITY DIAL. A q4 build runs on far less VRAM
//    and is measurably worse at reasoning than q8. Benchmark on your
//    own task — the loss is uneven, not uniform.

// 2. CONTEXT IS SET BY THE SERVER, NOT THE MODEL. Ollama defaults to a
//    fraction of what the weights support and SILENTLY TRUNCATES the
//    prompt: long inputs come back missing their beginning, with no
//    error. Set num_ctx explicitly.

// 3. NO VENDOR MEANS NO CAPACITY EITHER. One GPU serves a handful of
//    concurrent requests; vLLM's continuous batching is the difference
//    between a demo and a service.

// 4. THE LICENCE IS NOT MIT. Meta's community licence has conditions on
//    very large deployments and on naming. Read it before shipping.

QUESTIONS

Which languages are the Meta Llama examples in?

Python and JavaScript. Every sample runs as pasted and reads its credentials from an environment variable — no key is ever written into the code.

Which package do I install for Meta Llama?

The samples use openai (there is no Llama SDK). The install step on each language tab gives the exact command.

Which Meta Llama model do these examples use?

llama3.3, checked against the official documentation on 25 September 2026. Model ids change often; if a call returns a model-not-found error, check the current list in the official docs.

Where is the official Meta Llama documentation?

https://huggingface.co/meta-llama — it is linked at the bottom of this page, and it is the authority whenever it disagrees with a sample here.

ALSO IN LANGUAGE MODELS

The authority

Official Meta Llama documentation

Model names, pricing and rate limits change faster than any page here can. When this page and the official docs disagree, the official docs are right.

Read the docs ↗

https://huggingface.co/meta-llama

New drops, every week

Free tools & Claude templates, landing regularly.

We keep shipping new free tools, code recipes and Claude Code templates. Follow @freeyantra on Instagram or join the newsletter so you never miss one.

Follow @freeyantra

or get them by email

Double opt-in. Unsubscribe any time. Privacy