This guide shows you how to keep API keys, database credentials, and other secrets used by AI agents safe from accidental exposure and unauthorized access. You’ll learn a practical, layered approach—from environment variables and secret managers to permission scoping and rotation—so your agents can work without becoming a security risk.
What You’ll Need
- Access to the terminal or command line on the machine where your AI agent runs
- Basic familiarity with environment variables and your operating system’s shell (bash, zsh, PowerShell)
- For production setups: access to a cloud provider or a self-hosted secret management tool (e.g., HashiCorp Vault, AWS Secrets Manager, or Kubernetes Secrets)
- An understanding of the AI agent’s configuration files and how it loads settings (e.g., a .env file, config.yaml, or runtime parameters)
Step-by-Step
- Identify every secret your agent uses. Audit the agent’s code, configuration, and logs. Look for API keys, tokens, database passwords, private keys, and any other sensitive strings. Make a list of each secret, where it’s used, and what permissions it has.
- Move secrets out of source code. Remove any hardcoded secrets from your codebase. Replace them with references to environment variables or a secret manager. For example, change
api_key = "sk-1234"toapi_key = os.getenv("OPENAI_API_KEY"). - Use a .env file for local development. Create a
.envfile in your project root (outside of version control) and store secrets there in the formatKEY=value. Load it with a library likepython-dotenvor a similar tool for your language. This keeps secrets out of code while making local testing easy. - Add .env to .gitignore. Immediately add
.envand any other files containing secrets to your.gitignorefile to prevent accidental commits. Also check that your version control history doesn’t already contain secrets; if it does, rotate those secrets and consider rewriting history (with caution). - Use a secret manager for production. For deployed agents, store secrets in a dedicated secret manager like AWS Secrets Manager, Azure Key Vault, Google Cloud Secret Manager, or HashiCorp Vault. Retrieve them at runtime via the manager’s API or SDK, rather than embedding them in the environment.
- Inject secrets via environment variables in deployment. When deploying, set environment variables from your secret manager (e.g., in Kubernetes using
secretKeyRef, or in Docker with--env-file). This way, secrets are never written to disk in plaintext on the server. - Scope permissions to the minimum needed. Create separate API keys or service accounts for each agent with only the permissions it requires. For example, if your agent only reads from a database, grant read-only access. If it uses an AI API, restrict it to that specific service and model.
- Rotate secrets regularly and after any incident. Set up a rotation schedule (e.g., every 90 days) and rotate immediately if you suspect a leak. Use your secret manager’s built-in rotation features where possible. Update your agent’s configuration to use the new secrets.
- Encrypt secrets at rest and in transit. Ensure that any storage (like .env files on disk) is encrypted, and that all communication between your agent and the secret manager uses TLS. Most secret managers handle this automatically.
- Log and monitor access. Enable audit logging on your secret manager and monitor for unusual access patterns. Also, be careful that your agent’s own logs don’t accidentally print secrets—redact them in logging functions.
- Use short-lived tokens where possible. For cloud services, prefer temporary credentials (e.g., AWS STS tokens) that expire automatically, rather than long-lived API keys. This reduces the impact of a leak.
- Document your secret management process. Write down where secrets are stored, how to rotate them, and who has access. This helps new team members and ensures consistent practice.
Common Mistakes
- Committing .env files to version control. This is the most common leak. Always double-check your .gitignore and use tools like
git-secretsto scan for accidental commits. - Using the same key for multiple services. If one service is compromised, all others are at risk. Use unique keys per service and per agent.
- Giving agents overly broad permissions. An agent that only needs to read a spreadsheet doesn’t need write access to your entire cloud account.
- Hardcoding secrets in Docker images. Building secrets into an image means they’re visible in the image layers. Use build args or runtime injection instead.
- Storing secrets in plaintext config files. If you must use a config file, encrypt it and decrypt at runtime.
- Ignoring secret rotation. Keys that never change are more likely to be compromised over time.
Tips
- Use a dedicated library or SDK for your secret manager—it handles caching, retries, and encryption for you.
- Set up alerts for any access to secrets outside of expected times or from unexpected IPs.
- For local development, consider using a tool like
direnvto automatically load environment variables when you enter a directory. - If your agent runs in a container, use secrets mounted as files (e.g., in Kubernetes) rather than environment variables for extra security, as files can be permission-restricted.
- Regularly review your secret manager’s access logs and audit who has permission to view secrets.
- Never share secrets in chat, email, or documentation—even internally. Use the secret manager to share access.
Troubleshooting
- Agent can’t find the secret. Check that the environment variable is set correctly in the shell or deployment. Test with
echo $YOUR_VAR(on Unix) orecho %YOUR_VAR%(on Windows). Ensure the agent process inherits the environment. - Secret manager access denied. Verify that the agent’s IAM role or service account has the correct permissions to read the secret. Check the secret’s resource-based policies as well.
- Secrets appear in logs. Add a logging filter that redacts known secret patterns. Also, avoid logging full request/response bodies that may contain sensitive data.
- .env file not loading. Ensure the file is in the correct directory and that your code loads it before accessing the variables. If using python-dotenv, call
load_dotenv()at the start of your script. - Secret rotation breaks the agent. Update the agent’s configuration to fetch the latest version of the secret. Some secret managers support versioning—make sure your code uses the current version.
- Agent works locally but not in production. Compare the environment variables and secret manager configurations between local and production. Often, a missing environment variable or a typo in the secret name is the cause.
Frequently Asked Questions
Can I just use environment variables without a secret manager?
Yes, for small projects or local development, environment variables are fine. But for production, they can be visible in process listings or container configs. A secret manager adds encryption, rotation, and audit logging.
Is it safe to store secrets in a .env file?
Only if the file is outside version control and has restricted file permissions (e.g., chmod 600 on Unix). It’s acceptable for local development, but not for production.
How often should I rotate API keys?
At minimum every 90 days, or more frequently if you have compliance requirements. Rotate immediately if you suspect a leak or after a team member leaves.
What if my agent is a third-party service I don’t control?
You can still use a proxy or gateway that injects secrets at runtime, or use environment variables if the service supports them. Avoid giving the third party more access than necessary.
Should I encrypt secrets in my database?
Yes, if you must store secrets in a database, encrypt them at rest using a strong encryption key managed separately. Better yet, use a dedicated secret manager that handles encryption for you.
Can I use the same API key for multiple agents?
Technically yes, but it’s risky. If one agent is compromised, all others are exposed. Use unique keys per agent and scope permissions accordingly.
How do I detect if a secret has leaked?
Monitor your secret manager’s audit logs for unexpected access. Also, use services like GitHub’s secret scanning to alert you if secrets appear in public repos.
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
- Kick an Uma Codes? Meaning & Use
- Google Forms as Expense Tracker: Step-by-Step
- Evaluate Claude for Enterprise Workloads: Checklist
- Smart Lock Modules Worth Buying Early
- Wi-Fi Name Exposes More Info Than You Think?
- GBP to INR Business Payments: Bank vs Fintech vs FX Broker1,049,725.00 INR