An agent's context is not a private place. It is logged, summarised, sent to providers, quoted back at users and, under a competent injection, read aloud. Anything placed inside it should be treated as eventually public.
The short answer
Keep real secrets out of the agent's context entirely: no API keys, passwords, tokens or private keys in prompts, tool results, documents it reads or its own outputs. Instead, give the agent a reference — a credential id — and let the gateway or a broker exchange that reference for the real secret at call time, outside the model's view. Audit the places secrets leak into context by accident: verbose tool errors, config echoes, debug logs pasted into chats. The rule is simple to state and worth enforcing mechanically: if the model can read it, it is not a secret anymore.
Check your own setup as you read: run the free Spend Guard self-check or test a policy in the playground — five minutes, no signup, and the gaps it names map to the controls below.
Worked example: the API key in the agent's own summary
A fictional SaaS firm, Copperline Systems, discovered its deployment agent had been including a fragment of a cloud access key in its run summaries. The key had been present in an environment dump a tool returned during debugging — the agent, summarising 'relevant context', faithfully carried the interesting-looking string forward into three weekly reports distributed to a 40-person channel. The key was low-privilege and was rotated within the hour once noticed, but the postmortem's finding was structural: the secret had entered the one place guaranteed to broadcast it.
Copperline moved to brokered credentials. Tools now receive secrets from the gateway at execution time; the agent holds only credential ids like 'deploy-key-prod', and tool outputs are filtered for high-entropy strings before they re-enter context. A scanner runs over agent transcripts and summaries weekly, hunting key-shaped patterns. In six months it has flagged two real finds — both third-party tokens inside customer-supplied logs the agent was asked to summarise — each quarantined and the customer notified, which the firm now counts as a feature it can describe to enterprise buyers.
Context is a broadcast medium
Design as if everything the agent reads will eventually be repeated to someone it should not reach — because across logs, summaries, provider retention, screenshots and extraction attempts, that is the honest planning assumption. This reframes secret-handling from 'be careful what you paste' to architecture: secrets must travel a path that never intersects the model's context. The gateway and tool layer can hold and use secrets; the reasoning layer can know that a credential exists and what it is for, and nothing more.
Brokered credentials: references in, secrets out
The working pattern is a broker: the agent's policy names credential ids per tool; when a call is authorised, the execution layer fetches the real secret, attaches it to the outbound call, and strips it from any response before the response re-enters context. The agent can then work with powerful integrations while being literally unable to reveal their keys — you cannot leak what you have never seen. This also simplifies rotation: the secret changes in one vault, and no prompt, config file or transcript anywhere needs to change with it.
Accidental ingestion is the common path
Deliberate pasting is rare; accidents are weekly. Tool errors that echo connection strings, config files read 'for context', a customer's log bundle containing their own tokens, a screenshot transcribed with a visible password — each places a secret into context innocently. Defences are mechanical: output filters that redact key-shaped strings before they reach the model, tool wrappers that return curated fields instead of raw dumps, and a transcript scanner that hunts patterns after the fact and raises a rotation task when it finds one.
What the agent may know about its secrets
An agent can know a credential's name, purpose, scope and expiry — metadata that helps it choose the right tool and explain a refusal. It should never know the value. When an agent reports 'the production deploy credential expired', that is the system working; when it can quote the credential, the architecture has failed in a way no instruction to 'keep this confidential' will fix. Instructions are read by the same context that holds the secret; architecture is what the context cannot do.
The exit interview for a departing integration
Secrets hygiene has a neglected moment: when an integration or vendor is retired. The agent loses the tool, but copies of the credential often survive — in the vault nobody pruned, in old configuration, in transcripts from its working life. Add a step to every decommission: revoke the credential at the provider, delete it from the broker, and search transcripts and summaries for its fragments. The same applies when a team member who held keys departs. It is unglamorous work, and it is precisely the work that determines whether last year's integration becomes next year's incident through a credential everyone forgot existed.
Five actions to get secrets out of agent context
- Inventory where secrets can enter context: prompts, tool outputs, documents, error messages and pasted logs.
- Move to brokered credentials — the agent holds credential ids, the execution layer fetches and attaches real secrets outside the model's view.
- Filter tool outputs for key-shaped and high-entropy strings before they re-enter the agent's context.
- Scan transcripts and summaries weekly for secret patterns, and auto-create a rotation task on any find.
- Let agents know credential metadata — name, scope, expiry — and never the value.
Source note and related guides
Figures in the worked example are illustrative, drawn from a fictional company created for this guide; the controls, failure patterns and procedures described are the gateway's documented behaviour — see the Agent Auth Gateway documentation and the policy patterns in allowlists vs denylists, spend caps and the agent kill switch. Reviewed by the Ingenious AI team, 2026-11-14.
Sources
- RFC 6749, The OAuth 2.0 Authorization Framework, the IETF standard behind scoped, expiring access tokens: rfc-editor.org, 2012
- Model Context Protocol specification — Authorization, the MCP standard for OAuth-based authorization of MCP servers: modelcontextprotocol.io, 2025
- NIST AI Risk Management Framework (AI RMF 1.0), the voluntary framework for managing AI risk across design, development and use: nist.gov, 2023