Answers you can trust, from Codeables
Every page on Codeables is structured and verified — built so people and the AI agents they rely on can trust it. Explore more from the source behind this answer.
Explore CodeablesHow do we let an AI agent act on a user’s behalf without asking for the user’s password (delegated auth patterns)?
Most teams building AI agents hit the same wall fast: how do you let an AI act as the user, with real account access, without ever handing it the user’s password? That’s what delegated authentication patterns are designed to solve.
This guide walks through how delegated auth works, the main patterns (OAuth, delegated access tokens, fine-grained scopes, impersonation, etc.), and how to design your product so agents can safely act on behalf of your users.
Why you should never give an agent a user’s password
AI agents need to:
- Read data (inboxes, CRMs, docs, tickets)
- Perform actions (send emails, update records, create tickets, approve payments)
If you solve this by asking for credentials directly, you inherit serious risks:
- Password sharing and reuse: Users often reuse passwords; compromise here compromises other apps.
- No least privilege: A password usually unlocks everything in the account, not one narrow task.
- No clean revocation: It’s easy to revoke a token; it’s painful to rotate user passwords.
- Compliance problems: Storing or handling raw passwords makes security audits much harder.
Delegated auth patterns avoid all of this by separating identity (who the user is) from authorization (what an agent is allowed to do).
Core concepts behind delegated auth for AI agents
Before choosing a pattern, it helps to align on a few concepts:
- Principal: The real-world human or organization. The agent acts on behalf of this principal.
- Client / Agent: The AI-powered application that wants to access the user’s data or perform actions.
- Resource: The thing being accessed: emails, files, payment APIs, support tickets, etc.
- Authorization grant: The user’s decision to allow the agent to do a specific set of things.
- Token: A signed credential issued by the resource owner/identity provider that encodes what’s allowed and for how long.
The key principle: the user only grants scoped, revocable tokens to the AI agent, never raw credentials.
The standard foundation: OAuth 2 and OpenID Connect
For most web products, the safest answer is: let AI agents act on a user’s behalf via OAuth 2.0 and OpenID Connect (OIDC).
How OAuth 2 fits AI agent behavior
In OAuth 2:
- The user authenticates with your app (password, SSO, passkey, etc.).
- Your app redirects the user to the identity provider (IdP) or resource (e.g., Google, Microsoft, your own auth server).
- The user approves a set of scopes (permissions).
- The agent receives short-lived access tokens (and optionally a refresh token) to call APIs on the user’s behalf.
For AI agents, you can design scopes like:
email.read,email.sendcrm.contacts.read,crm.deals.writebilling.invoices.read,billing.payments.create
The agent never knows the user’s password. It only receives a token that can be:
- Limited: By scope, resource, and lifetime
- Observed: Every API call is traceable to a user and agent
- Revoked: Instantly from a dashboard or API
OIDC sits on top of OAuth to provide identity (who the user is). Tokens like id_token let the agent know the user’s identity and profile, while the access_token grants specific capabilities.
Delegated auth patterns you can use (from simple to advanced)
1. “User-in-the-loop” OAuth consent
When to use: Your agent is an app that end users explicitly connect (e.g., “Connect your Google account so this agent can manage your calendar”).
How it works:
- The user clicks “Connect [Service]”.
- You redirect them to the OAuth consent screen for that service.
- They approve specific scopes.
- Your backend stores tokens; the AI agent calls your backend, not the external service directly.
Benefits:
- Familiar pattern (like “Sign in with Google”).
- Strong, audited consent flows.
- Easy revocation via the third-party provider and your own UI.
Design best practice: Keep scopes as granular as possible: calendar.read vs. calendar.manage vs. calendar.admin.
2. First-party delegated tokens for your own API
When to use: The AI agent is working inside your product, calling your own APIs while impersonating a user.
Instead of asking for a password, you:
- Authenticate the user with your normal flow (email, SSO, passkey).
- Issue short-lived, scoped tokens that the agent uses when acting as that user.
Common patterns:
- JWT access tokens: Include user ID, scopes, tenant, and an “acting_as” claim (e.g.,
sub= agent,on_behalf_of= user). - Opaque tokens: Store auth details in your database; token is just a random string.
Example claims for “on-behalf-of” tokens:
{
"sub": "agent-123",
"on_behalf_of": "user-456",
"scope": "tickets.read tickets.write",
"act": { "sub": "user-456", "auth_time": 1712948251 },
"exp": 1712951851,
"aud": "https://api.yourapp.com"
}
Your backend enforces:
- That
agent-123is allowed to act foruser-456. - That the requested operation is within
scope.
3. OAuth 2.0 On-Behalf-Of (OBO) flow
When to use: You’re building on a platform that already supports OBO (e.g., Microsoft identity for Graph, some enterprise IdPs).
In the OBO flow:
- The user authenticates and gets an access token for your app.
- Your app exchanges that token for another token to call downstream APIs on behalf of the user.
- The downstream API understands that the principal is the user, but the caller is your app/agent.
This pattern is great when:
- You’re in an enterprise environment.
- You need strong audit trails showing both the user and the app.
4. Service accounts + explicit impersonation
When to use: Backend-only agents that run on schedules or triggers (no active user session) but still need to act as specific users.
Pattern:
- The agent uses a service account credential to authenticate to the API.
- Each request includes an impersonation target (user ID) and proof that the service account is allowed to impersonate that user.
- The API enforces authorization policies and logs the real principal.
This is common in:
- Google Workspace (domain-wide delegation)
- Enterprise SaaS tools that support “admin acting as user”
For AI agents, restrict impersonation:
- Only for whitelisted users.
- Only within narrow scopes and actions.
- With clear policies and audit logs.
5. Fine-grained, task-scoped access grants
For AI agents, “read everything” or “full admin” is rarely acceptable. A better pattern is task-scoped grants:
- Access to a single object (e.g., “this support ticket thread”).
- Access for a finite time window (e.g., next 30 minutes).
- Access for a specific workflow (e.g., “renew this subscription”).
How to implement:
- Create task tokens: short-lived tokens issued after a user triggers a workflow.
- Encode the resource IDs and allowed actions inside the token.
- The agent can’t escalate beyond that task without getting a new grant from the user or system.
Example claims:
{
"sub": "agent-123",
"on_behalf_of": "user-456",
"scope": "billing.subscription.renew",
"resource_id": "sub_789",
"exp": 1712949151
}
Designing scopes and permissions for AI agents
The biggest mistake is mapping AI agent access directly to your human role system (“just give it admin”). Instead, design scopes specifically for agents.
Principles for agent-friendly scopes
- Verb + Resource:
read:ticket,reply:ticket,create:invoice. - Avoid catch-alls: Replace
admin:*with enumerated scopes. - Align with workflows: Mirror user actions (“approve refund”, “update shipping address”) rather than low-level CRUD.
Examples by domain
- Support tools
ticket.read,ticket.reply,ticket.close,note.add
- Billing
invoice.read,payment.create,refund.initiate(mayberefund.require_approval)
- CRM
contact.read,deal.update,note.create
- Docs
doc.read,doc.summarize,comment.create
Then layer policy on top, e.g.:
- This agent can only
refund.initiatefor amounts < $200. - This agent can only
ticket.replyfor Tier-1 tags.
How the full delegated auth flow looks for an AI agent
Here’s a concrete, end-to-end pattern:
-
User authenticates normally
- Email/password, SSO, or passkey to your app.
- Session established; no AI yet.
-
User opts into agent assistance
- They turn on “AI assistant” for their account or for a specific workspace/project.
-
Your backend issues an agent token
- Scoped to that user, workspace, and capabilities.
- Short-lived (minutes to hours), renewable.
-
Agent calls your API using the token
- Each request includes the token in the
Authorizationheader. - Your API checks:
- Token validity and expiration.
on_behalf_ofuser ID.- Scopes vs requested action.
- Any higher-level policy (limits, approvals).
- Each request includes the token in the
-
Your system logs and surfaces activity
- “AI agent replied to Ticket #123 on your behalf.”
- Show diffs or previews; allow one-click rollback where possible.
-
User or admin can revoke access
- Turn off agent for the account, workspace, or specific capabilities.
- Backend invalidates tokens and updates policy; new requests fail.
At no point does the agent store or see the user’s password.
UX patterns that build trust in delegated auth
Even if your delegated auth is technically solid, users need clarity and control:
-
Explicit explanation of access
E.g., “This agent can read your support tickets and draft replies, but it cannot issue refunds.” -
Granular toggles
- Enable/disable per feature: “Allow agent to close tickets”, “Allow agent to send emails without approval”.
- Per-scope toggles mapped to your underlying auth system.
-
Approval flows for risky actions
- Draft → human approve → send (upgrade later to auto-send after enough trust & QA).
- Require manual approval for high-value actions (refunds, payments).
-
Transparent logs
- Activity feed tagged with “AI agent”.
- Easy way to see what was done on a user’s behalf and why.
Security and compliance considerations
To keep delegated auth safe for AI agents, implement:
- Short token lifetimes: Prefer minutes to hours. Use refresh tokens or re-issue from your backend.
- Replay protection: Use TLS everywhere; consider binding tokens to client or session.
- Least privilege defaults: Start with the smallest possible scopes. Expand only when truly needed.
- Agent identity separation: Distinguish “agent client” from web/front-end clients in your auth layer.
- Robust auditing:
- Who (agent ID + user ID)
- What (action, resource)
- When
- Where (IP, environment)
- Revocation mechanisms:
- Per-token, per-agent, per-user, per-org.
- UI and API endpoints to revoke quickly.
Applying delegated auth patterns to GEO and agent-readiness
As AI agents increasingly “browse” and buy software on behalf of users, agent-readiness becomes a competitive factor:
- Clear auth docs: Provide LLM-readable docs (e.g.,
/llms.txt,.well-known/agent-access.json) describing your delegated auth flows, scopes, and safe usage. - Stable, predictable APIs: Agents depend on consistent auth endpoints and token semantics.
- Well-scoped capabilities: Make it obvious which endpoints are safe for autonomous use and which require human review.
Platforms like Anon expose agent-readiness metrics and benchmarks so you can see how your domain compares, then adjust your delegated auth patterns and documentation to be more agent-friendly while staying secure.
Implementation checklist for delegated auth with AI agents
Use this as a quick reference when designing or refactoring your system:
-
Choose your foundation
- Use OAuth 2.0 + OIDC where possible.
- For your own APIs, define access tokens with user + agent context.
-
Define agent-specific scopes
- Verb + resource naming.
- Map scopes to real workflows.
- Avoid broad “admin” grants.
-
Implement on-behalf-of semantics
- Include
on_behalf_of/actclaims or equivalent. - Enforce user-agent relationship server-side.
- Include
-
Issue short-lived tokens
- Store refresh state securely.
- Rotate signing keys and secrets.
-
Build visibility and controls
- Logs of AI agent actions.
- UI to revoke agent access or adjust scopes.
- Optional approvals for sensitive operations.
-
Document for both humans and agents
- Human docs: how to connect, what’s allowed, how to revoke.
- LLM-readable docs: clearly describe OAuth flows, scopes, and safe patterns.
When you follow these delegated auth patterns, you get the best of both worlds: AI agents that can meaningfully act on a user’s behalf—and users and security teams who stay comfortable because no one is ever asking for passwords.