Eight months after ChatGPT, organisations are shipping LLM features into production at speed. Security practice is catching up: Google published its Secure AI Framework (SAIF) in June, OWASP is finalising a Top 10 for LLM applications, and last week leading AI companies made voluntary commitments to the White House that include security testing of their models.
For engineers building on top of these models, the question is practical: how do you build an LLM application that still behaves safely when someone is actively trying to break it?
Principle 1: assume the model is compromised
Prompt injection has no complete fix. Jailbreaks keep appearing. Retrieved documents and web pages can carry instructions. The only safe design assumption is that an attacker can, at least sometimes, control what the model outputs.
That shifts the question from "how do we stop the model being manipulated?" to "what is the worst a manipulated model can do in our system, and how do we limit it?"
Principle 2: keep authority outside the model
The model should never be the component that decides:
- which data a user may see,
- whether an action is permitted, or
- whether a business rule (a discount limit, a refund policy) applies.
Those decisions belong in deterministic code, using the user's identity and permissions. The model proposes; the application decides.
# Anti-pattern: authorisation expressed as prompt text
SYSTEM = "Only show the user their own orders. Never issue refunds over $50."
# Better: the model can only propose; code enforces identity, scope and limits
def handle_tool_call(call, user):
if call.name == "get_order":
order = orders.get(call.args["order_id"], owner=user.id) # scoped by code
return summarise(order)
if call.name == "propose_refund":
amount = Decimal(call.args["amount"])
if amount > REFUND_LIMIT or not refunds.eligible(user, call.args["order_id"]):
return {"status": "needs_human_review"}
return refunds.create_pending(user, call.args["order_id"], amount) # pending, not final
raise ToolNotAllowed(call.name)Principle 3: constrain outputs before anything acts on them
Structured outputs are easier to validate than prose. With the function-calling support OpenAI added in June, models can return structured arguments against a JSON schema. Validate them strictly anyway:
- check types, ranges and allowed values,
- allow-list tools, destinations and operations,
- encode any text before rendering it, and
- reject and log anything unexpected.
Principle 4: control the context
Everything in the prompt can influence the output, and everything in the prompt might leak.
- No secrets in prompts: assume system prompts will be extracted.
- Permission-aware retrieval: only retrieve documents the current user can access.
- Delimit untrusted content (user input, documents, web pages) and label it as data.
- Minimise: include only what the task needs.
Principle 5: limit blast radius
- Give tools the narrowest permissions possible, ideally the user's own.
- Require human confirmation for actions with side effects, showing exactly what will happen.
- Make actions reversible where possible (drafts, pending states, cancellation windows).
- Apply rate limits and budgets per user, per session and per feature.
Principle 6: observe everything
Log prompts, retrieved context references, model outputs, tool calls and decisions, with appropriate privacy controls. Build dashboards for unusual patterns: spikes in refusals, tool denials, repeated injection-like inputs or cost anomalies. Keep a kill switch to disable a feature or tool quickly.
A reference architecture
flowchart TB
U[User] --> IN[Input controls<br/>auth, rate limits, size,<br/>moderation]
IN --> CTX[Context assembly<br/>permission-aware retrieval,<br/>delimited untrusted text,<br/>no secrets]
CTX --> LLM[LLM]
LLM --> OUT[Output controls<br/>schema validation,<br/>allow-lists, encoding]
OUT --> ACT{Action requested?}
ACT -->|no| RESP[Response to user]
ACT -->|yes| AZ[Authorisation & rules in code<br/>+ human approval if needed]
AZ --> TOOLS[Scoped tools]
TOOLS --> RESP
IN -.-> LOG[(Logs, metrics,<br/>kill switch)]
LLM -.-> LOG
AZ -.-> LOGPrinciple 7: test adversarially, continuously
- Build an adversarial test suite: prompt injection, jailbreaks, data exfiltration attempts, tool misuse and cross-user access.
- Run it in CI whenever prompts, models, retrieval or tools change.
- Commission independent testing before launching high-risk features.
Mapping to SAIF
Google's SAIF describes six core elements, including expanding strong security foundations to the AI ecosystem, extending detection and response to AI, automating defences, harmonising platform-level controls, adapting controls with faster feedback loops, and contextualising AI risks in business processes. The principles above are an engineering interpretation of the same ideas: build on existing security foundations, add AI-specific controls, and close the loop with monitoring and testing.
The bottom line
Secure LLM engineering is less about clever prompts and more about architecture. Assume the model can be turned against you, keep authority in code, constrain what comes out, limit what actions can follow, and watch everything. Applications built that way can survive attackers, even when the model does not.
Our agentic AI security team builds LLM applications on these principles, and our application security testing validates them adversarially.