answrsnow
AI Infrastructure · Explainer

How do you prevent prompt injection at the infrastructure layer in 2027?

By Kory White · Updated Nov 2026 · 9 min read
Server racks lit in blue and yellow inside a computer server room
Direct answer

You do not prevent prompt injection outright; you contain it. Assume the model will sometimes follow instructions it should not, then build the infrastructure around it so that a hijacked model can do little: give it least-privilege tools, keep untrusted content apart from trusted instructions, run tool execution in a sandbox with controlled network egress, require a human to approve high-impact actions, validate what comes out, and log everything so you can see it happen.

The reason is structural. The UK National Cyber Security Centre says large language models do not enforce a security boundary between instructions and data inside a prompt, and that the risk may never be fully mitigated the way SQL injection can be. OWASP's entry for prompt injection (LLM01 in its Top 10 for LLM Applications) likewise states that no foolproof prevention method is known.

So this page is about the layers that sit outside the model. No source used here publishes a percentage showing how much any single layer reduces successful attacks, so none is given. Filters and classifiers are one layer among many, not the whole defense.

Why this is an infrastructure problem

Prompt injection is when text that reaches a model changes its behavior in ways the builder did not intend. OWASP separates two forms: direct, where a user's own input does it, and indirect, where the model ingests external content such as a web page or file that alters its behavior. MITRE ATLAS catalogs the same split as technique AML.T0051 (LLM Prompt Injection) with Direct and Indirect sub-techniques, and rates the technique as realized.

NIST AI 100-2 E2025 explains why the indirect form matters for infrastructure: because generative models combine the data and instruction channels, an attacker can use the data channel to affect the system, and the person harmed is often the legitimate user. It lists three categories of attacker goal for indirect injection: availability, integrity and privacy compromise. For agents that call tools, NIST adds that hijacking can lead to code execution or data exfiltration from the environment the agent runs in.

That is the lesson of the whole field: the model is not a reliable gatekeeper, so the things it is allowed to touch must be. The UK NCSC's December 8, 2025 post calls prompt injection a residual risk that cannot be fully mitigated with a product or appliance, and recommends deterministic safeguards outside the LLM that constrain what the system can do.

Give the model least privilege

Start with what a compromised model could reach. OWASP lists privilege control and least-privilege access as a core mitigation: restrict the model's access to the minimum necessary for its intended operations. The joint guidance Careful Adoption of Agentic AI Services, published May 1, 2026 by agencies including CISA, the NSA and the UK NCSC, calls strict adherence to least privilege critical because the privileges assigned to an agent directly determine the risk it can introduce.

The NCSC gives a useful rule of thumb: when an LLM processes information from a party, its privileges should drop to those of that party. An assistant reading mail from unknown senders should not hold tools that act with an employee's full authority. Microsoft's design pattern for indirect prompt injection adds short-lived privileges: grant them only when needed and remove them after each use.

Keep untrusted content away from trusted instructions

OWASP recommends segregating and clearly identifying external content so it has less influence on prompts. The NCSC likewise suggests marking external content as data. NIST lists the same family of input-processing methods, including filtering instructions out of third-party data and spotlighting, which means designing prompts so the model can separate trusted from untrusted data. Google describes a related technique it calls security thought reinforcement, which wraps prompt content in security instructions reminding the model to finish the user's task and disregard embedded adversarial commands.

Marking and wrapping make an attack harder, not impossible. The NCSC is explicit that this is a way to make injection harder, not a security boundary, so pair it with the controls that follow.

The stronger move is architectural isolation. NIST notes that designers can assume injection is possible whenever a model is exposed to untrusted input, and respond by using multiple models with different permissions or letting models touch untrustworthy data sources only through well-defined interfaces. Microsoft's pattern calls this information flow control: policy-based isolation of untrusted content using metadata and quarantined inference, so untrusted data cannot steer critical planning. In practice that means the component that reads the web page or inbox has no tools, and the component that holds the tools never sees raw untrusted text.

Anthropic's Claude Code documentation shows one concrete form: for most fetches, its web fetch tool runs a separate model call over the page, and the main model receives that call's answer instead of the raw page.

Sandbox tool execution

If the model can run code or shell commands, run them where a mistake is cheap. Anthropic's sandboxing documentation describes a sandboxed command tool with filesystem and network isolation, and its security page recommends using virtual machines to run scripts and make tool calls, especially when interacting with external web services. Its hosted cloud sessions run in isolated virtual machines with network access limited by default.

The Anthropic sandbox documentation explains why the two kinds of isolation are both needed. Filesystem isolation limits tampering, such as writing files that change what runs next. Network isolation limits exfiltration, since broad read access could otherwise be sent to an outside host. Neither covers everything alone, and the page itself says restricting domains narrows the risk but does not remove it.

The joint agentic guidance makes the same point at fleet scale: use isolation and segmentation to limit the blast radius of agent failure, separate high-risk agents into distinct domains, and isolate agents into enclaves with no write access to logs.

Control egress

Many injection attacks need a way out for stolen data. NIST names exfiltration through features such as markdown image rendering as one way agents leak data, so the network path is a control point.

