Running AI agents that call external APIs can expose your keys and secrets if they are not properly guarded. This guide shows you how to keep those credentials safe—using environment variables, secret managers, and strict permissions—so your agents can work without leaking sensitive data.
What You’ll Need
- Access to the terminal or command line on the machine running the agent.
- Basic familiarity with environment variables and your operating system’s shell (bash, zsh, PowerShell).
- If using a cloud provider, access to its secret management service (e.g., AWS Secrets Manager, Azure Key Vault, Google Secret Manager).
- If using a framework like LangChain or AutoGPT, know how to configure its settings file.
Step-by-Step
- Never hardcode keys in source code. Search your repository for any API keys, tokens, or passwords that may have been committed. Use tools like
gitleaksortrufflehogto scan history. If found, rotate those keys immediately and remove them from git history (using tools likegit filter-repo). - Use environment variables for local development. Instead of writing
api_key = "sk-..."in your script, read it from the environment:api_key = os.getenv("OPENAI_API_KEY"). Set the variable in your shell session:export OPENAI_API_KEY="sk-..."(Linux/macOS) or$env:OPENAI_API_KEY="sk-..."(PowerShell). - Create a .env file for local projects. Store secrets in a
.envfile (e.g.,OPENAI_API_KEY=sk-...) and load it with a library likepython-dotenv. Add.envto your.gitignoreso it never gets committed. - Use a secret manager for production. Move secrets to a dedicated service like AWS Secrets Manager, Azure Key Vault, or HashiCorp Vault. Your agent retrieves them at runtime via the provider’s SDK, so the secret never appears in code or configuration files.
- Restrict permissions on the agent’s credentials. Create a dedicated service account or API key with only the minimum permissions needed. For example, if the agent only reads from a database, grant read-only access. Avoid using admin keys.
- Set short-lived tokens where possible. Use temporary credentials (e.g., AWS STS, OAuth2 short-lived tokens) that expire automatically. This limits the damage if a token leaks.
- Encrypt secrets at rest and in transit. Ensure your agent uses HTTPS for all API calls. If storing secrets locally, encrypt them with a strong passphrase or use your OS’s keychain.
- Add logging redaction. Configure your logging library to mask or redact any string that looks like a secret. For example, in Python use a custom filter that replaces
sk-...patterns with[REDACTED]. - Monitor for leaks. Set up alerts for unusual API usage (e.g., sudden spikes in requests) and periodically scan your codebase and logs for exposed keys.
- Rotate keys regularly. Schedule periodic rotation of API keys and secrets. Automate this where possible, and update your agent’s configuration accordingly.
Common Mistakes
- Committing .env files or config files with real secrets to version control.
- Using the same API key for development and production, increasing blast radius.
- Logging full request/response payloads that include the API key in headers.
- Giving the agent more permissions than necessary (e.g., write access when read-only is enough).
- Storing secrets in plain text files on the server.
Tips
- Use a dedicated secret management library that integrates with your framework, like
python-dotenvordotenvfor Node.js. - For containerized agents, use Docker secrets or Kubernetes secrets instead of environment variables.
- If your agent runs on a schedule, consider using a CI/CD pipeline’s secret store (e.g., GitHub Actions secrets) to inject secrets at runtime.
- Test your redaction logic by intentionally logging a fake key and verifying it is masked.
- Document which secrets are needed and where they are stored, so future maintainers don’t take shortcuts.
Troubleshooting
Agent can’t find the key. Check that the environment variable is set in the same shell that runs the agent. If using a .env file, ensure the path is correct and the file is readable.
Secret manager access denied. Verify the agent’s IAM role or service account has permission to read the secret. Check that the secret name and region are correct.
Key appears in logs despite redaction. Review your logging configuration; you may be logging the request object before redaction is applied. Move redaction to the earliest point.
Agent works locally but not in production. Compare the environment variable names and values. Production may use a different secret manager or have different permissions.
Accidentally committed a key. Rotate the key immediately, then remove it from history. Even if you delete the commit, the key is compromised.
Frequently Asked Questions
Q: Can I use a .env file in production?
A: It’s not recommended. Use a secret manager for production, as .env files can be accidentally exposed and lack rotation.
Q: How often should I rotate API keys?
A: At least every 90 days, or immediately if you suspect a leak. Some providers offer automatic rotation.
Q: What’s the difference between an API key and a secret?
A: An API key is often used for authentication, while a secret could be a password, token, or encryption key. Both need protection.
Q: Should I encrypt environment variables?
A: Environment variables are plain text by default. For sensitive data, use a secret manager or OS keychain instead.
Q: Can I use the same key for multiple agents?
A: It’s safer to use separate keys per agent so you can revoke one without affecting others.
Q: What if my agent runs on a server I don’t control?
A: Use a secret manager with fine-grained access, and ensure the server’s security group limits access to only necessary endpoints.
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
- Zombie Rush Best Classes & Costs
- Parse SMS Transactions Without Trusting Regex Alone
- Bosch SMS2ITW01A Features & Ratings Explained
- Cinema HD vs Titanium TV: Which Is Better?
- Blizzard Game Classics & Remasters Compared
- OnePlus 5T Thin Bezels Leak Again: What’s New