keepcalculator

06 Numbers and Powers

Secure API Keys and Credentials for AI Agents

Card No. 06·56924 · Updated · Standard formula, shown below

Securing API keys and credentials used by AI agents reduces the risk of data exposure, unexpected usage, and unauthorized actions. This guide is for developers and operators who run agents locally, in a hosted service, or inside a company; you’ll need access to the agent’s configuration and the systems that issue its credentials.

What You’ll Need

  • Access to the agent’s source code, deployment settings, and runtime environment.
  • Administrative access, or a request path, for the API providers and services the agent uses.
  • A secret manager or secure deployment environment for storing credentials. For local development, use an untracked environment file with restricted permissions.
  • A way to review logs, restrict network access, and rotate credentials if one is exposed.

Step-by-Step

  1. List every credential and what it can access. Inventory provider API keys, database passwords, cloud credentials, webhook secrets, and tokens used by plugins or tools. Record the owner, purpose, environment, expiration or rotation process, and services authorized. Do not paste secret values into the inventory. This map helps you find credentials hidden in tool integrations and deployment jobs, not just the main agent configuration.
  2. Create separate credentials for each environment and purpose. Use distinct keys for development, testing, and production, and avoid sharing one key among unrelated agents or services. Give each credential only the permissions it needs—for example, read-only access when the agent only retrieves data. Prefer short-lived credentials or workload identity where the provider supports them; otherwise set an expiration or rotation schedule. A separate key limits the impact of a leak and makes it easier to identify which component used it.
  3. Put secrets in a managed store or protected runtime configuration. Use your organization’s secret manager, cloud platform secret store, or a deployment system designed to inject secrets at runtime. Grant access to the specific workload identity or service account that needs each secret. For local work, keep secrets in an environment file excluded from version control, restrict file access to your user, and provide a safe example file containing placeholders only. Never put secret values in source code, container images, command-line arguments, or files served to browsers.
  4. Load credentials at runtime and keep them out of agent prompts. Have trusted application code retrieve the secret and attach it to the appropriate outbound request. Do not put credentials in system prompts, user-visible context, conversation history, tool descriptions, or model-generated messages. Treat retrieved documents, user input, and tool output as untrusted: an instruction inside them must not be able to reveal environment variables or request a secret. The model can choose an approved tool action, but the application—not the model—should control authentication and enforce permissions.
  5. Limit what tools can do. Expose narrow, purpose-built tools instead of handing an agent a general-purpose shell, unrestricted database connection, or broad cloud token. Validate arguments in trusted code, check that the requested operation is allowed, and require human confirmation for consequential actions such as sending external messages, deleting records, or making purchases. Apply limits to requests and data access. A protected key can still be misused if the agent has excessive authority.
  6. Keep secrets out of source control and build artifacts. Add local secret files to the repository’s ignore rules, but do not treat that as protection for a file already committed. Review changes before merging, and use automated secret scanning on repositories and, where available, build logs and artifacts. Avoid printing configuration objects, request headers, environment variables, or full error payloads. Redact credentials from logs and traces before they leave the runtime.
  7. Restrict where credentials can be used. When a provider supports it, limit keys by API, resource, network origin, or permitted IP range. Use TLS for connections and validate the destination host so an agent cannot send an authenticated request to an untrusted endpoint. Keep outbound network access limited to required services where practical. Do not rely on an IP restriction alone if workloads use changing addresses; verify the deployment’s actual network path first.
  8. Test the controls and prepare for rotation. In a non-production environment, confirm that the agent can perform its intended task and that an unauthorized operation is rejected. Check that logs and prompts do not reveal credentials. Document how to revoke a key, issue a replacement, update the runtime, and verify recovery. Rotate immediately if a secret appears in a public repository, prompt, log, or other place outside its approved store; remove the exposed copy as well, but assume it may have been copied.

Common Mistakes

  • Putting keys in prompts or agent memory: Prompts can be logged, retained, or influenced by untrusted content. Keep credentials in application-controlled secret handling.
  • Using one all-powerful key: Broad permissions increase the consequences of misuse. Use separate, narrowly scoped credentials instead.
  • Assuming an ignored file is safe after a commit: Ignore rules prevent many future additions but do not erase repository history. Revoke and replace any committed secret.
  • Logging requests for debugging: Headers and full request objects may contain bearer tokens. Redact sensitive fields before logging and restrict log access and retention.
  • Rotating a key without checking all consumers: This can break production while leaving the old key active elsewhere. Identify dependencies, update them deliberately, then revoke the old credential.
  • Trusting model instructions to enforce access: A prompt is not an authorization boundary. Enforce permissions in code and at the service that owns the data.

Tips

  • Use secret names that identify their purpose without including the secret value, such as a service and environment label.
  • Keep access to secret stores limited, and review who or what can read each secret. Separate secret-management administration from routine agent operation where feasible.
  • Use structured logging with explicit redaction for authorization headers, cookies, tokens, and password fields.
  • Set usage alerts or quotas where providers offer them. Alerts help detect unusual activity but do not replace permission limits or revocation.
  • Keep a written incident checklist with the relevant provider’s revocation instructions and an internal contact path. Do not store replacement secrets in the checklist.

Troubleshooting

The agent reports an authentication error. Check that the secret is present in the runtime, the variable or secret name matches the configuration, and the deployment has been restarted or refreshed after an update. Confirm that the key belongs to the expected environment and has not expired or been revoked. Avoid printing its value while diagnosing the issue.

The agent works locally but fails after deployment. The deployed workload may not have permission to read the secret, or the secret may not have been injected into that environment. Verify its identity and access policy, secret name, and deployment configuration. Check whether network restrictions or an incorrect service endpoint are blocking the request.

A key was exposed or usage looks suspicious. Revoke or disable the credential with its issuer, then create a replacement with narrower permissions. Update each legitimate consumer, check provider activity and relevant logs for misuse, and remove exposed copies from repositories, build artifacts, prompts, and logs where possible. If a committed key is involved, treat it as compromised even after deleting the file; repository history and forks may retain it. Follow your organization’s incident process.

Rotation breaks an integration. Confirm which deployment or tool still uses the old credential, update its secret reference, and test the intended operation with the replacement. Keep the old key disabled if exposure prompted rotation; do not restore a compromised key just to recover service.

Frequently Asked Questions

Can an AI agent read an API key from an environment variable?
Yes, if trusted runtime code reads it and uses it for a specific request. Do not expose the value to the model, prompts, tool output, or logs.

Is an environment variable always secure?
No. It can be exposed through diagnostics, process inspection, misconfigured logs, or deployment access. Use a managed secret store where available and tightly control who can inspect the runtime.

Should I put API keys in a .env file?
It can be practical for local development if the file is excluded from version control and access is restricted. Do not use a committed .env file or copy production secrets into a developer machine unnecessarily.

How often should credentials be rotated?
Follow the provider’s and organization’s requirements, use short-lived credentials where practical, and rotate immediately after suspected exposure or unnecessary access. A fixed schedule is not a substitute for revocation after a leak.

What should I do if a key was committed to a public repository?
Revoke it first and issue a replacement. Then remove the exposed value where possible, check for unauthorized activity, and review repository history and copies; deleting the latest file does not make the old key safe.

Can prompt instructions keep an agent from revealing credentials?
They may help guide behavior but are not a security control. Keep secrets outside model-visible context and enforce access limits in trusted code and service permissions.

Related

Method: the standard formula for this calculation, rounded as each result states. See how the calculators work or report an error.

More cards in this drawer

The whole Numbers and Powers drawer →