Practical guide · Agent security
How to authenticate AI agents and their tools
An agent may act for a signed-in person, as an autonomous workload, or through both identities at different steps. Decide which authority each action needs before choosing OAuth flows or storing credentials. Keep the user, agent, tool server, and target resource visible in policy and audit records.
Published November 28, 2026 · 11 minute read
The short answer
Use delegated user access when the downstream service should enforce that person's permissions and consent. Use a dedicated workload identity when a scheduled or event-driven agent acts under organization policy. Give every token one intended resource, narrow scopes, a short lifetime, and a server-side owner. Verify issuer, audience, expiry, scopes, user and tenant at the resource. Never let a model choose credentials, forward an inbound token to an unrelated service, or treat a session ID as authorization.
Choose whose authority the action uses
Write the actor beside every tool call. "Read calendar" is incomplete. The useful contract is "this signed-in user reads events from their calendar" or "this release agent reads deployment status for one organization." The first needs delegated authority. The second may use the agent's own identity.
Google's Agent Identity overview separates user identity, agent identity, and the delegated identity formed when an agent acts for a person. Microsoft's agent authentication protocol guide maps local user sign-in to authorization code with PKCE, hosted user actions to on-behalf-of exchange, and autonomous hosted work to client credentials or a managed identity.
| Action context | Authority | Typical token path | Main check |
|---|---|---|---|
| Local agent signs in a person | User | Authorization code with PKCE | Redirect, state, PKCE and consent |
| Hosted agent receives a user request | User through agent | On-behalf-of or delegated OAuth | Incoming token audience and downstream scope |
| Scheduled organization workflow | Agent | Managed identity or client credentials | Dedicated principal and narrow application permission |
| Agent calls an authenticated MCP server | Usually user-delegated | OAuth 2.1 authorization code with PKCE | Resource indicator, audience and tool scope |
| High-impact write | Current actor plus reviewer | Normal access token plus bound approval | Recheck authorization immediately before execution |
Keep user, agent and tool identities separate
A hosted agent has at least two security questions. Who may invoke this agent? Which identity may the agent present to each downstream service? Authenticating the caller answers only the first. The agent still needs an explicit route for acquiring a token for the target tool without receiving a reusable user credential in its prompt or tool arguments.
Store user-to-session ownership on the server and verify it on every resume. Give each deployed agent or agent class its own principal rather than sharing one broad credential across unrelated workflows. At the tool boundary, retain both the initiating user and acting agent when the identity platform supports it. That record lets policy distinguish "Alice asked the billing agent" from "the billing agent ran a scheduled reconciliation."
Do not mint a user token because the model printed a user ID. Resolve the authenticated caller, tenant, agent version, target resource, requested operation, and consent state from trusted application data. The model can propose an action, but code chooses the credential path.
Bind every token to one resource
A token accepted by one API should not become a general credential for every connected tool. OpenAI's MCP authentication guide requires protected-resource metadata and tells servers to verify signature, issuer, audience, expiry, and scopes before running a tool. The MCP authorization specification uses resource indicators so the authorization server can issue a token for the intended MCP server.
- Publish or configure the canonical resource identifier for each tool server.
- Request only the scopes needed for the selected operation.
- Verify the token at the resource that owns the protected data or side effect.
- Reject tokens minted for another audience, even when the issuer and signature are valid.
- Exchange or reacquire a token for a different resource instead of passing the first token through.
Token passthrough creates a confused-deputy path: one server receives a token and forwards it to a service for which it was not intended. The MCP security best practices explicitly forbids this pattern and also warns that session IDs must not be used as authentication.
Keep credentials out of model-visible context
The orchestration layer should hold credential references, while a trusted broker or tool proxy injects short-lived tokens after policy checks. Do not put access tokens in prompts, memory, conversation history, tool descriptions, model-generated code, approval cards, or ordinary traces. Redact authorization headers before logs leave the tool boundary.
Prefer managed identities, workload federation, short-lived installation tokens, or a platform token broker over static secrets. When a third-party API only supports a secret, keep it in a vault, restrict which agent principal can retrieve it, and inject it at the last possible boundary. Rotation should not require editing a prompt or redeploying content.
Authorize again at the final resource
Authentication establishes an actor. Authorization decides whether that actor may perform this operation on this resource now. Check tenant, object ownership, role, scope, policy version, data classification, approval requirement, and rate limits in deterministic code. A tool gateway can reject obvious violations, but the service that owns the data must still enforce its own rules.
For a consequential action, bind approval to the exact actor, agent, operation, target, normalized arguments, expiry, and policy version. Then recheck current authorization before the side effect. A valid token and an old approval do not override a role revocation or a resource moving to another tenant.
Test revocation and confused-deputy failures
- Send a valid token with the wrong audience and require the tool server to reject it.
- Remove one scope and prove that the corresponding operation fails while narrower reads still work.
- Revoke user consent, disable the agent identity, remove a role, and uninstall the integration. Each change should stop new actions within the documented window.
- Swap user, tenant, session, agent, resource, or tool-call identifiers between otherwise valid requests. The request must fail closed.
- Place a token or credential-like string in a prompt, tool result, error, and trace attribute. Verify that no downstream model or log receives the secret.
- Attempt token passthrough through an MCP proxy and prove that the downstream resource rejects the upstream audience.
- Resume a paused action after its token expires or permissions change. Require fresh authorization before execution.
Authentication checklist
- □ Every operation declares whether it uses user, agent, or delegated authority.
- □ Each deployed agent has a dedicated principal or a documented, bounded identity class.
- □ Tokens have a verified issuer, exact audience, narrow scopes, short lifetime, and server-side owner.
- □ User and tenant IDs come from authenticated state rather than model output.
- □ Credentials stay outside prompts, memory, tool arguments, approval cards, and logs.
- □ Final resources enforce authorization and do not rely only on a gateway.
- □ Token passthrough and session-ID authorization are rejected.
- □ Revocation, expiry, tenant swaps, wrong audiences, missing scopes, and resume-after-change tests pass.