An AI agent needs credentials to call its tools, but every place a credential can appear in the agent's world, the prompt, the context window, tool output, memory, traces or the environment of a child process, is a place where the model can repeat it and injected text can ask for it. Runtime secrets injection is the set of delivery patterns that put a credential exactly where the outbound call is made, at the moment it is made, and nowhere the reasoning loop can read. This guide goes deeper than general vaulting: it compares the injection points (tool layer, credential proxy, sandbox file mount, process environment), shows how to stop secrets leaking back through tool results, logs and memory, and sets out how to test that the model really never sees a key. It is for platform and security engineers building agent runtimes, MCP servers and agent tooling. For the vault architecture and release policy that sit behind injection, see the Credential Vaulting for AI Agents Guide.
Key takeaways
- Inject at the call, not at the agent. The component that makes the outbound request fetches or receives the credential; the model only ever names the tool and the arguments.
- The prompt is not a vault. OWASP LLM07:2025 states that the system prompt should not be considered a secret or used as a security control, and that credentials and connection strings should not be in it.
- Environment inheritance is the quiet leak. Local MCP servers and tools launched by an agent client often inherit every credential in the parent environment; launch them with an explicit, minimal environment.
- The return path matters as much as the delivery path. Tool results, error messages, traces and agent memory can carry a secret back into the context window, so redact and scan on the way back.
- Prove it with canaries. Seed tagged dummy credentials and test that they never appear in prompts, transcripts, logs or memory stores.
Where credentials leak into the reasoning loop
Agents differ from ordinary services in one decisive way: anything in their context can be emitted as output, and anything in their environment can be found by a tool they control. MITRE ATLAS records this as AML.T0083 Credentials from AI Agent Configuration, noting that credentials for an agent's tools are frequently stored in configuration files for convenience, where an attacker who can read the configuration obtains direct access to the systems behind it. ATT&CK's T1552.001 Credentials In Files describes the same pattern on ordinary hosts.
| Exposure path | How the secret gets there | How it leaves |
|---|---|---|
| System prompt or instructions | Developer pastes an API key so the model can "use" a service | Prompt extraction, verbose errors, the model quoting its instructions |
| Tool definitions and arguments | The key is a tool parameter the model must fill in | Appears in every call, transcript and trace; injected text can redirect it |
| Workspace files | .env files, CLI configs, agent config with inline tokens | File-reading or shell tools; the agent searching when it gets stuck |
| Process environment | The agent client or MCP server inherits the developer's or host's variables | Child processes, crash dumps, debug output, a tool that prints its environment |
| Tool results and errors | A downstream API echoes the auth header or connection string in an error | Back into the context window and from there into output or memory |
| Memory and traces | Long-term memory or observability stores capture full prompts and tool payloads | Retrieval in later sessions; broad read access to the trace store |
The Amazon Q MCP Config Vulnerability 2026 shows the environment path. MCP servers defined in a repository's configuration were launched as local processes that received the developer's full environment, including cloud session tokens, API keys and SSH agent access. The fix added a trust prompt; the general lesson is that agent tooling should start child processes with a minimal environment rather than inheriting every credential.
Injection points compared
There are four places to inject a credential. They differ in what the agent process can ever observe, and most production designs use the first two together.
| Injection point | How it works | What the agent can see | Best fit |
|---|---|---|---|
| Tool or MCP server layer | The tool implementation authenticates to a broker or vault with its own workload identity, fetches a scoped credential per call or per lease, and makes the request | Tool name, arguments, sanitised result | Remote MCP servers, internal tool services, anything you write yourself |
| Credential proxy (egress injection) | The agent or tool sends an unauthenticated request to a local or network proxy, which matches the destination and adds the auth header or signs the request | The request without credentials | Third-party APIs called by generic HTTP tools, coding agents in sandboxes |
| Sandbox file mount | The orchestrator mounts a short-lived credential file into the tool's container or sandbox at run time, readable only by the tool process | Nothing if the model has no file tool in that sandbox; everything if it does | Batch tools, CLIs that read credential files, Kubernetes workloads |
| Process environment | A supervisor sets variables when it starts the tool process | Whatever the process or its children print | Legacy tools that only read environment variables; last resort |
The OWASP Secrets Management Cheat Sheet ranks the container options the same way: a sidecar that fetches secrets into an in-memory volume is preferred, runtime-mounted files are acceptable, and environment variables are a last resort because they are visible to all processes and can appear in logs and dumps. Secrets must never be built into images. See Environment Variables and .env Files for the wider case against environment-based secrets.
Decision guidance: if you control the tool code, inject at the tool layer and use a workload identity to reach the broker, which removes the static bootstrap secret (see the Secrets Zero Problem Guide). If the agent uses generic HTTP or CLI tools against third-party APIs, put a credential proxy in front of them and give the sandbox no credentials at all. Use file mounts for tools that cannot be changed, and keep any file tool the model controls out of that sandbox. Use environment variables only for tools that accept nothing else, and only with a scrubbed, explicit environment.
Designing the tool-layer pattern
- Give each tool its own identity. The tool or MCP server authenticates as a workload (cloud workload identity, a projected Kubernetes service account token, an mTLS certificate), not with a static key in its configuration.
- Request the narrowest credential per call. The broker issues a credential scoped to the operation and, where the downstream supports it, to the delegating user. Dynamic secrets with short leases are ideal; see the Dynamic Secrets and Ephemeral Credentials Guide.
- Keep the credential out of the schema. No tool parameter carries a key, token or password. If a tool definition asks the model for a credential, redesign it.
- Bind tokens to their audience. The MCP security best practices state that MCP servers MUST NOT accept tokens that were not explicitly issued for the MCP server, which rules out passing a client's token through to a downstream API. Exchange it for a downstream token instead; see the MCP Authorisation Specification Guide.
- Hold credentials in memory only. Fetch, use and discard; do not write to temp files, caches or the tool's own logs. Where a renderer must write a file, set restrictive permissions explicitly (for example, HashiCorp Vault Agent templates default to 0644 when no
permsvalue is set, so set one). - Return results, not plumbing. Shape the tool result to the data the task needs. Strip request headers, connection strings and raw upstream error bodies before the result reaches the model.
Designing the credential proxy pattern
A credential proxy lets you give a coding agent or sandboxed tool network access to an API without giving it the key. The sandbox's only route out is the proxy, and the proxy holds the mapping from destination to credential.
- Match on destination, not on request content: inject the credential for
api.example.comonly when the TLS destination is that host, so a prompt cannot redirect the key to another domain. - Use placeholders where a tool insists on a key: configure the tool with a dummy value; the proxy replaces it with the real credential on egress to the approved host and refuses the request if the placeholder is sent anywhere else.
- Scope per sandbox and per task: each agent session gets a proxy policy listing the hosts and methods it may call, which doubles as an egress allowlist. See the Agent Egress Control and Network Isolation Guide.
- Protect the proxy's own channel: the MCP best practices note that local services reachable over HTTP should require an authorisation token or use restricted IPC such as Unix domain sockets, so other local processes cannot use the proxy.
- Log at the proxy: every injected request becomes an auditable event tied to an agent session, without the credential appearing in any agent-side log.
Locking down local agents and MCP servers
On developer workstations, agent clients start local MCP servers and tools as child processes. Those processes run with the user's privileges, and by default they often see the user's environment and files. The MCP security best practices require one-click local server configuration to show the exact command and obtain explicit consent, and recommend running local servers sandboxed with minimal default privileges and restricted file system and network access.
- Explicit environment: configure each local server with only the variables it needs; do not let it inherit the shell environment that holds cloud sessions and tokens.
- Short-lived developer credentials: use short cloud sessions issued through single sign-on rather than long-lived access keys in profile files, so whatever a tool does find expires quickly.
- Keep secrets out of the workspace: the OWASP AI Agent Security Cheat Sheet recommends blocking tool access to sensitive file types such as
.env,.keyand.pem. Add credential directories to the agent's deny list too. - Treat repository configuration as untrusted: MCP server definitions, agent config files and tool manifests that arrive with a repository need review before they run. See the Local MCP Server Security Guide and Coding Agent Configuration File Attacks.
The same principle applies to hosted automation platforms that connect agents to SaaS apps: credentials should live in the platform's connection store and be applied by the connector, not typed into prompts or instructions. See the Zapier AI Security Guide for one platform.
Keeping secrets off the return path
Delivery is half the problem. A credential that never enters the prompt can still arrive in the context window through a tool result, and from there reach logs, traces, memory and the user.
| Return channel | Control | Notes |
|---|---|---|
| Tool results | Allowlist the fields returned; scan for credential patterns before handing to the model | Upstream errors are the usual culprit: replace raw bodies with a status and a safe message |
| Application and tool logs | Structured logging with known secret fields dropped; pattern-based masking as a second layer | The OWASP AI Agent Security Cheat Sheet calls for redacting passwords, API keys, tokens and credentials before writing logs |
| CI and pipeline logs | Platform masking for registered secrets, plus explicit masks for derived values | GitHub Actions masks registered secrets and values marked with ::add-mask::, and warns that values handled through some workarounds are not redacted |
| Observability traces | Keep prompt and tool payload capture off by default; enable per environment with redaction | Trace stores often have broader read access than the vault; treat them as sensitive data |
| Agent memory | Scan content for credential patterns before it is persisted; encrypt or refuse | OWASP recommends not storing sensitive data in memory without encryption or redaction. See the AI Agent Memory Security Guide |
If a secret does reach a log or store, the OWASP Secrets Management Cheat Sheet's response applies: revoke it immediately, rotate it through the automated process, and remove it from the exposed system while preserving log integrity.
Implementation roadmap
- Find the secrets the agents can reach. Scan prompts, tool definitions, agent and MCP configuration files, workspace files, container images and the environment of each agent process for credentials. Record each one with its owner and scope.
- Remove credentials from prompts and schemas first. These are the highest exposure and the easiest to fix: move each into the tool layer and rotate it, because it must be assumed exposed.
- Introduce the broker path. Give tools workload identities and have them fetch scoped credentials at call time. Pilot with one agent and two or three tools; keep the old static credential valid but unused during the pilot so rollback is a configuration switch.
- Put a credential proxy in front of sandboxed agents. Start in log-only mode to learn which hosts are called, then enforce the allowlist and remove credentials from the sandbox.
- Scrub environments. Change agent clients and supervisors to launch tools with explicit environments. Test each tool still starts; fix tools that silently relied on inherited variables.
- Close the return path. Add result shaping, log redaction, trace capture defaults and memory scanning. Then seed canary credentials and run the tests below.
- Retire the static credentials. Once telemetry shows no use, disable and then delete them, keeping the disable step reversible for one release cycle.
Testing that the model never sees a key
- Canary credentials: issue a uniquely tagged dummy credential through each injection path and search prompts, transcripts, traces, logs and memory stores for the tag after every test run. Any hit is a failure. Canary design is covered in AI Agent Honeytokens and Canaries.
- Adversarial prompts: include test cases that ask the agent to print its configuration, list environment variables, read credential files or repeat tool headers. The expected result is that the agent has nothing to reveal, not that it refuses.
- Error injection: force upstream authentication failures and check what the tool returns to the model.
- Process inspection: on a test host, inspect the environment of each tool process the agent launches and confirm it holds only the declared variables.
Detection and measurement
The OWASP Secrets Management Cheat Sheet lists what to audit around secret use: who requested a secret, whether the request was approved, when and by what it was used, expiry, attempts to reuse expired secrets and authentication errors. It also notes that use of a dynamic secret from outside the consumer's expected location is a strong detection signal. For agents, alert on broker requests that do not match a running task, credential use from an address that is not the tool or proxy, canary hits anywhere, and secret-scanner findings in transcripts or traces.
| Metric | Target |
|---|---|
| Agents with any credential in prompts, tool schemas or agent configuration | Zero |
| Tool calls authenticated through the tool layer or proxy rather than agent-held credentials | 100% in production |
| Agent-launched processes with an explicit, reviewed environment | 100% |
| Canary credential hits in transcripts, logs, traces or memory per test cycle | Zero |
| Median lifetime of credentials issued to agent tools | Minutes to hours, falling |
Common failure patterns
- "The model needs the key to call the API": it does not. The model needs a tool; the tool needs the key. A key in the prompt or schema should be treated as exposed and rotated.
- Inherited environments: local MCP servers and tools start with the full parent environment, so a single malicious or careless tool reaches every cloud session on the machine, as the Amazon Q MCP Config Vulnerability 2026 illustrates.
- Proxies that inject by header name: the proxy adds the credential to any request carrying a placeholder, whatever the destination, which lets injected text send it to an attacker's host.
- Raw upstream errors: the tool passes an API's error body, including a connection string or echoed header, straight back to the model.
- Full-payload tracing in production: observability captures every prompt and tool response, and the trace store has wider access than the vault.
- Static keys kept "for fallback": the old long-lived credential stays valid after the broker path goes live and is eventually found by an agent.
Framework mapping
| Framework | Reference | Relevance |
|---|---|---|
| OWASP Top 10 for LLM Applications 2025 | LLM07:2025 System Prompt Leakage; LLM02:2025 Sensitive Information Disclosure; LLM06:2025 Excessive Agency | Credentials out of prompts; disclosure through output; tools acting with minimum privilege |
| MITRE ATLAS | AML.T0083 Credentials from AI Agent Configuration; AML.T0055 Unsecured Credentials | Agent configuration and environment as credential sources |
| MITRE ATT&CK | T1552.001 Credentials In Files; T1528 Steal Application Access Token | Credentials found in files and tokens taken from applications |
| NIST SP 800-53 Rev. 5 | IA-5 Authenticator Management; IA-5(7) No Embedded Unencrypted Static Authenticators; SC-28 Protection of Information at Rest; AU-2 Event Logging | Managed, non-embedded authenticators; protected stores; auditable secret use |
| NIST CSF 2.0 | PR.AA-01; PR.DS-01 | Credentials for services managed by the organisation; data at rest protected |
Practitioner checklist
- No credential appears in any system prompt, instruction file, tool schema or tool argument; any found is rotated.
- Tools and MCP servers authenticate with workload identity and fetch scoped, short-lived credentials at call time.
- Sandboxed agents reach third-party APIs through a credential proxy that injects by verified destination and holds the only copy of the key.
- Every agent-launched process starts with an explicit, minimal environment; developer credentials are short-lived sessions.
- Agent file tools are denied credential files and directories such as
.env,.key,.pemand CLI credential folders. - Tool results are shaped and scanned; raw upstream errors never reach the model.
- Logs, traces and memory stores redact or refuse credentials, and payload tracing is off by default in production.
- Canary credentials and adversarial prompts run in CI and any hit fails the build.
- Static fallback credentials are disabled and deleted once broker telemetry shows no use.
Standards and references
- OWASP: Top 10 for LLM Applications 2025 (LLM07 System Prompt Leakage)
- OWASP: Secrets Management Cheat Sheet
- OWASP: AI Agent Security Cheat Sheet
- Model Context Protocol: Security Best Practices
- MITRE ATLAS: Credentials from AI Agent Configuration
- MITRE ATT&CK: Unsecured Credentials: Credentials In Files
- NIST: SP 800-53 Rev. 5
- GitHub Docs: Using secrets in GitHub Actions
Related NHI Mgmt Group resources: Credential Vaulting for AI Agents Guide · Secrets Management Guide · AI Agent Credential Lifecycle and Rotation Guide · Tool-Call Security for AI Agents · MCP Security Guide