SYNTROX.ai
Blog

Keeping Secrets Out of an AI Agent's Context Window

5 min read

Never put an API key, token or password anywhere a model can read it: not the system prompt, not memory, not a tool description, not a file in its workspace. A model cannot keep a secret it has seen. Broker credentials outside the model instead, so each tool attaches a short-lived, task-scoped token at call time, and scan traces, memory and outputs for any secret that still gets through.

On this page

Few engineers would paste a production database password into a chat window. It is easy for an agent to end up with one anyway: in a system prompt explaining how to call an internal API, in a tool description with an example request, in a memory entry saved during a debugging session, or in a .env file in the workspace the agent can read.

Everything in an agent's context window can come back out. A model has no way to hold a value it can see while promising never to repeat or use it. The only secret an agent keeps is one it never saw.

Why API keys don't belong in an agent's prompt

OWASP's entry on System Prompt Leakage (LLM07:2025) starts from the right premise: "the system prompt should not be considered a secret, nor should it be used as a security control." Credentials and connection strings, it says, should not be in it. Its first mitigation is to move sensitive information "to the systems that the model does not directly access".

The same reasoning covers every other part of the context:

  • Tool descriptions and schemas are sent to the model alongside the system prompt. A key in an example request is a key in the prompt. Tool descriptions are an attack surface in their own right, too.
  • Memory. OWASP's Top 10 for Agentic Applications describes, under ASI03 Identity and Privilege Abuse, agents that "cache credentials, keys, or retrieved data for context and reuse". Its example is an IT admin agent that caches SSH credentials during a patch, which a non-admin later prompts it to use. Its mitigation is to wipe state between tasks.
  • Tool results. An agent that runs env, reads a configuration file or receives a stack trace with a connection string in it now has the secret in context, whether or not anyone intended it.

How secrets leak out of an agent's context

Once a secret is in context, there are many ways out: in the agent's reply, in the arguments of a later tool call, in a URL it fetches, in the traces your observability stack keeps.

And the context extends to anything the agent can read. In August 2025 Johann Rehberger showed Amazon Q Developer for VS Code reading a developer's .env file and leaking its first line through a DNS lookup. The instructions sat in a comment in a source file. When the developer asked the agent to explain the file, it ran ping with the secret as part of the hostname, and ping was among the commands that ran without confirmation. AWS's security bulletin AWS-2025-019 says Language Server v1.24.0, released on 29 July 2025, requires human confirmation for ping and dig.

No one put that key in a prompt. It was within reach, and that was enough.

How to give an AI agent credentials without exposing them

The pattern that works is the one used for any untrusted client. The agent asks for an action; something outside the model holds the credential and performs it.

Broker the call. The agent calls create_ticket, not a raw HTTP request with an Authorization header it wrote itself. The tool server, or a proxy in front of it, fetches the credential from a secrets manager at call time and attaches it. The model sees the result, never the key.

Make the credential short-lived and scoped. Under NHI2 Secret Leakage, OWASP's Non-Human Identities Top 10 recommends replacing static secrets "with short-lived, ephemeral credentials that are generated on-demand", and restricting each secret to the services that need it. The OWASP Secrets Management Cheat Sheet describes dynamic secrets that live only as long as their consumer, with a warning worth keeping: "stopping the consumer alone does not revoke the secret". Tie expiry to the task, not to the agent process.

Don't pass user tokens through. If the agent acts for a user, each token should be issued for the service that receives it. The MCP specification's security best practices call token passthrough an anti-pattern that the authorisation specification explicitly forbids. Our post on agent identity and delegation covers audience-bound and sender-bound tokens.

Keep secrets out of the workspace. No .env files, credential files or forwarded SSH agents anywhere the agent's file and shell tools can reach.

Where the secret sitsWhat a manipulated agent can do with it
System prompt or tool descriptionRepeat it to anyone who asks the right way
MemoryReuse it in a later session, for a different user
A file in the workspaceRead it when an injected instruction says to
A broker outside the modelNothing; it only sees results

Secret scanning on AI agent traces

Brokering makes it rarer for a secret to reach the context. It does not make it impossible: a tool can still return an error with a connection string in it. So scan where secrets would land.

NHI2 notes that leaked secrets are hard to detect "given the sheer variety of data stores in which they may appear", application logs among them. Agents add three more: traces, memory stores and outputs.

  • Traces. Scan exported traces with the tools you already use on repositories. Gitleaks, for example, scans "git repos, files, and whatever else you wanna throw at it via stdin". The OpenTelemetry Collector's redaction processor removes attributes that are not on an allowlist and masks values matching blocked patterns before export. What to keep, and for how long, is covered in PII and secrets in agent traces.
  • Memory. Scan entries before they persist, not after.
  • Outputs and actions. GitHub's push protection for its MCP server is an example at the tool boundary: it blocks secrets in AI-generated responses and in actions such as creating an issue.

When a scan finds a live secret, treat it as exposed. The cheat sheet's containment order is revoke, rotate, delete, and it adds that "secrets in logs must have a process for removing the secret while maintaining log integrity".

A secrets checklist for AI agents

  1. Have you searched every system prompt, tool description and agent configuration for keys, tokens and connection strings?
  2. Can any agent's file or shell tool read a .env file, credential file or SSH key?
  3. Does each tool attach its own credential at call time, or does the model handle it?
  4. Do credentials expire with the task?
  5. Are traces, memory and outputs scanned for secrets, and who is told when one is found?

Where Syntrox fits

Nucleus, our judge, is small enough to run on hardware inside your own network, so the traces a check reads, and anything sensitive that slipped into them, do not have to leave it. It treats any text inside the material it evaluates as evidence, never as instructions. Syntrox records, for each trace it evaluates, the score, confidence and judge version, outside the agent.

5 min read

PII and Secrets in AI Agent Traces: How to Keep an Audit Trail Without Keeping Everything

Agent traces capture prompts, retrieved documents, tool arguments and tool results, so once content capture is on they fill with personal data and credentials. Keep the audit trail but minimise what goes into it: remove secrets before storage, pseudonymise identifiers you need to correlate, keep bulky content apart from the action record, restrict and log access, and delete each kind of data on its own schedule.

Read

5 min read

AI Agent Identity: Why Shared Service Accounts Break Authorisation, and What Replaces Them

Give every agent its own identity, and when it acts for a person, put both in the credential: the user it acts for and the agent doing the acting. OAuth 2.0 Token Exchange (RFC 8693) defines exactly that delegation. Then make each credential short-lived, scoped to the task and bound to one audience, so a leaked or misused token is worth little and every action traces back to an agent and a person.

Read

5 min read

MCP Tool Poisoning: How a Tool Description Hijacks an AI Agent

A malicious MCP server can hide instructions in a tool's description, which the model reads and the user never sees. In Invariant Labs' demonstration, an innocent-looking "add" tool made an agent read the user's SSH key and send it out as a tool argument. The defences: pin and review tool descriptions, show full arguments before approval, scope each server's credentials, and check every tool call — the action and its arguments — before it runs.

Read