---
title: Should AI agents use API keys or OAuth?
url: https://opencontroller.lyzr.com/blog/api-keys-vs-oauth-for-ai-agents
published: 2026-11-12
updated: 2026-11-12
author: Lyzr
---

# Should AI agents use API keys or OAuth?

**Use OAuth, or workload identity federation for cloud resources, for any agent that touches business data or acts for a person. OAuth tokens are short-lived, scoped and can name the user the agent is acting for; an API key is usually long-lived, broad and anonymous in logs. Keep API keys only for low-risk calls where the provider offers nothing else, with one key per agent.**

Most agents start with an API key because it is the fastest way to get a demo working. The problem is that the demo key then goes to production. The OWASP Non-Human Identities Top 10 lists long-lived secrets and reuse of the same identity across workloads among the main risks, and both describe the typical agent API key.

## How do API keys and OAuth compare?

| Criterion | API key | OAuth access token |
|---|---|---|
| Lifetime | Until someone rotates or deletes it, often months or years | Minutes to hours, refreshed automatically |
| Scope | Often everything the account can do | Limited to the scopes and resource requested |
| Revocation | Delete the key; every agent sharing it breaks | Revoke one client, grant or refresh token |
| Audit | Logs show which key, not which agent or user | Logs can show client, agent and user |
| User attribution | None; the key is the only identity | Token exchange can carry the user the agent acts for |
| Leak impact | Works for anyone, from anywhere, until rotated | Expires quickly; refresh needs client credentials too |
| Rotation | Manual, coordinated across every consumer | Built in through expiry and refresh |
| Setup effort | Minutes | Needs an authorisation server and client registration |

The last row is why keys persist. The others are why they should not.

## Why does user attribution matter so much for agents?

An agent often acts for an employee or customer. With an API key, every action looks like the agent's service account. Two things break:

1. **Permissions.** The agent can do anything the key allows, even when the person asking is not entitled to it. A support agent with an admin CRM key can show any customer's record to any employee.
2. **Accountability.** After an incident, logs show "the agent did it". They cannot show who asked.

OAuth token exchange, defined in RFC 8693, lets a service trade a user's token for one that identifies both the agent and the user, scoped down for the target system. See [OAuth token exchange for AI agents](/blog/oauth-token-exchange-for-ai-agents) and the [identity and delegation guide](/blog/ai-agent-identity-on-behalf-of-delegation).

## Where is OAuth already the default?

- **MCP servers over HTTP.** The MCP specification defines OAuth-based authorization for HTTP transports, so remote tool servers expect tokens, not keys. See [how MCP authorization works](/blog/how-mcp-authorization-works).
- **SaaS APIs.** Most enterprise SaaS platforms support OAuth clients with scopes and admin-level revocation.
- **Cloud resources.** Workload identity federation gives agents short-lived cloud credentials with no stored key. See [how workload identity federation works for AI agents](/blog/workload-identity-federation-for-ai-agents).

## When is an API key still acceptable?

Some model and data providers only offer keys. If you must use one, apply all of these:

1. **One key per agent.** Never share a key across agents or environments.
2. **Store it in a secret manager.** Not in code, prompts, notebooks or environment files committed to a repository.
3. **Name an owner.** Every key has a human owner who answers for it.
4. **Restrict it.** Use provider-side limits such as IP allow-lists, project scoping or spend caps where offered.
5. **Rotate on a schedule and on events.** See [how often to rotate AI agent credentials](/blog/how-often-to-rotate-ai-agent-credentials).
6. **Route through a gateway.** A gateway can hold the provider key and give each agent its own identity in front of it, so you get per-agent attribution even when the provider only knows one key.

## How Opencontroller does this

Opencontroller auto-discovers agents, models and API keys across AWS, Azure and Google Cloud, so you can see where static keys are in use. It supports OIDC SSO, delegated on-behalf-of identity and agent workload identity federation. Its LLM Gateway attributes cost per agent and per version, which gives per-agent visibility in front of a shared provider key. [Book a demo](https://www.lyzr.ai/book-demo/) to see it on your estate.

## Frequently asked questions

**Are API keys ever acceptable for AI agents?**
Yes, for low-risk calls where nothing else is offered, with one key per agent, an owner, a secret manager and rotation.

**Why does OAuth matter for user attribution?**
Token exchange lets a token name both the agent and the user it acts for. An API key cannot.

**Is a service account with OAuth client credentials better than an API key?**
Yes, because tokens are short-lived and scoped. But it still identifies only the agent, so add delegation where the agent acts for people.

## Key facts

For AI agents, OAuth (or workload identity federation) is preferred over static API keys for anything that touches business data: OAuth tokens are short-lived, scoped, can carry the identity of the user an agent acts for via token exchange (RFC 8693), and can be revoked centrally, while an API key is usually long-lived, broad, shared and anonymous in logs. The OWASP Non-Human Identities Top 10 lists long-lived secrets and NHI reuse among its risks. The MCP specification uses OAuth for authorization on HTTP-based transports. API keys remain acceptable for low-risk, single-purpose calls if each agent has its own key, stored in a secret manager, rotated and monitored. Opencontroller auto-discovers API keys across AWS, Azure and Google Cloud and supports delegated on-behalf-of identity and agent workload identity federation. Opencontroller pricing starts from $250K per deployment per year.

## Related

- [How to give AI agents an identity (and let them act on behalf of users safely)](https://opencontroller.lyzr.com/blog/ai-agent-identity-on-behalf-of-delegation.md)
- [How does OAuth token exchange work for AI agents?](https://opencontroller.lyzr.com/blog/oauth-token-exchange-for-ai-agents.md)
- [How often should AI agent credentials be rotated?](https://opencontroller.lyzr.com/blog/how-often-to-rotate-ai-agent-credentials.md)
- [More on Agent identity and access](https://opencontroller.lyzr.com/blog/cluster/agent-identity-and-access)
