Aller au contenu

Leçon 20 · Serving and Safety

Serving Llama to Real Users

Choose a serving stack for Llama, run vLLM, protect the API, plan for concurrency and context, add limits, queues, monitoring and safe fallbacks.

A model that runs on your laptop is not yet a service. Real users send requests at the same time, send odd input and expect uptime. This lesson covers the choices around a Llama server.

Choose a serving path

Option Often a good fit for
Ollama Development, personal and small team use
llama.cpp server Efficient GGUF serving on CPU or mixed hardware
vLLM and similar engines GPU servers with many concurrent requests
Managed hosting provider No hardware operations, under the provider's terms

For a managed provider, check data handling, region, retention, model version policy and license obligations.

vLLM in brief

vLLM is an open source inference engine with an OpenAI-compatible server. A basic launch looks like this (run it on a GPU machine that meets its requirements):

Terminal
export VLLM_API_KEY="a-long-random-secret"
vllm serve meta-llama/Llama-3.2-3B-Instruct \
  --dtype auto \
  --host 127.0.0.1 --port 8000

Its documentation shows --api-key (or VLLM_API_KEY) and warns that the key protects only certain URL path prefixes, so you should not rely on it alone. Put a reverse proxy or gateway with real authentication and TLS in front, and expose only the paths you need.

For tool calling, set the matching parser and template, as shown in tool calling.

Concurrency and batching

Serving engines can batch many requests, so the GPU does useful work for several users at once. Throughput rises, but each user may wait longer when the server is busy. Memory for the KV cache limits how many long sequences fit. See serving, batching and speculative decoding.

Do capacity math with your own measurements.

Python A simple capacity estimate

Press Run (or Ctrl + Enter) to see the result here.

Program input · what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

The safety factor covers peaks and is a choice, not a law. Load-test with realistic prompt and reply lengths, because long prompts change the picture a lot.

Protect the service

  • Authentication and TLS at the edge; never expose a raw model port to the internet.
  • Limits: maximum input size, maximum output tokens, request rate and per-user quotas.
  • Timeouts and a queue with a maximum length. Reject politely when it is full instead of crashing.
  • Isolation: run the server with a restricted account and no unnecessary access to files or the network.
  • Updates: patch the runtime and verify model files with checksums.
Python Stand-in: a token-bucket rate limiter

Press Run (or Ctrl + Enter) to see the result here.

Program input · what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

A real service would keep one bucket per user or key, and could make the cost depend on the number of tokens requested.

Health, logging and metrics

Track request count, errors, queue length, time to first token, tokens per second, memory use and rejected requests. Health checks should confirm that the model can actually answer, not only that a port is open.

Decide what you log. Prompts often contain personal or confidential data. Log as little as possible, protect the logs, set a retention time and tell users what you keep. Check your legal duties.

Versioning and rollouts

Treat the model file, quantization, chat template, runtime and prompt as one release. Pin each. Change them through the same test and rollout process as code. Keep a rollback.

Fallbacks

Plan for failure: a smaller backup model, a cached answer, a clear "try again later" message or a human queue. Do not silently send private data to a different provider during a fallback unless your users and policies allow it.

A sensible first architecture

A simple and safe starting design has four parts. Your application receives the user's request and checks who the user is. It applies limits and builds the prompt. It calls the model server over a private network. Then it validates the reply before showing it. The browser or mobile app never talks to the model server directly, so users cannot change the system message, remove limits or pick a different model.

Load testing

Before launch, send realistic traffic to a test copy of the service. Mix short and long prompts, add a burst of simultaneous users and let it run long enough to see memory growth and heat effects. Watch the time to first token at the slow end of the distribution, such as the 95th percentile, because averages hide the users who wait the longest. Find the point where errors or delays become unacceptable, and set your limits below it.

Cost and capacity

A GPU server costs money while it is idle. Compare the cost of your own server, including the time to operate it, with a hosted provider for your expected load. Use current prices from the official pages of the services you consider. Review the comparison when traffic changes.

Write a runbook

Write short instructions for the person on call: how to check health, restart the service, roll back a release, block an abusive user and switch to the fallback. Practice these steps once before launch. Instructions that nobody has tried often fail at the worst moment.

Plan updates before you need them. Security fixes arrive for runtimes, drivers and libraries. Keep a test copy of the service where you can try an update with your test set and load test, and then move it to production with the same rollback plan as any other release.

Start small. Release to a small group first. Their real traffic shows which limits, timeouts and messages need adjustment before more people depend on the service.

Common mistakes

Do not rely on a model server's API key flag as your only protection. Put the server behind proper network controls and authentication.

Check your understanding

Quick check · 3 questions

of 3 answered You scored / 3 · perfect!

  1. 1. What should sit between the internet and a raw model server?

    The raw server should not be your security boundary.
  2. 2. Why limit maximum output tokens?

    Unlimited replies can tie up capacity.
  3. 3. Which should be released and rolled back together?

    Any of them can change behavior, so version them as a unit.

Summary

  • Pick a serving stack for your load and operations skills.
  • Protect the API with a gateway, limits, timeouts and queues.
  • Load-test with realistic requests and monitor useful metrics.
  • Version everything together and plan fallbacks.

Next: Llama Guard and Prompt Guard

Votre progression est enregistrée dans ce navigateur. Aucun compte nécessaire.