Inferensys

Prompt

API Key and Secret Leak Detection in User Input Prompt

A practical prompt playbook for detecting exposed API keys, tokens, passwords, and connection strings in user-submitted text before it enters logs, model context, or downstream systems.
Engineer optimizing context window usage on laptop, token usage charts visible, technical work session.
PROMPT PLAYBOOK

When to Use This Prompt

Understand the ideal deployment point, user, and boundaries for the API key and secret leak detection prompt.

This prompt is designed for platform security engineers, DevRel teams, and API gateway operators who need to scan user-submitted text for accidentally exposed credentials. It belongs at the ingress layer, before user input reaches model context, application logs, error reporting systems, or internal databases. The prompt identifies API keys, access tokens, passwords, connection strings, private keys, and internal endpoint URLs, then outputs a structured detection report with redacted previews and recommended remediation actions. Use this when you need a fast, LLM-based secret scanner that can catch patterns regex-based scanners miss, including credentials embedded in natural language, code snippets, or formatted messages.

Deploy this prompt as a pre-processing guard in your request pipeline. It is ideal for AI platform builders who accept free-text user input and need to prevent secret leakage into training data, log aggregators like Splunk or Datadog, or downstream model context windows. For example, a developer pasting an error log into a support copilot might accidentally include a Bearer token or an AWS AKIA key. This prompt will detect the secret, redact it, and return a structured alert so your application can block the request or sanitize it before it propagates. It is not a replacement for client-side secret scanning in CI/CD pipelines (like git-secrets or truffleHog) or runtime secret management; it is a safety net for user-generated text entering your AI systems.

Do not use this prompt as your sole defense for compliance with standards like PCI-DSS or SOC 2. It is a detection layer, not a prevention control. False negatives are possible, especially with heavily obfuscated or non-standard credential formats. Always pair this with deterministic regex scanning for known patterns and a human review queue for high-confidence detections in production. If your application handles highly regulated data, ensure that any redacted input is still subject to your standard data retention and encryption policies before it is logged or stored.

PRACTICAL GUARDRAILS

Use Case Fit

Where this prompt works, where it fails, and the operational preconditions required before you put it in front of real user traffic.

01

Good Fit: Pre-Ingestion Sanitization

Use when: you need to scan user-submitted text for secrets before it touches logs, databases, or model context windows. Guardrail: Run this prompt as a synchronous pre-flight check in your API gateway or middleware layer. Block or redact before any downstream processing.

02

Bad Fit: Encrypted or Obfuscated Payloads

Avoid when: secrets are embedded inside base64 blobs, encrypted payloads, or multi-layer encoding without prior decoding. Guardrail: Pair this prompt with an obfuscation detection step. If the input fails a decoding pre-scan, route to a specialized decoder before secret scanning.

03

Required Inputs

What you need: The raw user input string, a defined secret pattern library (regex or signature set), and a redaction policy (full mask, partial preview, or full block). Guardrail: Maintain a versioned secret pattern registry. Drift in pattern coverage is the top cause of false negatives.

04

Operational Risk: Log Contamination

Risk: If this prompt runs after logging, secrets are already persisted in plaintext. Guardrail: Place the detection step before any logging or telemetry collection in your request pipeline. Validate ordering with a canary token test in staging.

05

Operational Risk: Latency Budget

Risk: Synchronous LLM calls add latency to every user request. Guardrail: Set a strict timeout (e.g., 500ms). If the prompt exceeds budget, fail open to a regex-only fallback scanner. Never block legitimate traffic waiting for an LLM response.

06

Operational Risk: False Positive Triage

Risk: Over-blocking on strings that resemble secrets (e.g., base64-encoded images, git SHAs) breaks legitimate user workflows. Guardrail: Implement a quarantine queue for blocked inputs. Allow users to appeal false positives and feed appeal outcomes back into eval examples.

PROMPT PLAYBOOK

Copy-Ready Prompt Template

A copy-ready prompt template for detecting exposed API keys, tokens, and secrets in user-submitted text before it enters logs or model context.

