Join our Newsletter — 33% off our NHI Course

What is the difference between keeping credentials in the agent’s context and resolving them only on request?

Keeping credentials in context means the model can see sensitive values during execution, which expands exposure if the conversation, logs, or tooling are later accessed. Resolving them only on request keeps the agent working with placeholders until an approved host is reached. That pattern preserves utility while preventing the raw secret from becoming part of the agent’s working memory.

Why these two patterns behave differently

Keeping credentials in the agent’s working context means the secret is available to the model while it reasons, plans, and calls tools. That increases the blast radius of a prompt leak, logging path, memory dump, or downstream tool compromise. Resolving credentials only when needed keeps the agent on placeholders until an approved host or policy gate is reached, which narrows how long the raw secret exists and where it can surface.

The difference is not just where the secret lives, but whether the agent ever needs to “know” it. If a task can be completed by fetching a credential at the moment of use, the safer pattern is to keep the secret outside the prompt, outside the conversation transcript, and outside general-purpose context storage. That design preserves utility while reducing accidental persistence.

What changes in practice when the secret is deferred

Deferred resolution changes the control point. Instead of trusting the agent to carry sensitive values correctly through a sequence of steps, you trust a host-side broker, vault, or policy engine to release the credential only to the approved destination. This is especially important for secrets that grant broad access, because exposure in context can turn a single operational step into a reusable compromise.

It also changes observability. With request-time resolution, the log trail should show that access was requested and granted, but not reveal the underlying value. That makes review and incident handling more workable, because operators can inspect behaviour without exposing the credential itself.

For readers comparing implementation approaches, Secrets Management Guide is the best place to see how centralised secret handling, dynamic secrets, and secretless patterns fit together. If the issue is API keys specifically, API Key Management Guide covers scoping, rotation, revocation, and safe handling when keys leak.

Where the risk boundary really sits

The main boundary is between reasoning and release. Once a credential is embedded in the agent’s context, every intermediate step, retry, or chained tool action inherits the same exposure risk. Resolving on request keeps the exposure bounded to the exact approval moment, which is materially safer when the agent is handling high-value tokens, long-lived keys, or credentials with broad permissions.

That boundary also helps with lifecycle discipline. If a secret is treated as ordinary context, it tends to persist longer than intended and become harder to rotate cleanly. If it is fetched only when needed, the system can expire, swap, or revoke the credential without depending on the model to forget anything. Guide to NHI Rotation Challenges is useful here because rotation and short-lived access become much easier to manage when the secret is not embedded in the agent’s working memory.

Risk and Threat Considerations

Keeping a credential in context expands the number of places it can leak, including transcripts, traces, intermediate prompts, and any tooling that can later inspect prior context. Resolving only on request reduces that exposure window and lowers the chance that a single compromise becomes reusable access.

Failure mechanism: The agent retains the raw secret in memory or logs, and an attacker, operator, or adjacent tool later reads it from conversation history, telemetry, or cached state. A second failure mode is over-broad reuse, where one exposed secret can be replayed across multiple actions because it was never meant to be visible to the model at all.

Impact: Credential exposure can lead to unauthorized access, unintended tool execution, privilege misuse, and delayed revocation pressure. In practice, the harm is often larger than the original task because the secret may outlive the session that first needed it.

Standards & Framework Alignment

This section maps relevant standards and security frameworks to the operational risks and controls described in this guidance.

OWASP Non-Human Identity Top 10 and OWASP API Security Top 10 address the attack surface, NIST SP 800-53 Rev 5 sets the technical controls, and ISO/IEC 27001:2022 defines the regulatory obligations.

Framework Control / Reference Relevance
OWASP Non-Human Identity Top 10 NHI-02 — Secret Leakage Directly addresses secret exposure when credentials enter agent context.
NHI-07 — Long-Lived Secrets Supports the risk of secrets persisting longer than needed in working memory.
Recommendation — Keep secrets out of agent context and deliver them only through controlled, auditable request-time retrieval. Prefer short-lived credentials and rotate any secret that must be exposed to the agent.
NIST SP 800-53 Rev 5 IA-5 — Authenticator Management Covers credential lifecycle, rotation, storage, and revocation for secrets used by the agent.
AC-6 — Least Privilege Applies because request-time resolution should limit what the agent can obtain and use.
AU-2 — Event Logging Relevant because request-time secret access should be observable without exposing the secret itself.
Recommendation — Manage credential issuance, storage, rotation, and revocation outside the agent prompt and transcript. Limit the agent to the minimum access needed for the specific request and nothing broader. Log credential requests and approvals while preventing secret values from appearing in audit trails.
ISO/IEC 27001:2022 A.5.15 — Access control Supports restricting when and how an agent may obtain sensitive credentials.
Recommendation — Define controlled access paths so secrets are released only under approved conditions.
OWASP API Security Top 10 API2 — Broken Authentication Relevant when an exposed credential can be replayed to impersonate the agent or its caller.
API5 — Broken Function Level Authorization Applies when request-time access must be limited to approved actions and not broader functions.
Recommendation — Treat exposed credentials as authentication material that must be prevented from leaking into context. Enforce function-level checks so the agent only receives credentials for authorized operations.

Practitioner Guidance

What to verify: Check whether the agent actually needs the secret value or only needs the ability to request it through an approved host. If the latter is true, keep the model on placeholders and require a brokered lookup at the moment of use.

Common mistake: Teams often store secrets in prompts or hidden context because it seems simpler than building a request-time path. That shortcut usually trades convenience for wider exposure, weaker revocation, and harder incident response.

What good looks like: The agent can complete the task, but the raw credential never appears in general conversation state, and access is visible only as an approved retrieval event. The most important judgement is to keep secrets out of the model’s working memory unless there is a concrete, unavoidable need for the model itself to inspect them.