You gave your AI agent access to your infrastructure. You had to — it needs to call APIs, connect to databases, push to S3, trigger deployments. Credentials are the price of capability.
What you probably didn't anticipate is how many places those credentials would show up once an agent started using them. Shell command history. Log files. LLM context. Error messages. The agent's own reasoning trace. Each is a potential leak surface, and most of them get very little attention in standard secrets management guides.
Where AI Agents Leak Secrets
The classic leak surfaces are well understood: hardcoded credentials, committed .env files,
environment variables in error traces. What's less understood is the agent-specific surface area.
1. Command Arguments
When an agent runs a shell command, credentials often end up as arguments:
curl -H "Authorization: Bearer sk_live_abc123..." https://api.stripe.com/v1/charges
mysql -u root -pMySecret123 production_db -e "SELECT ..."
aws s3 cp s3://bucket/file . --aws-access-key-id AKIAIOSFODNN7EXAMPLE
These commands land in shell history, process lists (ps aux is readable by anyone on the box),
and — critically — in your command audit log. If you're using a tool like expacti that intercepts
commands before execution, those intercepted commands get reviewed by a human who can now read
the credential inline.
The fix isn't to stop reviewing commands. It's to ensure credentials never appear as arguments in the first place. Use environment variables, credential files, or temporary tokens with narrow scope.
2. LLM Context Windows
This one is underappreciated. When an agent reads a file to understand what it's working with, that file's content — including any secrets it contains — gets loaded into the LLM's context window. If the model is hosted externally, that's an implicit data transfer to a third party.
A .env file read "just to check the database URL" might contain a dozen other credentials.
An agent that reads config files broadly will routinely ingest secrets it doesn't need.
Mitigation: separate config from secrets at the file level. Config files have URLs, feature flags, limits. Secrets files have credentials, and agents don't read them directly — they're injected at runtime via environment or a secrets manager.
3. Error Messages and Stack Traces
Authentication failures often include the credential that failed. A Postgres connection error might say: "password authentication failed for user 'app_user' with password 'wrongpassword'". An AWS SDK error might include the key ID. A curl failure might print the full request headers.
Agents that report errors to reviewers, logs, or upstream systems can inadvertently surface credentials through error propagation. Scrubbing error messages before they leave the agent is harder than it sounds — you'd need to know all possible credential formats ahead of time.
4. The Approval Queue
If you're running human-in-the-loop approval (which you should be for sensitive operations), your reviewers will see the commands before they execute. That's the point. But it means reviewers are now reading credentials in plain text, potentially in a web UI that logs page views, on screens visible to colleagues.
The solution here is redaction at the queue level — credentials in commands should be masked before they reach reviewer eyes, while still being present in the actual execution context. This requires the approval system to understand credential patterns, not just pass commands through verbatim.
A Tiered Secrets Model for Agents
Think of secrets in terms of agent exposure risk, not just sensitivity classification:
| Type | Exposure Risk | Recommended Approach |
|---|---|---|
| Long-lived API keys | Critical | Never inject directly. Rotate frequently. Prefer short-lived tokens. |
| Database passwords | Critical | Use IAM auth or short-lived credentials via Vault/similar. Never as CLI args. |
| Service account tokens | High | Scope narrowly. Expire aggressively. Audit usage per-agent, not per-team. |
| Temporary session tokens | Medium | Acceptable if TTL < 1h and tied to specific agent session. |
| Read-only API tokens | Low | Still rotate. Still scope. Treat as medium if they access PII data. |
Practical Controls
Use a Secrets Manager, Not Environment Variables
Environment variables are better than hardcoded strings but worse than a proper secrets manager. They're visible in process listings, inherited by subprocesses, and often logged by mistake.
The pattern worth adopting: the agent authenticates to a secrets manager at startup using a short-lived token (OIDC, workload identity, instance profile), retrieves only the secrets it needs for the current task, and discards them after use. No long-lived credentials sitting in memory waiting to be exfiltrated.
Short-Lived Credentials Are the Default
AWS STS, Google Cloud Workload Identity, HashiCorp Vault dynamic secrets — all of these issue credentials that expire. An agent with an AWS access key that expires in 15 minutes is dramatically less dangerous than one holding a permanent key, even if both have the same permissions.
The rotation cadence should be shorter than the blast radius requires. If a leaked credential could delete production data, its lifetime should be measured in minutes, not days.
Scrub Commands Before Logging
Build credential pattern matching into your command interceptor. Common patterns to redact:
sk_live_[A-Za-z0-9]+(Stripe)AKIA[A-Z0-9]{16}(AWS access key IDs)ghp_[A-Za-z0-9]{36}(GitHub PATs)-p[A-Za-z0-9!@#$%^&*]{8,}(MySQL password flag)- Any arg following
--password,--token,--secret,--api-key
Redacted versions go to the audit log and reviewer queue. The actual command — with real credentials — executes only if approved, and the execution context has them injected separately rather than via the command string.
Scope Credentials to Agent Sessions
When possible, issue credentials that are scoped to a specific agent session, not the agent type. If your deployment agent is running job #482, its credentials should only be valid for the resources job #482 is supposed to touch. Cross-job credential reuse is a blast radius multiplier.
This is easier with some systems than others. Kubernetes service accounts with RBAC can scope to specific namespaces and resources. AWS IAM with session tags can scope to specific resources. Custom application tokens can include session IDs in their claims and be validated server-side.
Audit Credential Usage, Not Just Command Execution
Your command audit log tells you what the agent did. Your credentials audit tells you what
it accessed. You want both. An agent that runs a benign-looking command (aws s3 sync ./reports s3://bucket/reports/)
but uses elevated credentials to do it is worth investigating even if the command looks fine.
CloudTrail, GCP Audit Logs, GitHub audit log — most providers emit credential usage events separately from resource access events. Correlating them gives you a fuller picture than either source alone.
What to Do When a Secret Leaks
Assume it will happen. The question is response time.
- Revoke immediately. Don't investigate first. Pull the credential, then understand what happened.
- Scope the blast radius. What did that credential have access to? What could have been accessed in the window between creation and revocation?
- Check your logs. Did anything use the credential between when it leaked and when you revoked it? Cloud provider audit logs are your first stop.
- Issue a replacement with reduced scope. If the old credential had broad access, this is your opportunity to scope the replacement correctly.
- Update your redaction rules. If this credential type wasn't being scrubbed, add it now.
If you can't complete step 1 (revoke immediately) within 5 minutes of detection, your incident response playbook has a gap. Practice it before you need it.
The Bigger Picture
Secrets management for AI agents isn't fundamentally different from secrets management for any automated system. The principles are the same: least privilege, short-lived credentials, audit trails, fast revocation.
What is different is the surface area. AI agents read broadly, reason across contexts, and can end up ingesting credentials in ways that weren't anticipated by whoever provisioned them. The failure modes are harder to predict because the agent's behavior is harder to predict.
That's exactly why command-level interception matters. When every shell command the agent runs passes through an approval layer, credentials that appear as arguments get caught before they execute — and before they propagate downstream. It's not a substitute for proper secrets management, but it's a reliable last line of defense for the cases where proper management breaks down.
Command-level secrets detection in your approval flow
Expacti intercepts shell commands before execution. Reviewers see redacted versions; audit logs get scrubbed output. Your credentials stay in the execution context, not in the review queue.
Get Started Read the Docs