This prompt is designed for a single, security-critical job: detecting and replacing API keys, cloud credentials, and high-entropy secrets inside code snippets before those snippets are sent to a large language model. The ideal user is a developer building an AI coding assistant, a security engineer hardening a pre-inference data pipeline, or a platform engineer responsible for preventing secret leakage in a multi-tenant SaaS product. The prompt expects a raw code block as input and returns the same code block with all detected secrets replaced by descriptive placeholder variables such as [REDACTED_AWS_ACCESS_KEY]. It is not a general-purpose PII redaction tool, a log sanitizer, or a natural-language document scrubber—use the sibling PII Detection or Context Sanitization prompts for those tasks.
Prompt
API Key and Secret Scrubbing Prompt for Code Snippets

When to Use This Prompt
Define the job, ideal user, required inputs, and hard boundaries for the API key scrubbing prompt before you integrate it into a code-processing pipeline.
You should use this prompt when code is the primary input modality and the risk is exfiltration of programmatic credentials to a third-party model provider. The prompt is tuned to catch common secret formats: AWS AKIA... keys, Azure connection strings, GCP service account JSON, GitHub ghp_... tokens, Stripe sk_live_... keys, and generic high-entropy base64 or hex strings that match the structural fingerprint of secrets. It also handles secrets embedded in environment variable assignments, config files, and CLI examples. The output is a drop-in replacement for the original code, preserving syntax and structure so that downstream tasks—code explanation, refactoring, test generation—still function correctly. Do not use this prompt if you need cryptographic guarantees of redaction; a regex-based pre-filter combined with this LLM-based scrubber provides defense-in-depth, but neither replaces a dedicated secret scanning platform for production audit trails.
Before wiring this into your application, gather the following: a representative sample of your codebase's secret formats (including internal patterns like company_secret_*), a golden dataset of code snippets with known secrets for evaluation, and a clear policy on what should happen when the model is uncertain. The prompt includes a [CONFIDENCE_THRESHOLD] variable that lets you control the trade-off between false positives (over-redaction that breaks code) and false negatives (missed secrets that leak). Start with a threshold of 0.7 and adjust based on your eval results. If your application handles code in multiple languages, test the prompt against each language's common secret-loading idioms—Python's os.getenv(), Node's process.env, Go's os.LookupEnv—to ensure the scrubber recognizes secrets regardless of how they are referenced.
When to choose a different approach: If your primary concern is PII in natural language (names, emails, SSNs) rather than programmatic secrets, use the PII Detection Prompt Template instead. If you need to sanitize the entire assembled prompt context—not just code blocks—before tool dispatch, the Context Sanitization Prompt is a better fit. If you are building a final pre-inference gate that must catch everything that slipped through earlier redaction layers, deploy the Prompt Assembly Guard Prompt as a last line of defense. This prompt is a specialized component in a larger sensitive-data handling architecture, not a standalone solution.
Use Case Fit
Where the API key and secret scrubbing prompt works reliably and where it introduces unacceptable risk or operational overhead.
Good Fit: Pre-Processing for Internal Developer Tools
Use when: sanitizing code snippets before they enter an internal LLM-powered code review, documentation generator, or pair programmer. Guardrail: The prompt operates in a controlled, non-adversarial environment where the cost of a false negative is low and can be caught by subsequent manual review.
Bad Fit: Sole Defense for Public-Facing Chatbots
Avoid when: this prompt is the only line of defense before a user-supplied code block is processed by a model and the response is streamed to an external user. Guardrail: An LLM-based scrubber is probabilistic. For public-facing surfaces, it must be paired with a deterministic regex engine and a post-scrub high-entropy string validator.
Required Inputs: Code Block and Secret Catalog
What to watch: The prompt fails silently if it receives only a code block without a defined catalog of secret patterns to match (e.g., AWS key prefixes, JWT structures). Guardrail: Always provide a structured [SECRET_PATTERNS] variable containing known prefixes, entropy thresholds, and placeholder replacement rules. A generic 'remove secrets' instruction is insufficient.
Operational Risk: High-Entropy String False Positives
What to watch: The prompt may aggressively scrub base64-encoded binary blobs, cryptographic nonces, or UUIDs that are functional code, not secrets, breaking the code's logic. Guardrail: Implement a pre-check that distinguishes between assigned variables (potential secrets) and inline constants or encoded assets. Always log scrubbed strings for a human auditor to review before the code is executed.
Operational Risk: Model Drift on New Secret Formats
What to watch: Cloud providers and OSS libraries release new secret formats (e.g., new key prefixes, token structures) that the model has not seen during training. Guardrail: Maintain a versioned, updatable regex pattern list as the primary detection mechanism. Use the LLM prompt only to resolve ambiguous cases where context determines if a matched pattern is a secret or a benign string literal.
Bad Fit: Real-Time Streaming Code Completion
Avoid when: the system requires sub-50ms latency for inline code suggestions in an IDE. Guardrail: An LLM-based scrubber adds unacceptable latency for keystroke-level interactions. For this use case, use a deterministic, cached regex engine for the hot path and reserve the LLM scrubber for an asynchronous, pre-commit hook.
Copy-Ready Prompt Template
A reusable prompt template that detects and replaces API keys, secrets, and cloud credentials in code snippets with safe placeholder variables.
This prompt template is designed to be integrated into a pre-processing pipeline for any developer tool, AI coding assistant, or internal platform that processes user-submitted code before it reaches an LLM. The template accepts a raw code snippet and a configurable list of secret patterns to scan for, then returns a scrubbed version of the code with all detected secrets replaced by descriptive placeholder variables. The output also includes a structured audit map so downstream systems can verify what was changed and, if needed, rehydrate the original values in a secure execution environment.
textYou are a security-focused code sanitizer. Your task is to scan the provided code snippet for API keys, access tokens, connection strings, private keys, and other secrets. You must replace every detected secret with a descriptive placeholder variable while preserving all other code structure, comments, and formatting exactly. ## INPUT CODE
[CODE_SNIPPET]
code## SECRET PATTERNS TO DETECT [SECRET_PATTERNS] ## INSTRUCTIONS 1. Scan the input code line by line. 2. Identify any string literal, variable assignment, config value, or hardcoded credential that matches a known secret pattern or exhibits high entropy typical of keys and tokens. 3. For each detected secret: - Replace the secret value with a descriptive placeholder in the format `[REDACTED_<TYPE>]` where `<TYPE>` is a short, descriptive label (e.g., `AWS_ACCESS_KEY`, `GITHUB_TOKEN`, `DATABASE_URL`, `STRIPE_SECRET`). - If the same secret appears multiple times, use the same placeholder consistently. - Do not alter variable names, only the assigned values. 4. Preserve all code structure, indentation, comments, imports, and surrounding logic exactly as-is. 5. Do not redact non-secret strings such as public identifiers, file paths, library names, or user-defined variable names. 6. If no secrets are detected, return the original code unchanged. ## OUTPUT FORMAT Return a JSON object with the following structure: { "scrubbed_code": "<the full code snippet with secrets replaced>", "replacements": [ { "placeholder": "[REDACTED_<TYPE>]", "secret_type": "<type classification>", "line_number": <integer>, "original_length": <integer> } ], "secrets_detected": <integer>, "scrubbed": <boolean> } ## CONSTRAINTS - Never output the original secret values in any field of the response. - The `original_length` field must record only the character count of the redacted value, not the value itself. - If the input code is empty or contains only whitespace, return an empty scrubbed_code and set secrets_detected to 0. - Do not add explanatory text outside the JSON object.
To adapt this template for your environment, replace [CODE_SNIPPET] with the raw code block submitted by the user or extracted from a repository. The [SECRET_PATTERNS] placeholder should be populated with a structured list of the specific credential formats you need to catch—such as AWS AKIA* keys, GitHub tokens (ghp_*, gho_*, ghu_*, ghs_*), Azure connection strings, JWT tokens, Stripe sk_live_* keys, and generic high-entropy base64 strings. For production use, pair this prompt with a regex pre-filter that catches unambiguous patterns before calling the LLM, reducing both latency and the risk of the model accidentally echoing a secret in its response. Always log the replacements array for auditability, and never send the original [CODE_SNIPPET] to an external model endpoint without this scrubbing layer in place.
Prompt Variables
Required inputs for the API key and secret scrubbing prompt. Each placeholder must be populated before the prompt is assembled and sent to the model. Validation notes describe preflight checks that prevent common failures.
| Placeholder | Purpose | Example | Validation Notes |
|---|---|---|---|
[CODE_SNIPPET] | The raw code block or file content to scan for secrets | const apiKey = 'AKIAIOSFODNN7EXAMPLE'; const db = mysql.createConnection({ password: 's3cr3t!' }); | Must be non-empty string. Check for null, undefined, or whitespace-only input. Max length should be enforced at application layer to prevent token overflow. |
[SECRET_PATTERNS] | List of secret formats to detect, specified as named patterns or regex categories | ["AWS_ACCESS_KEY", "GCP_SERVICE_ACCOUNT", "AZURE_CONNECTION_STRING", "GITHUB_TOKEN", "JWT", "GENERIC_HIGH_ENTROPY"] | Must be a valid JSON array of strings. Validate against a known pattern catalog. Empty array means detect all known patterns. Unknown pattern names should trigger a warning. |
[REPLACEMENT_STYLE] | How detected secrets should be replaced in the output | "PLACEHOLDER" or "REDACTED" or "VARIABLE_ASSIGNMENT" | Must be one of the allowed enum values. PLACEHOLDER replaces with descriptive name like <AWS_ACCESS_KEY>. REDACTED replaces with <REDACTED>. VARIABLE_ASSIGNMENT replaces with a variable reference like process.env.AWS_ACCESS_KEY. |
[OUTPUT_SCHEMA] | JSON schema describing the expected output structure | {"type": "object", "properties": {"scrubbed_code": {"type": "string"}, "findings": {"type": "array", "items": {"type": "object", "properties": {"line": {"type": "number"}, "type": {"type": "string"}, "original_length": {"type": "number"}}}}}, "required": ["scrubbed_code", "findings"]} | Must be valid JSON Schema. Validate with a schema validator before prompt assembly. Required fields should include scrubbed_code and findings at minimum. Reject if schema is malformed. |
[CONFIDENCE_THRESHOLD] | Minimum confidence score for reporting a finding, between 0.0 and 1.0 | 0.85 | Must be a float between 0.0 and 1.0. Values below 0.7 increase false positives. Values above 0.95 may miss obfuscated secrets. Default to 0.85 if not specified. |
[CONTEXT_WINDOW_BUDGET] | Maximum token budget allocated to this prompt in the overall assembly | 4000 | Must be a positive integer. Enforce at assembly time by truncating [CODE_SNIPPET] if needed. Log a warning if the snippet alone exceeds 80% of budget before other prompt components are added. |
[ALLOWED_LIBRARIES] | List of libraries or frameworks whose known test fixtures and example keys should be ignored | ["aws-sdk-mock", "jest", "mocha"] | Must be a valid JSON array of strings or null. When provided, patterns matching known test fixtures from these libraries are flagged as LOW_RISK rather than removed. Null means no library exclusions. |
Implementation Harness Notes
How to wire the API key and secret scrubbing prompt into a production code-processing pipeline with validation, retries, and audit logging.
Integrating this prompt into an application requires treating it as a deterministic pre-processing step, not an advisory check. The prompt should be called before any code snippet enters a model's context window, a tool's argument list, or a logging pipeline. The harness must enforce a strict contract: the input is a raw code block, and the output must be a structurally identical code block with all detected secrets replaced by descriptive placeholders. Any deviation in code structure—such as reformatting, line reordering, or comment removal—constitutes a failure and must trigger a retry or fallback. The system should never pass the original, unscrubbed code to a downstream model.
The implementation should wrap the LLM call in a validation layer that performs at least three checks. First, a structural diff between the input and output code must confirm that only secret-bearing lines changed and that the AST (abstract syntax tree) remains equivalent modulo literal values. Second, a high-entropy regex scan must run on the output to verify that no strings exceeding a configurable entropy threshold (e.g., 4.5 bits per character) remain. Third, a known-pattern check must confirm that common secret formats—AWS access keys (AKIA...), Azure connection strings, GCP service account keys, GitHub personal access tokens, and common OSS library patterns like SECRET_KEY = assignments—are absent from the scrubbed output. If any check fails, the harness should retry with a more explicit prompt variant that includes the specific failure reason, up to a maximum of two retries before escalating to a human review queue.
Model choice matters for this workflow. A smaller, faster model (such as Claude 3 Haiku or GPT-4o-mini) is usually sufficient because the task is pattern-matching with structured replacement, not open-ended reasoning. The harness should log every scrubbing operation with a trace ID, the original code hash, the scrubbed code hash, a diff summary, and the model version used. For high-security environments, consider running the prompt through two different model families and comparing outputs; any disagreement should block the pipeline. Never log the original secrets themselves—log only their detected presence, location, and the placeholder used. When deploying in CI/CD pipelines, integrate this prompt as a pre-commit hook or a pre-inference gateway that fails closed: if the scrubbing service is unavailable, the pipeline must block code from reaching any model endpoint rather than falling back to unscrubbed input.
Expected Output Contract
Defines the structured JSON output that the scrubbing prompt must produce. Use this contract to validate the model's response before passing scrubbed code downstream.
| Field or Element | Type or Format | Required | Validation Rule |
|---|---|---|---|
scrubbed_code | string | Must not contain any string matching the original detected secrets. All placeholders must use the [REDACTED_*] format. | |
replacements | array of objects | Array length must equal the number of unique secrets detected. Each object must have original, placeholder, and type fields. | |
replacements[].original | string | Must be a verbatim substring found in the input code. Validate with exact string match against the input. | |
replacements[].placeholder | string | Must match the pattern [REDACTED_TYPE_HASH]. The HASH segment must be a consistent 8-character hex string derived from the original value. | |
replacements[].type | string | Must be one of the allowed enum values: aws_access_key, aws_secret_key, gcp_service_account, azure_connection_string, github_token, jwt_token, generic_api_key, private_key, database_url, or other_secret. | |
detection_confidence | string | Must be one of: high, medium, low. If any replacement has low confidence, a human_review_required flag must be set to true. | |
human_review_required | boolean | Must be true if detection_confidence is low for any secret, or if the input contains strings that look like secrets but do not match known patterns. | |
scrubbing_summary | object | Must contain total_secrets_found (integer >= 0) and secrets_by_type (object mapping type strings to integer counts). Sum of counts must equal total_secrets_found. |
Common Failure Modes
API key and secret scrubbing prompts fail in predictable ways. These are the most common production failure modes and the guardrails that catch them before they cause damage.
High-Entropy String False Negatives
What to watch: The model misses secrets that don't match known patterns—custom API tokens, internal service keys, or base64-encoded credentials that look like random noise. Guardrail: Pair the LLM prompt with a deterministic regex pre-scan for known formats (AWS, Azure, GCP, GitHub tokens) and a Shannon entropy check on any string above 20 characters. Flag low-confidence LLM detections for human review.
Over-Redaction Breaking Code Syntax
What to watch: The prompt aggressively replaces variable assignments, environment variable names, or config keys with placeholders, producing syntactically broken code that can't be parsed or executed downstream. Guardrail: Constrain the output schema to require the scrubbed code to pass a syntax validator (e.g., ast.parse for Python, esprima for JavaScript) before the response is accepted. Reject and retry if the code fails to parse.
Placeholder Collision with Existing Variables
What to watch: The model replaces a secret with a placeholder like [API_KEY] that already exists in the codebase, creating ambiguity about which value is real and which is redacted. Guardrail: Instruct the model to use unique, namespaced placeholders such as [REDACTED_SECRET_1] and include a mapping table in the output. Validate that no generated placeholder matches an existing variable name in the input.
Context Window Truncation Dropping Secrets
What to watch: Long code snippets exceed the model's context window, and secrets near the end of the file are silently dropped without detection or redaction. Guardrail: Chunk the input into overlapping segments before processing, run detection on each chunk independently, and merge results. Add a preflight token count check and log a warning if the input exceeds 80% of the model's context limit.
Model Hallucinating Secrets That Weren't There
What to watch: The model fabricates placeholder values or invents secret names that don't exist in the original input, creating false audit trails and confusing downstream developers. Guardrail: Require the output to include character offsets for every redaction. Post-process by extracting the original text at each offset and verifying it matches the claimed secret pattern. Discard any redaction claim that can't be verified against the source.
Multi-Line Secret Truncation
What to watch: Secrets spanning multiple lines—PEM-encoded private keys, multi-line connection strings, or JSON credentials—are only partially redacted because the model treats each line independently. Guardrail: Include explicit instructions and few-shot examples showing multi-line secret detection. Add a post-processing step that scans for unbalanced delimiters (-----BEGIN, { without }) and flags incomplete redactions for retry.
Evaluation Rubric
Use this rubric to test the API key and secret scrubbing prompt before integrating it into a production pipeline. Each criterion targets a specific failure mode observed when models handle high-entropy strings, known secret patterns, and cloud credential formats.
| Criterion | Pass Standard | Failure Signal | Test Method |
|---|---|---|---|
Known Pattern Recall | All AWS, Azure, GCP, and common OSS secret patterns in the test suite are detected and replaced with [REDACTED_*] placeholders | A known secret pattern appears unmasked in the output code block | Run the prompt against the golden dataset of 50 known secret formats and assert zero false negatives |
High-Entropy String Detection | Strings with Shannon entropy above 3.5 and length greater than 20 characters are flagged and replaced unless they match an allowlist pattern | A high-entropy API key or token passes through without redaction | Inject 20 synthetic high-entropy strings into code samples and verify all are redacted; confirm allowlisted strings like base64-encoded images are preserved |
Placeholder Consistency | Each redacted secret is replaced with a descriptive placeholder following the format [REDACTED_<SERVICE>_<KEY_TYPE>] | Placeholders are generic (e.g., [REDACTED]) or inconsistent across the same secret type | Scan output for placeholder format compliance using a regex validator; check that the same secret type always maps to the same placeholder label |
Code Structure Preservation | The output code block is syntactically valid and structurally identical to the input, with only secret values replaced | The output code fails to parse, has broken string literals, or missing line breaks | Run the output through a language-appropriate parser (e.g., Python AST, JavaScript ESLint) and assert no syntax errors introduced |
False Positive Rate on Non-Secrets | No more than 2% of non-secret strings are incorrectly redacted, and no variable names, comments, or documentation strings are touched | A variable name like | Run the prompt against a clean code corpus with zero secrets and assert fewer than 2% of tokens are modified; manually review any redactions for correctness |
Multi-Line and Concatenated Secret Handling | Secrets split across multiple lines or constructed via string concatenation are detected and redacted as a single unit | A secret built with | Test with 10 code samples that construct secrets dynamically; verify the final assembled secret value is redacted, not the individual fragments |
Contextual Awareness in Comments | Secrets embedded in comments, docstrings, or error messages are redacted with the same rigor as secrets in code | A comment like | Include secrets in code comments, log statements, and string literals in the test suite; assert all instances are caught regardless of syntactic position |
Output Completeness and No Data Loss | The output contains every line of the input code; no lines are dropped, truncated, or summarized | The output is shorter than the input or contains ellipsis or summary statements like | Assert line count of output equals line count of input; diff input and output to confirm only secret values changed |
Enabling Efficiency, Speed & Accuracy
Intelligent Analysis, Decision & Execution
We build AI systems for teams that need search across company data, workflow automation across tools, or AI features inside products and internal software.
Talk to Us
Search across company data
Give teams answers from docs, tickets, runbooks, and product data with sources and permissions.
Useful when people spend too long searching or get different answers from different systems.

