How Do You Keep Secrets Out of AI Agent Logs?
Keep secrets out of AI agent logs: keys in env vars or a secret manager, never in prompts, redact before logging, record what changed, rotate on leaks.
Reviewed by WaveBrain Operations TeamReviewed 9 min read
The short answer
You keep secrets out of AI agent logs by never putting them where the agent can read or repeat them: keys live in environment variables or a secret manager, not in prompts, and every log entry records what changed, not the credential used. Add redaction before anything is written, give each agent a scoped key, and rotate any key that does leak, because deleting the log line doesn't make it safe again.
Every AI agent you run touches credentials. It needs an API key to deploy, a token to post, a password to sign in to a dashboard. Sooner or later one of those values ends up somewhere it shouldn't: in a prompt you pasted, in a command the agent printed, or in the summary it wrote after the job. Logs are where those leaks pile up, because they are kept, shared and read by more people than the agent's chat ever was.
This guide is for small teams and solo founders who run agents like Claude Code, Codex, Cursor or Hermes. It covers where secrets leak into agent logs, the steps that keep them out, and what to do when one gets through anyway.
Why do secrets end up in AI agent logs?#
Secrets end up in agent logs because agents repeat what they see. If a key sits in a prompt, a config file the agent reads or the output of a command it runs, the model can copy it into a summary, an error message or a log entry without knowing it matters.
OWASP's Top 10 for LLM Applications lists this as LLM02:2025 Sensitive Information Disclosure, and it names "security credentials" among the data at risk. It also warns that telling the model in its system prompt what not to return "may not always be honored and could be bypassed via prompt injection or other methods." So an instruction like "never print the key" helps, but it can't be your only control.
Logs make the problem worse in two ways. They last longer than the session, and more people and tools can read them. OWASP's Logging Cheat Sheet lists "An attacker with read access to a log uses it to exfiltrate secrets" as a known attack on logs.
What should never go into an agent log?#
Credentials of any kind should never go into an agent log, along with the personal and payment data a log reader doesn't need. The OWASP Logging Cheat Sheet's "Data to exclude" list says these should usually be removed, masked, sanitized, hashed or encrypted instead of logged directly:
- Access tokens and session identification values
- Authentication passwords
- Database connection strings
- Encryption keys and other primary secrets
- Bank account or payment card holder data
- Sensitive personal data such as health records or government identifiers
- Application source code and commercially sensitive information
For agents, add one-time codes, recovery codes, webhook URLs that carry a token, signed URLs and full account IDs. If a value would let someone act as you or your agent, it doesn't belong in a log.
How do you keep secrets out of agent logs, step by step?#
You keep secrets out with seven habits, most of which take minutes to set up. The first three stop secrets from reaching the agent's text at all, which is far more reliable than cleaning them up afterwards.
- Never paste API keys, tokens or passwords into a prompt, a chat or an instruction file. Once a key is in the conversation, the model can repeat it anywhere.
- Store keys in environment variables, the system keychain or a secret manager, and let tools read them from there. Many MCP clients can reference a variable instead of the raw key in their config.
- Give each agent its own scoped, short-lived key. A key that can only post to one channel or deploy one project does less damage if it leaks, and you can revoke one agent without breaking the rest.
- Redact before anything is written. Mask known secret patterns in your own scripts and CI before output reaches a log, and keep request headers and bodies out of debug output.
- Log what changed, not how. "Deployed the pricing page to production" is the useful entry; the token, connection string or command line with the key in it is not.
- Scan for leaks. Turn on secret scanning for your repositories and grep your agent logs for key prefixes during the weekly review.
- Rotate any key that leaks, right away, then remove the copy from the log.
Step 5 does most of the work in practice. An entry written as a plain past-tense sentence about the outcome rarely has a reason to contain a credential at all.
Where do secrets leak in a typical agent setup?#
Secrets leak at five points in most agent setups: the prompt, config files, command output, the agent's own summaries and third-party logs. Each one has a simple fix.
| Leak point | Example | Fix |
|---|---|---|
| Prompt or chat | "Here's the Stripe key, use it to refund order 5521" | Put the key in an env var or the tool's credential store |
| Config files | Raw key in an MCP config you share or back up | Reference a variable such as ${WAVEBRAIN_API_KEY} instead |
| Command output | A script prints the request with its Authorization header | Mask the value and stop printing headers |
| Agent summaries | Log entry details include the connection string it used | Instruct the agent to describe the change, and review entries |
| CI and third-party logs | A workflow echoes a secret to the build log | Use the platform's secret store and masking |
GitHub Actions is a good example of the last row. Its docs tell you to "mask all sensitive information that is not a GitHub secret by using ::add-mask::VALUE", which makes the runner redact that value from logs. They also warn that with some workarounds "GitHub does not redact secrets that are printed in logs", so masking only covers values the platform knows are secret.
How do you redact secrets before they're logged?#
You redact secrets by filtering text on your side before it leaves the machine, using patterns for the key formats you actually use. OWASP's LLM02 entry recommends tokenization and redaction, noting that "techniques like pattern matching can detect and redact confidential content before processing."
In practice that means a small filter in front of any script or hook that sends log text somewhere. Match the prefixes of the keys you hold (for example sk-, ghp_, xoxb- and Bearer ), JWT-shaped strings and long random strings, and replace each match with a placeholder such as [redacted]. Keep the filter strict and boring: it is a safety net for the agent that forgot step 5, not a reason to let secrets near the log in the first place.
Two limits are worth knowing. Pattern matching misses secrets it has no pattern for, such as a plain password. And redaction only protects the log you control, so it doesn't help if the key was already sent to a model provider or a third-party tool in a prompt.
What do you do if a secret leaks into a log?#
Rotate the secret first, then clean up the log. Treat a leaked key as already compromised, because anyone who read the log while it was there may have copied it.
GitHub's guide to remediating a leaked secret says "you should consider any leaked secret to be immediately compromised" and that removing it from the codebase, pushing a new commit or recreating the repository "do not prevent the secret from being exploited." Its secret scanning docs make the same point: rotate the credential immediately, and history cleanup is "often unnecessary if you've already revoked the credential."
- Revoke or rotate the key at the service that issued it.
- Check that service's own logs for use of the old key since the leak.
- Delete or redact the log entry that contained it.
- Find out how it got there: a prompt, a config file, a command, a summary.
- Fix that path, so the next run can't repeat it.
Our guide on what to do when an AI agent makes a mistake covers the same contain, fix and prevent loop for other kinds of errors.
How do you review agent logs for leaked secrets?#
You review agent logs for secrets the same way you review them for mistakes: on a schedule, with a short checklist. A weekly pass of 10 to 15 minutes is enough for most small teams.
- Search the log for key prefixes,
password,token,Bearerand://with credentials in the URL. - Open a few entries from each agent and read the details field, not only the title.
- Check which agents hold which keys, and revoke keys no agent has used in weeks.
- Confirm secret scanning is on for every repository your agents push to. GitHub's secret scanning "scans your entire Git history on all branches" for API keys, passwords and tokens.
- Restrict who can read the logs. OWASP says "the privileges to read log data should be restricted and reviewed periodically."
If you're still deciding what a good entry should contain, our guide to what an AI employee activity log should record has field-by-field examples, and our guide to giving an AI agent access to tools safely covers scoped keys and approvals.
Where does WaveBrain fit?#
WaveBrain is an activity log built around step 5: agents report the outcome of each action in a plain sentence, not the credentials or commands behind it. It's a hosted log where you read what every agent did in one place.
Agents connect through WaveBrain's MCP server, which gives them a log_action tool, and scripts and cron jobs log the same fields with one REST request to https://getwavebrain.com/api/v1/actions. The server's instructions tell every connected agent to "never put secrets, passwords, codes or full IDs in a log", and the details and metadata fields repeat that rule in their descriptions. The setup steps for Codex and Hermes read the WaveBrain key from the WAVEBRAIN_API_KEY environment variable instead of storing it in the config file. The API keys page suggests a separate key per machine or agent, so you can revoke them one at a time, and you can delete a single entry from the dashboard if something sensitive gets through. Slack alerts carry an action's title, project, type and status and leave out its details. See what gets logged for the full list.
Be clear about what it isn't. WaveBrain has no server-side secret filtering: it stores the text agents write, so a key an agent puts in an entry is stored as written, and the redaction and review steps above still apply. It records only what agents report, so an agent that skips log_action leaves no entry, and it isn't a tamper-proof audit log. It isn't LLM tracing either, and it doesn't record prompts, responses or token costs. The Free plan includes 1,000 actions a month, 7-day history and 2 API keys. Pro is $12 a month for 50,000 actions, 90-day history and 50 API keys. You can start free with no credit card.
Frequently asked questions#
Is it safe to paste an API key into an AI agent's chat?#
No. Anything in the conversation can be repeated in a summary, an error or a log entry, and it may be sent to the model provider. Store the key in an environment variable or secret manager and let the tool read it there.
Does telling the agent "never log secrets" work?#
It helps, but it isn't enforced. OWASP warns that prompt restrictions "may not always be honored", so pair the instruction with keys the agent never sees, redaction and a weekly review.
Is deleting the log entry enough after a leak?#
No. Rotate or revoke the key first, because anyone who read the log may already have it. GitHub's guidance treats any leaked secret as immediately compromised.
What should an agent log instead of the credential?#
The outcome: what changed, where and whether it worked, such as "Rotated the Stripe webhook secret for the shop project". Name the key or service if you need to, never its value.
Does WaveBrain filter secrets out of log entries?#
No. Agents are told never to put secrets in an entry, but WaveBrain doesn't filter the text on the server, so treat each entry like a commit message and keep redaction on your side.
Sources#
- OWASP Cheat Sheet Series: Logging Cheat Sheet
- OWASP Top 10 for LLM Applications: LLM02:2025 Sensitive Information Disclosure
- GitHub Docs: Secret scanning
- GitHub Docs: Remediating a leaked secret in your repository
- GitHub Docs: Using secrets in GitHub Actions
- WaveBrain homepage: what gets logged and pricing