This prompt template is designed to be integrated into an input sanitization layer, scanning user-submitted text for exposed secrets such as API keys, access tokens, passwords, connection strings, private keys, and internal endpoint URLs. The primary job is to identify and flag these secrets without ever outputting the full secret value, thereby preventing accidental logging, storage, or model context poisoning. Adapt the placeholders to fit your specific secret taxonomy, confidence thresholds, and remediation workflows.

text
Scan the following user-submitted text for exposed API keys, access tokens, passwords, connection strings, private keys, and internal endpoint URLs. Identify each detected secret by type, provide a redacted preview showing only the first 4 and last 4 characters, assess confidence, and recommend a remediation action. Do not output the full secret value under any circumstances.

[INPUT]

[CONSTRAINTS]
- Never output the full secret value.
- Use a redacted preview format: first 4 characters + '...' + last 4 characters.
- If no secrets are detected, return an empty list.
- If the input is ambiguous, flag it with lower confidence and recommend human review.

[OUTPUT_SCHEMA]
{
  "findings": [
    {
      "secret_type": "string (e.g., 'stripe_api_key', 'github_pat', 'jwt_token', 'password', 'connection_string', 'private_key', 'internal_endpoint')",
      "redacted_preview": "string (e.g., 'sk_l...abc1')",
      "confidence": "float (0.0 to 1.0)",
      "remediation": "string (e.g., 'revoke_and_rotate', 'quarantine_input', 'human_review')"
    }
  ]
}

[EXAMPLES]
Input: "My API key is sk_live_abc123xyz789"
Output: {"findings": [{"secret_type": "stripe_api_key", "redacted_preview": "sk_l...z789", "confidence": 0.98, "remediation": "revoke_and_rotate"}]}

Input: "The connection string is Server=db.internal.net;User=admin;Password=SuperSecret123;"
Output: {"findings": [{"secret_type": "connection_string", "redacted_preview": "Serv...t123", "confidence": 0.95, "remediation": "quarantine_input"}, {"secret_type": "password", "redacted_preview": "Supe...t123", "confidence": 0.90, "remediation": "revoke_and_rotate"}]}

Input: "Here is a random string: abc123xyz"
Output: {"findings": []}

[RISK_LEVEL]
High. Leaked secrets in model context or logs can lead to unauthorized access and lateral movement. Always require human review for ambiguous findings.

To adapt this template, replace the example secret types in the secret_type enum with your organization's specific taxonomy. Adjust the remediation actions to map to your internal incident response playbooks, such as automated key revocation via your secrets manager or ticket creation in your security queue. For high-throughput production systems, consider adding a [TOOLS] section that allows the model to call a quarantine API directly. Always pair this prompt with a post-processing validator that confirms the output JSON schema and rejects any response containing a full secret value.

IMPLEMENTATION TABLE

Prompt Variables

Required inputs for the API key and secret leak detection prompt. Each placeholder must be populated before the prompt is sent to the model. Validation notes describe how to check the input quality and format at runtime.

PlaceholderPurposeExampleValidation Notes

[USER_INPUT]

The raw text submitted by the user that will be scanned for leaked secrets

Here is my config: AWS_SECRET_ACCESS_KEY=wJalrXUtnFEMI/K7MDENG/bPxRfiCYEXAMPLEKEY

Check that input is a non-empty string. Reject null or whitespace-only inputs before model invocation. Log input hash for deduplication.

[SECRET_PATTERNS]

A list of regex patterns or secret formats to scan for, including API key prefixes, token structures, and connection string templates

['AKIA[0-9A-Z]{16}', 'sk-[A-Za-z0-9]{32,}', 'ghp_[A-Za-z0-9]{36}', 'mongodb+srv://']

Validate that the list contains valid regex strings. Test each pattern against known examples before deployment. Update patterns when new secret formats are released by vendors.

[CONTEXT_WINDOW_SIZE]

The number of characters before and after a detected secret to include in the redacted preview for human review

40

Must be an integer between 20 and 200. Smaller windows risk missing context for remediation; larger windows risk exposing more of the secret in logs. Default to 40 if not specified.

[REDACTION_CHAR]

The character used to mask the secret value in the redacted preview output

