Prompt Engineering

Expert field guide

AI prompt injection: how it works and how to reduce risk in real applications

Learn the difference between direct and indirect AI prompt injection, why prompt wording alone is not a security boundary, and how to build layered defenses.

What you will learn

  • Prompt injection happens when untrusted content changes an AI system’s behavior beyond the user’s intended task.
  • A stronger system separates instructions from data, limits permissions, validates actions and outputs, and requires approval for consequential operations.
  • Security testing must cover direct input, retrieved documents, webpages, email, tool results, memory, and every path that can place text in model context.

01

What is AI prompt injection?

AI prompt injection is a security weakness in which text supplied by a user or an external source changes a model’s behavior in a way the application did not intend. The risk exists because language models receive instructions and data through the same general medium: tokens in context. A paragraph that should be summarized can contain language that looks like a new instruction, and the model may not reliably preserve the boundary.

This is different from ordinary prompt improvement. A legitimate user asks the system to complete an allowed task. An injection attempts to redirect that task, expose protected information, misuse a connected tool, weaken a policy, or make an untrusted source control the workflow. The important security question is not whether a suspicious phrase appears; it is whether untrusted content can influence a sensitive decision or action.

02

Direct and indirect prompt injection

Direct prompt injection arrives through an input controlled by the person interacting with the application. The person may try to replace the intended objective, obtain hidden configuration, or push the model beyond the authority the product grants them. A chat interface is the obvious example, but form fields, uploaded files, API parameters, and support tickets can all become input channels.

Indirect prompt injection is embedded in material the AI is asked to read: a webpage, document, email, code comment, knowledge-base entry, retrieved passage, tool result, image, or stored memory. The person using the application may never see the embedded instruction. Indirect injection is especially important for retrieval systems and agents because external content can travel from a low-trust source into a model that has access to valuable data or tools.

  • Direct input: chat messages, forms, API fields, filenames, and uploaded text
  • Retrieved context: documents, vector-store passages, search results, and knowledge bases
  • Tool context: API responses, issue descriptions, emails, webpages, and database text
  • Persistent context: conversation history, memory records, saved notes, and shared workspaces
  • Multimodal context: visible or hidden text in images and complex documents

03

Why a stronger system prompt is not enough

Clear system instructions are useful, but they are not an authorization layer. Adding a sentence that tells the model to ignore malicious instructions may reduce some failures, yet the same model still interprets both trusted and untrusted language. Attackers can vary wording, split instructions across turns, hide content in retrieved material, or exploit a path the original prompt designer did not test.

Treat the model as a component that can make mistakes under adversarial input. Enforce identity, permissions, data access, allowed actions, approval rules, and business constraints outside the model whenever possible. The model can recommend an action, but ordinary application code should decide whether the authenticated user is allowed to perform it and whether the proposed parameters are valid.

04

Map the complete trust boundary

Begin with a data-flow diagram. Mark every source that can place content in the model context, every secret or protected record the workflow can access, every tool it can call, and every destination that receives model output. Assign a trust level and an owner to each boundary. A public webpage should not receive the same trust as an approved internal policy simply because both appear in one prompt.

Then connect each input to a possible impact. Could the content alter a customer message, publish text, change a record, send an email, expose another user’s data, or trigger an external request? Risk grows when untrusted content, sensitive data, and consequential tools meet in the same model context. Reducing any side of that combination can shrink the attack surface.

  • Which people or systems control each input?
  • Can external content be clearly labeled, minimized, or processed in isolation?
  • Which data can the model read, and is every field necessary?
  • Which actions can the model propose or execute?
  • What deterministic validation occurs before an action?
  • Which events are logged, reviewed, blocked, or escalated?

05

Use layered prompt-injection defenses

No single filter provides complete protection. A practical design combines data minimization, explicit context boundaries, input screening, constrained output formats, least-privilege tools, server-side authorization, parameter validation, output handling, monitoring, and human approval. The layers should remain useful even if another layer misses an attack.

Separate trusted instructions from untrusted material with structured fields and clear labels. Ask the model to treat external text as evidence to analyze rather than commands. This improves the task contract, but it must sit beside real controls. Allow-list tools for each workflow, expose only the smallest required function, validate every argument, and make read-only access the default. Never place credentials in model-visible context.

  • Minimize and label untrusted content before it reaches the model
  • Use structured input and output contracts with strict schema validation
  • Apply authentication and authorization outside the model
  • Grant each tool the least privilege and narrowest scope
  • Require human approval for irreversible, sensitive, or high-impact actions
  • Sanitize rendered output and block unsafe links, markup, or executable content
  • Rate-limit, monitor, alert, and preserve privacy-aware security evidence