Anthropic's sandbox is a worked example of default-deny: a sandboxed command has no direct route to the network, the only exit is a proxy that checks each hostname against allowed and denied domains, and the allowed list starts empty. Google's Gemini defenses attack the same path from the application side: its markdown sanitizer does not render external image URLs, and suspicious links are detected using Google Safe Browsing and redacted from responses.

For your own stack, that translates into an outbound allow list for agent workloads, a proxy that logs every destination, and no direct internet route from the hosts that run tools. The joint guidance also lists network egress as an action that deserves a human approval checkpoint.

Require human approval for high-impact actions

OWASP recommends human-in-the-loop controls for privileged operations. The joint guidance specifies the kinds of actions where the cost of error is high: system resets, network egress and deletion of critical records, and it also suggests quarantining requests to delete logs or audit records until a human approves them. Microsoft calls verifying risky actions with the user the last line of defense. Google's Gemini asks for explicit confirmation before some risky operations, such as deleting a calendar event.

Two design rules follow. First, the approval should show the real action and its arguments, not the model's description of it. Second, the system's designers should decide which actions need approval, which the joint guidance states directly, rather than leaving the model to decide when to ask. Be aware that approval prompts can cause fatigue; Anthropic's documentation describes allowlisting frequently used safe commands as a way to reduce prompts, which only works if the allow list stays narrow.

Validate outputs and constrain formats

OWASP advises defining expected output formats and using deterministic code to validate adherence to them, and applying semantic filters and string checks to scan for disallowed content. In practice, treat model output as untrusted input to the next system: parse it against a schema, check tool arguments against policy before executing, and reject anything outside the allowed shape.

OpenAI's safety best practices add two simple constraints: limiting the amount of text a user can enter helps avoid injection, and limiting output tokens reduces misuse. They also note that returning existing validated content can be safer than returning novel generated text.

Where do dedicated detection products fit? Google, Microsoft and the joint guidance all list classifiers or prompt injection filters as part of a layered design, and the joint guidance says strong input controls can only partially mitigate the risks. The NCSC warns that deny-listing known attack phrases is fragile and advises caution toward products that claim to stop prompt injection. For a comparison of the products themselves, see our ranking of LLM guardrail and safety tools, and treat them as one layer.

Log, monitor and plan to detect

The NCSC recommends logging LLM inputs and outputs along with tool and API calls, and notes that failed tool calls can reveal the early stages of an attack. The joint guidance goes further: monitor all agent operations, not just inputs and outputs, log tool usage in a human-readable format, use runtime monitoring and anomaly detection against behavioral baselines, and monitor for goal drift against approved specifications.

Microsoft's pattern names runtime checks such as plan drift detection, critic agents that audit inputs and outputs, and tool chain analysis that blocks risky sequences of tool use. It also lists the costs honestly: added complexity, performance overhead and false positives from probabilistic defenses.

Logs only help if the agent cannot edit them, which is why the joint guidance recommends enclaves with no write access to logs.

Red-team and measure

OWASP recommends regular adversarial testing and attack simulations, treating the model as an untrusted user to test whether trust boundaries and access controls hold. OpenAI recommends red-teaming your application so it is robust to adversarial input. The joint guidance adds testing in sandbox environments before production deployment and running red-team exercises to find loopholes and unintended behavior.

NIST points to public evaluation tools: AgentDojo, an evaluation framework for agent vulnerability, and PyRIT are among the benchmarks and tools it lists for identifying weaknesses. It also cautions that new mitigations should be tested adversarially, ideally by the people who proposed them trying to break them, because novel defenses often look stronger than they are.

Re-run these tests whenever you add a tool, a data source or a model version, since each one changes what an injected instruction could reach.

A layered checklist

Microsoft's guidance puts the mindset plainly: assume some attacks will succeed, and design so the damage is contained.

Questions people ask

Can prompt injection be fully prevented?

No source used here claims it can. OWASP states that no foolproof prevention method is known, and the UK NCSC says it may never be mitigated the way SQL injection is. The goal is to reduce likelihood and contain impact.

Is a prompt injection classifier or guardrail enough?

Not by itself. The joint agentic AI guidance says input controls can only partially mitigate the risks, Microsoft says no single solution is sufficient, and the NCSC warns against relying on a product to stop it. Use classifiers as one layer alongside permissions, isolation and approvals.

What is the difference between direct and indirect prompt injection?

Per OWASP and MITRE ATLAS, direct injection comes from the user's own input, while indirect injection arrives through external content the model ingests, such as web pages or files. Indirect injection is the bigger infrastructure concern for agents because the attacker is a third party.

Which infrastructure control matters most?

No source ranks them, but least privilege is the one every source returns to, because it caps what a hijacked model can do whatever else fails.

Do I need a human to approve every action?

No. The joint guidance puts approval checkpoints on actions where the cost of error is high, such as system resets, network egress and deletion of critical records, and says designers should decide which ones.

Where are the tools that detect prompt injection compared?

See our ranking of LLM guardrails and safety tools at /ai-infrastructure/best-llm-guardrails-and-safety-tools-2027/.

Sources

Facts were read from the linked pages on November 8, 2026. Product features change, and the security literature on prompt injection is evolving quickly; check each vendor's current documentation before relying on a specific control. This page describes defenses only and no attack methods.

Top photo: NOIRLab/NSF/AURA/T. Slovinský / CC BY 4.0 via Wikimedia Commons

Share this page