Must be a single character string. Common values are '*' or '#'. Do not use empty string or whitespace. The redaction character should not appear in the original secret to avoid ambiguity.

[CONFIDENCE_THRESHOLD]

The minimum confidence score required to flag a match as a detected secret, reducing false positives from coincidental pattern matches

0.85

Must be a float between 0.0 and 1.0. Lower thresholds increase recall but generate more false positives. Tune against a labeled dataset of known secrets and benign strings. Default to 0.85 if not specified.

[ALLOWED_TEST_PREFIXES]

A list of known test or example secret prefixes that should be suppressed from alerts to reduce noise from documentation and sample code

['TEST_', 'EXAMPLE_', 'DEMO_', 'SANDBOX_']

Validate that the list contains non-empty strings. Apply case-insensitive matching. Log suppressed matches for auditability. Review this list quarterly to prevent attackers from using allowed prefixes to bypass detection.

[REMEDIATION_ACTIONS]

A mapping of secret types to recommended remediation steps, used to generate actionable output for the user or security team

{'aws_access_key': 'Revoke via IAM console and rotate immediately', 'github_token': 'Revoke at github.com/settings/tokens and check commit history'}

Validate that the mapping is a valid JSON object with non-empty string values. Ensure coverage for all secret types in [SECRET_PATTERNS]. Missing entries should trigger a warning at prompt assembly time.

PROMPT PLAYBOOK

Implementation Harness Notes

How to wire the secret detection prompt into an API gateway or input sanitization pipeline with validation, logging, and safe handling of detected credentials.

This prompt is designed to operate as a pre-processing guard in your application's ingress layer. It should be invoked before user input is written to any persistent log, stored in a database, or passed to downstream models and tools. The ideal integration point is an API gateway middleware, a reverse proxy filter, or a serverless edge function that inspects the raw request body. The prompt expects a single user input string and returns a structured JSON object indicating whether secrets were detected, what type, a redacted preview, and a recommended action. Because the prompt itself processes potentially live credentials, you must ensure that the model endpoint you call for this check is hosted in a trusted environment with access controls equivalent to your production secret store.

Wiring the prompt into your application involves a synchronous HTTP call to your model provider with a strict timeout (recommend 2–5 seconds). The request should include the prompt template with the [USER_INPUT] placeholder replaced by the raw text. The response must be parsed against a strict JSON schema. If the model returns action: "block", your gateway should return a 422 Unprocessable Entity or 400 Bad Request to the client without echoing the input. If action: "redact", strip the detected secrets from the input before forwarding it. If action: "allow", proceed normally. Implement a retry policy with exponential backoff (max 3 attempts) for transient model errors, but fail closed: if the detection service is unavailable, either queue the input for async review or reject it based on your risk tolerance. Never fail open and allow un-scanned input through.

Validation and logging require extreme care. The model's output JSON must be validated field-by-field: check that action is one of the allowed enum values, detected_secrets is an array, and each secret object contains a type, redacted_preview, and confidence score. If validation fails, treat the input as potentially dangerous and route it for human review. When logging detection events, never log the redacted_preview field if there is any chance it contains a partial secret. Instead, log only the detection type, confidence, and action taken. For audit trails, store a hash of the original input and the detection result, not the input itself. In high-security environments, run this prompt in an isolated execution environment where the model's context window is cleared between requests to prevent cross-request secret leakage.

Model choice and cost optimization matter at scale. This classification task works well with smaller, faster models (e.g., Claude 3 Haiku, GPT-4o-mini, or a fine-tuned open-weight model) because the output structure is constrained and the reasoning is pattern-matching rather than generative. If you process high volumes, consider batching multiple inputs into a single prompt with clear delimiters, but be aware that a single detected secret in a batch may require discarding the entire batch's context for safety. For latency-sensitive paths, deploy a regex-based pre-filter that catches obvious patterns (AWS key formats, JWT headers, connection strings) before calling the LLM. Only send inputs that pass the regex filter but still look suspicious to the model for deeper semantic analysis. This two-tier approach reduces cost and latency while maintaining detection coverage for obfuscated or non-standard secret formats.

