Skip to content

Prompt Leakage Detection: Analysis

Summary

Prompt Leakage Detection is the process of identifying cases in which a model reveals the contents of the system prompt or internal instructions in a response to the user; it is related to prompt-injection and prompt-leaking.

What prompt leakage is

Prompt leakage is a situation in which an LLM includes in its response fragments of the system prompt, instructions, examples or other internal data that were not intended for the end user. This can happen accidentally or be the result of a deliberate attack.

Detecting leakage is the process of monitoring the output of a model for confidential fragments of the prompt. Unlike prompt-injection-detection, where the goal is to detect malicious input, here the focus is on analysing the output.

Types of leakage

Type Description Example
Direct leakage The model quotes the system prompt verbatim The response starts with 'You are an AI assistant created...'
Indirect leakage The model paraphrases the instructions 'My rules forbid me to answer this'
Leakage through examples The model reveals few-shot examples The response contains fragments of the training examples
Leakage through tools The model reveals internal function names or parameters 'I am calling the function get_user_data(user_id=42)'

Detection methods

1. String matching

The simplest method is searching for exact matches of prompt fragments in the response.

import re

def detect_exact_leak(response: str, prompt_fragments: list[str]) -> bool:
    for fragment in prompt_fragments:
        if fragment in response:
            return True
    return False

2. Semantic analysis

Using embeddings to find semantically close fragments. More about embeddings in embedding-basics.

from sentence_transformers import SentenceTransformer
from sklearn.metrics.pairwise import cosine_similarity

model = SentenceTransformer('all-MiniLM-L6-v2')

def detect_semantic_leak(response: str, prompt_fragments: list[str], threshold: float = 0.85) -> bool:
    response_emb = model.encode(response)
    for fragment in prompt_fragments:
        fragment_emb = model.encode(fragment)
        similarity = cosine_similarity([response_emb], [fragment_emb])[0][0]
        if similarity > threshold:
            return True
    return False

3. LLM-as-a-judge

Using a separate LLM to assess the response for leakage. More details in llm-as-a-judge.

import openai

def judge_leak(response: str, system_prompt: str) -> bool:
    judge_prompt = f""
    Check whether the following response contains fragments of the system prompt.
    System prompt: {system_prompt}
    Response: {response}
    Answer only 'yes' or 'no'.
    ""
    result = openai.chat.completions.create(
        model="gpt-4o-mini",
        messages=[{"role": "user", "content": judge_prompt}]
    )
    return result.choices[0].message.content.strip().lower() == "yes"

Architecture of a detection system

flowchart LR
    A[User] --> B[LLM app]
    B --> C[Model response]
    C --> D{Leak detector}
    D -->|Leak found| E[Block / mask]
    D -->|No leak| F[Response to user]
    E --> F

Practical recommendations

  1. Keep prompt fragments separately — comparison requires access to the original prompt.
  2. Use several methods — string matching catches exact matches, semantic analysis catches paraphrases.
  3. Log incidents — record leakage cases for analysis and improvement of the prompts.
  4. Integrate with guardrails — use guardrails for automatic filtering.
  5. Test regularly — carry out red-teaming to check resilience.

Relation to other topics