Updated October 7, 2026
By ClawBud
AI agents should use OAuth when they act in a user's account and the service supports narrow scopes, consent, expiration, and revocation. Use an API key for a server-to-server service that has no suitable OAuth flow, but store it outside prompts and memory, restrict it at the provider, rotate it, and give each agent or workload a separate credential.
Quick answer: Choose OAuth for Gmail, calendars, CRMs, and other user-owned services. Choose a workload identity or short-lived token for cloud infrastructure when available. Use a static API key only when the provider requires it and the key can be tightly scoped, monitored, rotated, and revoked.
Conditional recommendation: Choose OAuth for delegated user access, workload identity for machine-to-machine access, and a scoped API key only as the fallback for services without a safer short-lived credential option.
What is the difference between an API key and OAuth?
An API key is a secret string that identifies a calling application or account. Whoever holds it can usually use the permissions attached to it. OAuth is an authorization framework that lets an application receive limited access to a protected service, often on behalf of a user, without receiving the user's password. OAuth 2.0, RFC 6749
The difference matters more for agents because an agent can read untrusted messages, pages, documents, and tool output. A leaked static key may remain usable until someone revokes or rotates it. A properly configured OAuth token can be limited by scope, audience, user consent, and lifetime. OAuth still needs careful implementation. It is not a security sticker.
Which credential should you choose?
Define the job and trust boundary before choosing the credential.
| Choice | Best fit | Setup burden | Management | Privacy or security | Integrations | Main limitation |
|---|---|---|---|---|---|---|
| OAuth delegated access | Agent acts for a named user in Gmail, Calendar, Slack, or a CRM | Medium | Consent, refresh, revocation, and scope reviews | Can limit scopes and avoid sharing a user password | Strong when the service has a mature OAuth app | Refresh tokens remain sensitive and broad scopes can erase the benefit |
| Workload identity or short-lived token | Agent service calls cloud or internal infrastructure | Higher initial setup | Automated issuance and expiration | Reduces long-lived secrets and can bind access to a workload | Best in clouds and identity-aware internal systems | Not available for every SaaS tool |
| Scoped API key | Server-to-server API with no suitable OAuth option | Low | Storage, rotation, revocation, and usage monitoring | Safe only within the provider's scope and restriction controls | Common for model and data APIs | Often long-lived and sometimes tied to a broad account |
| Personal user credential | Temporary manual test only | Low | Poor attribution and difficult offboarding | Inherits a person's access and mixes human with agent activity | Works where no agent identity exists | Wrong default for persistent production agents |
This table separates verified protocol properties from editorial judgment. OAuth's delegated authorization model is defined in RFC 6749. Current OAuth security guidance recommends authorization code flows with stronger protections and discourages less secure legacy patterns. OAuth 2.0 Security Best Current Practice, RFC 9700
When is OAuth the better choice for an AI agent?
OAuth is usually better when the agent acts inside a user's account. The user can consent to named permissions, the application can request only the scopes it needs, and access can be revoked without changing the user's password.
Use it for a calendar agent that reads availability, a CRM agent that updates approved records, or an inbox agent that drafts messages. Do not request a broad write scope because it is convenient. Start with read access, test the workflow, then add the smallest required write scope.
For tokens intended for a particular API, resource indicators can help the authorization server issue a token for the intended protected resource. OAuth 2.0 Resource Indicators, RFC 8707
When is an API key acceptable?
An API key is reasonable when the provider offers no suitable OAuth or workload identity flow and the agent calls a bounded server API. Model providers commonly use keys for this kind of access.
Apply five controls:
- Create a separate key for one agent, environment, or workload.
- Limit the key by permissions, project, budget, IP address, referrer, or endpoint when the provider supports it.
- Store it in a secret manager or protected runtime configuration, never in prompts, memory, source code, logs, or shared workspace notes.
- Monitor use and set spend or rate alerts.
- Rotate it on a schedule and immediately after suspected exposure.
OWASP recommends centralizing secrets, limiting access, automating rotation where possible, and keeping audit information without logging secret values. OWASP Secrets Management Cheat Sheet
How should you implement agent credentials safely?
Start with one real workflow, not a generic access bundle.
- Name the action. Write down the exact records, messages, files, or endpoints the agent must read or change.
- Choose the identity. Prefer a dedicated agent identity, delegated OAuth grant, or workload identity over a person's main account.
- Reduce the scope. Remove permissions that the workflow does not exercise in testing.
- Separate environments. Development and production agents should not share credentials.
- Keep secrets outside context. The model should call a tool that uses the credential. It should not see the raw credential.
- Add consequence gates. Require approval before payments, public publishing, account changes, deletion, or broad outbound messaging.
- Prove revocation. Disable the identity or token and confirm that the agent loses access as expected.
OpenClaw's security guidance recommends treating the gateway host and configuration as part of the trust boundary, restricting tool access, and assuming external content may be hostile. OpenClaw security documentation
Where does ClawBud fit?
ClawBud is a fully managed Agentic OS for an AI agent army, including managed OpenClaw on a private cloud computer. It is a fit when a team wants the runtime, integrations, browser, and operating controls managed together. The current ClawBud plans list a dedicated server, integrations, skills, and a dedicated firewall. ClawBud pricing
Credential design still belongs to the workflow. A private runtime does not make an overpowered token safe. Teams should connect each agent with the smallest useful access, keep sensitive actions behind approval, and retain a tested revocation path.
For the surrounding policy, see What permissions should an AI agent have? and Should AI agents use separate accounts or shared accounts?.
ClawBud is not the right fit when a security team must own the host, identity broker, network, token issuer, and audit pipeline directly. Self-hosted OpenClaw can be the better choice for that operating model.
How can you verify the setup before production?
Run four tests with a non-production account:
- Ask the agent to access a record outside its approved scope. The request should fail.
- Revoke the token or key. The next call should fail without exposing the credential in an error or log.
- Put a hostile instruction in a test document or web page. The agent should not expand its permissions or reveal secrets.
- Repeat a write after a timeout. The integration should prevent duplicate or ambiguous side effects.
The last test matters because authentication proves who may call a service. It does not make the action correct or idempotent.
What are the limitations?
OAuth can still be deployed badly. Broad scopes, long-lived refresh tokens, weak redirect URI validation, and poor token storage can leave a large blast radius. API keys can be workable when a provider offers strong restrictions and rapid rotation. The label alone does not decide safety.
This guide is general technical guidance, not a security audit or legal advice. Provider capabilities differ. Verify current scopes, token lifetimes, audit logs, data-processing terms, and incident procedures with each service before connecting production data.
Frequently asked questions
Is OAuth always safer than an API key for an AI agent?
No. OAuth provides useful controls such as delegated access, scopes, expiration, and revocation, but poor implementation can still expose powerful refresh tokens. A tightly restricted API key may be safer than an OAuth grant with excessive scopes. Compare the actual permission, lifetime, storage, monitoring, and revocation controls.
Should every AI agent have a separate credential?
Persistent agents should usually have separate credentials or workload identities. Separation improves attribution, scope reviews, revocation, and incident response. A shared credential may be acceptable for a narrow low-risk service with no better identity model, but it should not span unrelated agents, environments, or customers.
Where should an AI agent's API key be stored?
Store it in a secret manager or protected runtime configuration that injects the credential only when the tool makes a request. Keep raw keys out of system prompts, chat history, memory files, source repositories, screenshots, analytics, and logs. The agent should use the capability without reading the secret value.
Should an agent use a human employee's OAuth account?
Use delegated human access only when the action genuinely belongs to that person and the service requires it. For persistent operations, prefer a dedicated application, bot, service account, or workload identity. This avoids broken workflows when an employee leaves and makes agent activity easier to identify and revoke.
How often should API keys be rotated?
There is no universal safe interval. Follow the provider's current guidance and your risk policy, automate rotation where possible, and rotate immediately after suspected exposure or a role change. More important, prove that rotation works without downtime and that the old key stops working.
Can an AI agent see its own OAuth token?
The model should not receive the raw token. A connector or tool can attach the token when calling the service while returning only the necessary result to the agent. This design reduces accidental disclosure through prompts, tool output, memory, or a hostile page that asks the agent to reveal credentials.
What should happen when a token expires during a workflow?
Pause the affected step, preserve the last confirmed state, and refresh or reauthorize through the approved identity flow. Do not switch to a broader credential as an emergency shortcut. Before retrying a write, check whether the external service already completed it and use an idempotency key when supported.
Three facts worth quoting
- OAuth lets an application receive limited access without collecting the user's password.
- A private agent runtime does not make an overpowered credential safe.
- The model should use a credential through a tool without seeing the raw secret value.
Sources
- OAuth 2.0 Authorization Framework, RFC 6749
- OAuth 2.0 Security Best Current Practice, RFC 9700
- OAuth 2.0 Resource Indicators, RFC 8707
- OWASP Secrets Management Cheat Sheet
- OpenClaw security documentation
- ClawBud pricing