IMPLEMENTATION TABLE

Expected Output Contract

Fields, format, and validation rules for the API key and secret leak detection response. Every field must pass these checks before the output is logged, routed, or acted upon.

Field or ElementType or FormatRequiredValidation Rule

detection_result

enum: CLEAN | LEAK_DETECTED | INCONCLUSIVE

Must be exactly one of the three enum values. Reject any other string.

leaks

array of leak objects

Must be an array. If detection_result is CLEAN, array must be empty (length 0). If LEAK_DETECTED, array must contain at least 1 item.

leaks[].secret_type

enum from [SECRET_TYPE_TAXONOMY]

Must match a known type: API_KEY, AUTH_TOKEN, PASSWORD, CONNECTION_STRING, PRIVATE_KEY, INTERNAL_ENDPOINT, OTHER_SECRET. Reject unknown types.

leaks[].redacted_preview

string

Must contain the first 4 and last 4 characters of the detected secret with the middle replaced by [REDACTED]. Length must be >= 12 characters. Must not contain the full original secret.

leaks[].confidence

number (0.0-1.0)

Must be a float between 0.0 and 1.0 inclusive. Values below [CONFIDENCE_THRESHOLD] should trigger INCONCLUSIVE result instead of LEAK_DETECTED.

leaks[].start_index

integer

Must be a non-negative integer representing the character offset in [INPUT]. Must be less than the length of [INPUT]. Validate bounds before logging.

leaks[].end_index

integer

Must be an integer greater than start_index. Must be less than or equal to the length of [INPUT]. The substring [INPUT][start_index:end_index] must match the detected secret.

recommended_action

enum: ALLOW | REDACT_AND_LOG | BLOCK_AND_QUARANTINE | HUMAN_REVIEW

Must be exactly one of the four enum values. BLOCK_AND_QUARANTINE requires detection_result LEAK_DETECTED and at least one leak with confidence >= 0.9.

PRACTICAL GUARDRAILS

Common Failure Modes

API key and secret leak detection prompts fail in predictable ways. These are the most common failure modes observed in production, along with concrete guardrails to prevent each one.

01

False Negatives on Non-Standard Secret Formats

What to watch: The prompt misses internal API keys, custom tokens, database connection strings, or proprietary secret formats that don't match well-known patterns like sk- or AKIA. The model relies on pattern recognition of popular services and fails on anything custom. Guardrail: Include explicit examples of your organization's internal secret formats in the few-shot examples. Supplement the prompt with a regex pre-filter that flags high-entropy strings regardless of format, and route those to the LLM for deeper analysis.

02

Over-Redaction of Benign Code and Config Snippets

What to watch: The prompt flags placeholder values (YOUR_API_KEY_HERE), example code from documentation, hardcoded test credentials in tutorial repos, or environment variable names as live secrets. This creates noisy alerts and erodes trust in the detection pipeline. Guardrail: Add a classification tier that distinguishes live secrets from placeholders, examples, and documentation. Include negative examples of common false-positive patterns in your few-shot demonstrations. Require a confidence score and only block or redact above a high threshold.

03

Context Window Truncation Hiding Secrets

What to watch: Long user inputs get truncated by context window limits, and secrets buried deep in pasted logs, stack traces, or configuration dumps fall outside the analyzed portion. The prompt returns clean when secrets exist in the truncated tail. Guardrail: Chunk long inputs and scan each chunk independently before assembly. If chunking isn't feasible, implement a pre-scan with a lightweight regex or entropy-based detector that runs on the full untruncated input before the LLM call.

04

Multi-Line and Concatenated Secret Evasion

What to watch: Users split secrets across multiple lines, insert whitespace or comments mid-token, or concatenate fragments to evade detection. The model sees broken tokens and fails to reconstruct the full secret. Guardrail: Normalize whitespace and strip common comment characters before analysis. Include few-shot examples of split and concatenated secrets with the reconstructed form. Test with adversarial inputs that use line breaks, inline comments, and string interpolation tricks.

05

Model Hallucinating Secret Types and Remediation Steps