06

Design safer RAG and AI agent workflows

A retrieval-augmented generation system should preserve source identity and trust level. Filter documents before ingestion, restrict retrieval by user permission, keep citations attached to passages, and prevent one retrieved item from silently becoming an instruction source. When possible, extract constrained facts from untrusted content before the privileged workflow sees them.

Agents require stronger controls because model output can become a tool call. Keep planning separate from execution. Compare every proposed action with the authenticated user’s original request, current permissions, allowed destination, and approved data scope. Use confirmation screens for meaningful actions and ensure a rejected or failed tool call cannot be disguised as success in the final answer.

07

Validate outputs and downstream behavior

Model output is untrusted even when the input appeared safe. Parse structured responses with a strict schema, reject unexpected fields, validate identifiers against the user’s permission scope, and escape content before rendering it as HTML or Markdown. Do not allow generated URLs, queries, filenames, or tool parameters to bypass the same validation applied to ordinary application input.

A workflow should fail safely. If evidence is missing, a tool response is malformed, or a permission check fails, stop or route the case to a person. Do not ask the model to invent a substitute value. Record enough event metadata to investigate failures while minimizing personal data and secrets in logs.

08

Build a prompt-injection test plan

Test the complete application rather than only the system prompt. Create cases for each input channel, trust boundary, model step, retrieval path, tool, and output renderer. Include ordinary requests, ambiguous instructions, conflicting sources, external content that resembles an instruction, unavailable tools, malformed data, and attempts to exceed the user’s authority.

For every case, define the expected safe behavior and the action that must never happen. Measure whether the system exposes protected data, changes scope, calls an unauthorized tool, sends information to an unapproved destination, renders unsafe output, or hides the failure from the user. Re-run the suite after changes to the model, prompt, tools, retrieval pipeline, memory, permissions, or interface.

  • Normal task with trusted context
  • Direct attempt to replace the task
  • Instruction-like text inside a retrieved document
  • Conflicting trusted and untrusted sources
  • Tool result containing unexpected instructions
  • Request for an action outside the user’s permissions
  • Malformed structured output and unsafe rendered content
  • Repeated attempts, multi-turn drift, and persistent-memory contamination

09

A practical launch checklist

Before launch, confirm that the product has an explicit threat model, restricted tools, server-side authorization, protected secrets, data retention rules, approval boundaries, output validation, security monitoring, and a tested incident process. Name the person responsible for each control. A checklist without ownership will become outdated quickly.

Prompt injection risk cannot be reduced to zero by wording alone. The goal is to make one model mistake insufficient to cause a serious outcome. Limit what the system can see, limit what it can do, verify what it proposes, and place a person at the decision point when impact is difficult to reverse.

FAQ

Common questions

Can prompt injection be completely prevented?

No single prompt or filter can guarantee complete prevention. Use defense in depth: isolate untrusted content, minimize data, restrict tools, enforce permissions outside the model, validate outputs and actions, monitor behavior, and require approval for consequential operations.

What is the difference between prompt injection and a jailbreak?

Prompt injection is the broader application-security problem of untrusted input changing intended model behavior. A jailbreak usually focuses on bypassing a model’s behavioral safeguards. In an application, either can become more serious when the model can access sensitive data or tools.

Does separating instructions and data solve prompt injection?

It improves clarity and should be used, but it is not a complete security boundary. Pair structured prompts with least privilege, authorization, input and output validation, isolation, monitoring, and human approval.

Who should own prompt-injection testing?

Product, engineering, security, data, and workflow owners should share responsibility. The application team understands the data flow and tools; security specialists bring threat modeling and adversarial testing; business owners define which outcomes require approval.

Sources

Primary sources and live documentation

These links point to authoritative documentation used to verify and maintain this guide for the July 2026 update.

Apply

Use the related expert prompts

Turn this framework into a repeatable workflow with prompts designed for ChatGPT, Claude, and Gemini.

CodingCreate an AI prompt-injection threat modelOpen prompt →CodingReview untrusted context in an AI workflowOpen prompt →

Turn the method into a reusable instruction.

Explore expert prompts