Automate internal workflows
Use AI to route work, draft outputs, trigger actions, and keep approvals and logs in place.
Useful when repetitive work moves across multiple tools and teams.

Add AI to products and internal tools
Build assistants, guided actions, or decision support into the software your team or customers already use.
Useful when AI needs to be part of the product, not a separate tool.
Adapt This Prompt
How to adapt
Start with the base prompt and a small set of known secret patterns (AWS keys, Bearer tokens, password=). Use a simple regex pre-filter to reduce LLM calls, then pass only matched lines to the model. Keep the output schema flat: a list of {line, match, replacement} objects.
code[CODE_SNIPPET] Replace all API keys, tokens, and secrets with placeholder variables. Return JSON: [{"line": int, "match": string, "replacement": string}]
Watch for
- High-entropy strings that are not secrets (base64-encoded images, hashes) being flagged
- Missing multi-line secret patterns (JSON configs, .env files)
- No confidence scoring, so every match looks equally certain

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.
Partnered with leading AI, data, and software stack.
How We Work
Custom AI workflows for your Business
One-fit-all AI don't work for modern businesses. At Inferensys, we aim to understand your business & custom requirements; which we use to define most efficient agentic workflows, the data, and the tools for your business.
01
Review the use case
We understand the task, the users, and where AI can actually help.
Read more02
Pick the right approach
We define what needs search, automation, or product integration.
Read more03
Build the first useful version
We implement the part that proves the value first.
Read more04
Improve from there
We add the checks and visibility needed to keep it useful.
Read moreThe first call is a practical review of your use case and the right next step.
Talk to Us