What to watch: The prompt confidently identifies a secret as an AWS access key when it's actually a Stripe test key, or recommends rotating a credential that doesn't exist. Hallucinated secret types lead to incorrect incident response and wasted engineering time. Guardrail: Constrain the output schema to only allow secret types from a predefined, validated taxonomy. Require the model to cite the specific pattern or entropy characteristic that triggered detection. Route all positive detections to a human-reviewed quarantine queue before automated remediation actions fire.

06

Base64 and Encoded Payload Blind Spots

What to watch: Secrets encoded in base64, URL-encoded strings, or hex pass through undetected because the prompt analyzes the encoded form rather than decoding first. JWTs, basic auth headers, and encoded connection strings are common carriers. Guardrail: Pre-process inputs to detect and decode common encoding formats before the LLM analysis. Include decoded versions alongside the original in the prompt context. Test against base64-encoded API keys, URL-encoded tokens, and hex-encoded secrets in your eval suite.

IMPLEMENTATION TABLE

Evaluation Rubric

Criteria for testing the API key and secret leak detection prompt before production deployment. Each row defines a pass standard, a failure signal, and a concrete test method to validate detection quality.

CriterionPass StandardFailure SignalTest Method

AWS Access Key Detection

Detects AKIA-prefixed keys and redacts secret portion

Misses keys embedded in logs, JSON, or connection strings

Run 50 positive samples with varied key formats; require >= 98% recall

Bearer Token Detection

Flags tokens with standard prefixes (ghp_, sk-, xoxb-)

False positives on base64-encoded binary blobs or UUIDs

Run 100 negative samples of non-token structured strings; require <= 1% false positive rate

Redacted Preview Output

Outputs first 4 and last 4 characters of secret with middle masked

Leaks full secret in [REDACTED_PREVIEW] field

Parse output schema; assert [REDACTED_PREVIEW] matches pattern [A-Za-z0-9]{4}...[A-Za-z0-9]{4}

Remediation Action Mapping

Returns correct action from set: ROTATE, REVOKE, QUARANTINE, ESCALATE

Recommends IGNORE for active production credentials

Validate [REMEDIATION] field against expected mapping for each secret type in test set

Multi-Secret Input Handling

Detects all distinct secrets in a single input block

Reports only first secret found; misses subsequent leaks

Feed input containing 3 different secret types; assert output array length equals 3

False Negative on Benign Input

Returns empty findings array for clean text with no secrets

Flags UUIDs, SHA hashes, or high-entropy session IDs as secrets

Run 200 benign samples (code snippets, config stanzas, log lines); require zero findings

Confidence Score Calibration

Confidence >= 0.9 for unambiguous secrets; <= 0.5 for ambiguous patterns

Returns 0.99 confidence for all detections regardless of ambiguity

Spot-check 20 edge cases; assert confidence distribution matches expected tiers

Latency Under Load

Prompt completes in under 500ms for inputs under 2KB

Exceeds 2 seconds for single short input

Benchmark with 100 concurrent requests; measure p95 latency

ADAPTATION OPTIONS

Adapt This Prompt

How to adapt

Use the base prompt with a simple pass/fail output and minimal post-processing. Focus on catching obvious plaintext secrets (API keys, tokens, passwords) without complex regex or entropy checks.

code
Analyze the following user input for exposed secrets. Return JSON with a single boolean field `contains_secret` and a list of `findings`.

[INPUT]

Watch for

  • False positives on UUIDs, base64-encoded images, and git hashes
  • Missing secrets embedded in code blocks or logs
  • No handling of multi-line connection strings
Prasad Kumkar

About the author

Prasad Kumkar

CEO & MD, Inference Systems

Prasad Kumkar is the CEO & MD of Inference Systems and writes about AI systems architecture, LLM infrastructure, model serving, evaluation, and production deployment. Over 5+ years, he has worked across computer vision models, L5 autonomous vehicle systems, and LLM research, with a focus on taking complex AI ideas into real-world engineering systems.

His work and writing cover AI systems, large language models, AI agents, multimodal systems, autonomous systems, inference optimization, RAG, evaluation, and production AI engineering.