By Abhijith N M · Reviewed 24 October 2026 · Editorial standards
A nightly job fails on a Tuesday. An engineer copies the API key into a notebook to reproduce the error, commits the notebook 'for now', and moves on.
Five weeks later a scanner finds the key in a public fork. The key still works, because nothing in the process required it to be revoked. That week's bill shows requests the team never made.
Two failures caused this, and only one is the commit. The key was written where it could be copied, and rotation depended on a person remembering.
No. Deleting a commit removes one copy. Clones, forks, CI caches, shell history and the provider's logs keep others. A key that leaked must be treated as used, so the response includes a spend review, not only a delete.
A secret is a string that grants access to whoever holds it: an API key, an access token, a password, a private key, a connection string, a signed URL.
A Claude API key looks like sk-ant-.... The format is not the point. Possession equals permission, which makes a key a password rather than a configuration value.
That one property drives every rule here. A configuration value can be copied freely. A password cannot, so it must be injected at runtime, scoped to a task, rotated on a schedule and revoked on demand.
| Place | How it happens | Control |
|---|---|---|
| Source code and notebooks | A debug paste, or a hard-coded default | A pre-commit secret scan, and review |
| Logs and traces | A full request dump or an error object | Redact at the logging boundary |
| Prompts and tool results | A key pasted in, or returned by a tool | Never put a key in a prompt; filter output |
| Client bundles | A build-time variable that ships to the browser | Proxy the call through your server |
| Configuration files | A committed .env, or a literal token | An environment variable or a secret reference |
Three of those rows are created by a tool rather than a person: a logger, an error serialiser and a build step. A control that depends on people remembering will not keep pace with three automatic copiers, so the control has to be automatic too.
The rule for configuration is short. The repository holds the name of a secret, and the runtime holds the value.
ANTHROPIC_API_KEY with os.environ and fail loudly at startup when it is missing.${ANTHROPIC_API_KEY} into configuration instead of the value.Rotation has four steps, and the order matters: issue the new credential, deploy it, revoke the old one, then confirm the old one fails. A rotation that skips the revoke step has doubled your attack surface while everyone believes it shrank.
Personally identifiable information (PII) is data that identifies a person, alone or in combination: a name, an email, a phone number, a card number, a government id, an account number. Free text usually carries more of it than a structured field.
Treat every prompt, response and trace as a data store. A request reaches a provider, your logs, your evaluation sets and often a cache. Handling PII therefore starts before the call.
CUST_8812, while the real value stays in your database.The flow fails in a specific way worth naming. If redaction misses a value, the call still succeeds, so nothing breaks and no alert fires. Only a test that asserts on a fixture catches it.
A redactor is a small piece of code with a large responsibility. Write it once, test it on a fixture that looks like production text, and keep the mapping out of the prompt.
import re
PATTERNS = {
'EMAIL': re.compile(r'[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,}'),
'CARD': re.compile(r'\b(?:\d[ -]?){12,18}\d\b'),
'PHONE': re.compile(r'\+?\d[\d ()-]{7,}\d'),
'API_KEY': re.compile(r'sk-ant-[A-Za-z0-9_-]{8,}'),
}
def redact(text: str) -> tuple[str, dict[str, str]]:
mapping: dict[str, str] = {}
for label, pattern in PATTERNS.items():
def swap(match: re.Match) -> str:
token = f'<{label}_{len(mapping) + 1}>'
mapping[token] = match.group(0)
return token
text = pattern.sub(swap, text)
return text, mapping
TEXT = (
'My card 4111 1111 1111 1111 was charged twice. '
'Call me on +1 415 555 0132 or email ana.torres@example.com. '
'The key sk-ant-api03-FAKEKEYFORTESTING is in the old job.'
)
clean, mapping = redact(TEXT)
print(clean)
print(sorted(mapping))Add this fixture to your test suite and assert on both lines. When a new identifier type appears, add one pattern and one case in the same commit, so coverage grows with the data.
Key takeaways
A secret grants access to whoever holds it, so treat a key as a password rather than a setting.
Secrets leak through source, logs, prompts, client bundles and config; three of those are copied by tools.
The repository holds the name of a secret; the runtime holds the value.
Rotate in order: issue, deploy, revoke, confirm. Revocation is the only step that reaches every copy.
Redact PII before the call, tokenise identifiers, and rehydrate only after output checks pass.
Test the redactor, because a missed value fails silently and produces no alert.
Written and edited by the nexGenCoders team. Spotted a mistake? Tell us and we will fix it.
Don't lose this lesson
Create a free account to save your progress, keep your streak, and work toward a shareable certificate.