How to Set Up Lakera Guard to Block Prompt Injection in Your App
Lakera Guard is a screening API that sits between user input and your model. You send text, it returns a score, and you decide whether to pass, block or flag it.
The setup is one HTTP call. The hard part is deciding what to do with the answer, because a guard that blocks everything unusual will break normal users within a week.
This guide covers the integration, the fields that actually matter, and the deployment patterns that hold up under real traffic. Written against the Guard API as documented in late 2026.
What does Lakera Guard check for?
Three categories, each scored separately: prompt injection (including jailbreaks), harmful content (hate, violence, self-harm), and sensitive data leakage such as PII or secrets in the prompt.
You choose which detectors run per request. Running all of them costs more and adds latency, so most production setups enable injection always and the others depending on the surface. A public chatbot needs harmful-content screening; an internal tool that handles customer PII needs the data detector.
Who this is for: consumer chatbots, support agents that read untrusted documents, and any RAG system where retrieved text can carry instructions. Who it is not for: fully offline deployments, since Guard is a hosted API, and systems where the input is already trusted and typed.
How do you make the first API call?
The endpoint is https://api.lakera.ai/v2/guard. Get a key from the Lakera platform and store it server-side.
import requests
r = requests.post(
"https://api.lakera.ai/v2/guard",
headers={"Authorization": "Bearer " + KEY},
json={"messages": [{"role": "user", "content": text}],
"breakdown": True},
)
The response has flagged (boolean), payload with per-detector results, and a breakdown when you ask for it. Always set breakdown: True while developing; without it you cannot tell which detector fired.
What role does the session ID play?
This is the part that separates a useful deployment from a noisy one. Guard accepts a project_id and a session identifier, and it scores prompt injection in the context of the session, not just the message.
Attackers do not usually send one obviously malicious message. They send three harmless ones to establish a persona, then the payload. A stateless check sees three benign prompts and one slightly odd one. A session-aware check sees the escalation.
Pass a stable session ID per conversation and you get the second behaviour for free. It is one extra string in the request and the largest single quality gain in the whole integration.
How do you choose a threshold?
Guard returns a score between 0 and 1 per detector. Where you cut is a product decision.
- 0.7 and above: block. Catches clear injections with few false positives. A good starting default.
- 0.4 to 0.7: flag. Log it, let it through, and look at the logs weekly. This band is where most false positives live.
- Below 0.4: pass. Do not inspect.
Measure false positives against your own traffic before you tighten anything. A support bot that gets 3% flagged at 0.7 will feel normal. Tightening to 0.5 may push that to 12%, and users will hit blocks on questions that include the word “ignore” or “system”.
What causes false positives in practice?
Real cases from teams running this:
- Quoted text. A user pasting an email that says “ignore the previous invoice” trips the injection detector. It is describing existing content, not attacking you.
- Technical writing. Security researchers and developers writing about jailbreaks get flagged for talking about jailbreaks.
- Non-English input. Detection quality varies by language; some non-English traffic scores higher than equivalent English prompts.
- Long documents. Screening an 80,000-character PDF as a single message produces noisy scores. Chunk it, and screen chunks separately or skip the document body and rely on retrieval-level controls.
Mitigation for cases one and two: allow-list known user roles, or route flagged-but-not-blocked items to a review queue instead of a hard stop.
How much latency does this add?
A Guard call typically lands in the low hundreds of milliseconds, and it runs before your model call, so it is additive to total response time. Two ways to hide it:
- Parallelise with retrieval. Fire Guard and your vector search at the same time. If Guard blocks, you discard the retrieval result. Users see no extra delay.
- Skip screening on repeat input. Identical message text within a session can be cached; users retry the same thing more often than you would guess.
For streaming responses, the guard result must arrive before the first token, so it cannot be parallelised with generation. That makes the Guard call your first-token latency.
What does Guard not protect against?
It screens input. It does not stop a model that has already been given dangerous tool permissions, it does not verify retrieved documents are trustworthy, and it does not sanitise model output.
An agent with database write access is still an agent with database write access if someone talks it into using that access through a legitimate-looking request. Pair input screening with least-privilege tool scopes. Treat Guard as one layer.
How do you test before and after deployment?
Collect 200–500 real messages, label the ones that genuinely attempted manipulation (a few dozen at most), and run them through Guard at several thresholds. Plot blocks against false positives. Ship at the point where false positives are under about 2% for a consumer surface, or higher for an internal one where retries are acceptable.
Re-run that test every time you change the model behind your product. A new model changes how your app responds to partially-blocked prompts, and detection thresholds tuned for the old behaviour may no longer line up.
Quick Summary
- Guard screens text with one HTTP call and returns per-detector scores; enable
breakdownduring development. - Pass a stable session ID, because injection attacks build up over several messages and stateless scoring misses them.
- Block at 0.7, log 0.4–0.7, pass below; measure false positives on real traffic before tightening.
- Quoted text, security writing and long documents are the main false-positive sources, so use a review queue instead of a hard stop for those.
- Guard adds low hundreds of milliseconds; run it in parallel with retrieval, but not with streaming generation.
