Think Build Implement Repeat
London, UK +44 7367 067226
WhatsApp FOLLOW f in X
  1. Home
  2. Blog
  3. Our AI Agent Has Our API Keys and Database Passwords in Its Config. How Should We Be Managing Its Secrets?
Problems We Solve

Our AI Agent Has Our API Keys and Database Passwords in Its Config. How Should We Be Managing Its Secrets?

AI agent secrets management done badly means broad, long-lived keys the agent can leak. How SpiderHunts scopes, stores and rotates agent credentials safely.

Updated 3 min readBy SpiderHunts Technologies

Free estimateNo obligation

Get a free estimate

Tell us what you need. A senior engineer reads every enquiry.

Takes under a minute. We never share your details.

  • Free consultation
  • No commitment
  • NDA on request

Prefer to talk? Book a free 30-minute call →

Quick answer — TL;DR

AI agents often run with broad, long-lived API keys pasted into config or environment files, and a prompt injection or a leaked log can expose them. SpiderHunts manages agent secrets by giving each tool its own narrowly scoped credential, keeping secrets out of the model's context entirely, storing them in a secrets manager, using short-lived tokens where possible, and logging every action the agent takes with them.

The agent that can do anything

The prototype was exciting. The agent reads a customer email, looks up the order in your database, checks the payment in Stripe, updates the CRM and drafts a reply. To get it working, a developer gave it an admin API key for each service and the production database password, all in a .env file. Some of those keys may even be in the system prompt, so the agent knows how to call things.

Now it is heading towards production, and somebody asks the obvious question: if the agent can do all that, what stops it from doing something else?

Why agents end up over-privileged

  • It is quickest to reuse existing admin keys during a prototype, and they never get replaced.
  • Agent frameworks make it easy to pass credentials around as plain strings.
  • Tools are defined broadly (run any SQL query, call any endpoint) instead of narrowly (look up an order by number).
  • Secrets end up in prompts, logs or traces where the model or a support engineer can see them.
  • Nobody treats the agent as a separate identity with its own permissions.

The difference from ordinary software is that an agent's behaviour is driven partly by text it reads. A cleverly written customer email or a web page can try to steer it. If the agent holds broad credentials, a successful injection can use them.

What is at risk

Weak pointWhat can happen
Admin keys given to the agentA manipulated agent can change or delete far more than its job needs
Secrets in promptsThe model can be tricked into repeating them in a reply
Secrets in logs and tracesAnyone with log access, or a logging vendor, can read them
Long-lived keysA leaked key keeps working until someone notices
Shared credentialsYou cannot tell the agent's actions from a person's

How we manage agent secrets

  1. Give the agent its own identity. It gets separate service accounts or API keys in each system, never a person's login or a shared admin key.
  2. Narrow the tools. Each tool does one specific thing (fetch order by number, add CRM note), and its credential only allows that. Database access is read-only or limited to specific tables or stored procedures where possible.
  3. Keep secrets out of the model. Credentials are held by the tool code, not placed in prompts, so the model never sees them and cannot repeat them.
  4. Store them in a secrets manager such as AWS Secrets Manager, Azure Key Vault or HashiCorp Vault, loaded at runtime rather than committed in files.
  5. Use short-lived credentials where the platform supports them, such as cloud role-based access or OAuth tokens that expire, and rotate the rest on a schedule.
  6. Redact secrets from logs and traces automatically, including in LLM observability tools.
  7. Require a person's approval for high-impact actions like refunds, deletions or outbound messages above a threshold, and log every tool call with its inputs and outcome.

If the agent uses MCP servers or third-party tools, we apply the same rules to them: what they can access, where their credentials live and what they log.

After the rework

The agent can do its job and not much more. If someone manages to manipulate it, the damage is limited to what its narrow tools allow, and the risky actions still need a person. Secrets live in a vault rather than in files, prompts or logs. And every action the agent takes is recorded under its own identity, so you can see exactly what it did.

Is your agent set up like this?

  • Your agent uses admin or personal API keys.
  • Credentials are stored in .env files, config or code.
  • Keys or passwords appear anywhere in prompts, logs or traces.
  • The agent can run arbitrary queries or call any endpoint.
  • You could not easily list everything the agent is able to change.

FAQ

Frequently asked questions

The questions readers ask us after this guide.

Still have a question?

Ask us directly — a senior engineer will get back to you.

Ask about your project

Is a .env file not good enough?

For local development it is common. In production, a secrets manager gives you access control, rotation and an audit trail that a file on a server does not.

Can prompt injection really expose our keys?

If keys are in the prompt or the agent can call tools that reveal them, yes. Keeping secrets out of the model's context removes that route.

Will narrower permissions make the agent less useful?

It makes each tool more specific, which usually makes the agent more reliable as well as safer. Broad tools give the model more ways to get things wrong.

Does this apply to agents built on no-code tools like Zapier or n8n?

Yes. The same principles apply: separate accounts, narrow permissions, stored credentials rather than pasted keys, and logging.

Keep reading

More on Problems We Solve

Start here

Worried about what your agent can reach?

Tell us what your agent does, which systems it connects to and how its credentials are stored now. We will suggest a safer setup that fits, and if the risk is small, we will tell you that too.

  1. You tell us what you needTwo minutes on the form, or a message on WhatsApp.
  2. A senior engineer reviews itAnd comes back with questions, a realistic range and an honest view on fit.
  3. Free 30-minute scoping callWe talk through scope, options and a realistic estimate — with no obligation.
Free estimateNo obligation

Talk to someone who builds this

Send a short brief and we will come back with an honest view and a realistic range.

Takes under a minute. We never share your details.

  • Free consultation
  • No commitment
  • NDA on request

Prefer to talk? Book a free 30-minute call →