In 2026, weak non-human identity (NHI) management was linked to 41% of identity breaches (Sophos), and Gartner forecasts that by 2028, 25% of enterprise breaches will involve AI agent abuse. Yet most teams still deploy autonomous agents the same way they wrote cron scripts in 2015: by dropping a static, long-lived API key into an environment variable.
When an AI agent can call MCP servers, query internal APIs, and spawn subagents, a static key gives it blanket privileges with zero audit trail. Extending our earlier guide on running Hermes Agent with Caddy and Keycloak, here is how to give agents their own short-lived, scoped OAuth identities using Keycloak.
1. Stop Sharing User Tokens or Static Keys
If an agent borrows your personal OAuth token or a shared service API key, two things break immediately:
- No attribution: Audit logs cannot distinguish a human engineer’s action from an agent’s hallucinated tool call.
- Blast radius: If one agent is prompt-injected, revoking its key breaks every other integration sharing that credential.
2. Pattern A: Workload Identity (client_credentials)
When an agent acts on its own behalf (for example, a nightly triage bot or CI reviewer), register it in Keycloak as a confidential client with a service account enabled, short access-token lifespans (60–300 seconds), and refresh tokens disabled:
curl -s -X POST https://auth.example.com/realms/agents/protocol/openid-connect/token -H "Content-Type: application/x-www-form-urlencoded" -d "grant_type=client_credentials" -d "client_id=agent-triage-svc" -d "client_secret=${AGENT_SECRET}" -d "scope=tickets:read"
Now every request carries sub = agent-triage-svc, is scoped strictly to tickets:read, and can be disabled in one click in the Keycloak Admin Console without touching human accounts.
3. Pattern B: On-Behalf-Of Delegation with RFC 8693 Token Exchange
When an agent acts for a specific user—and needs to call a downstream MCP server or microservice—use Standard Token Exchange (V2) in Keycloak, which implements OAuth 2.0 Token Exchange (RFC 8693).
Keycloak’s client delegation flow works in three steps:
- 1. User consent with
may_act: The client requests the parameterized scopedelegation:client:actor-app. Keycloak embeds amay_actclaim naming the agent’s service account and addsactor-appto theaudclaim. - 2. Agent authenticates itself: The agent obtains its own
actor_tokenviaclient_credentials. - 3. Delegated exchange: The agent swaps the user’s
subject_tokenplus its ownactor_tokenfor a narrower, audience-bound token aimed at one downstream service:
curl -s -X POST https://auth.example.com/realms/agents/protocol/openid-connect/token -H "Content-Type: application/x-www-form-urlencoded" -d "grant_type=urn:ietf:params:oauth:grant-type:token-exchange" -d "client_id=actor-app" -d "client_secret=${ACTOR_SECRET}" -d "subject_token=${USER_ACCESS_TOKEN}" -d "subject_token_type=urn:ietf:params:oauth:token-type:access_token" -d "actor_token=${AGENT_ACTOR_TOKEN}" -d "actor_token_type=urn:ietf:params:oauth:token-type:access_token" -d "audience=mcp-billing-readonly"
4. Two Keycloak Guardrails You Should Never Skip
- Cap exchanged lifetimes: Keep Allow exchange token expiration after subject token expiration OFF. The exchanged token will inherit the remaining lifetime of the user’s
subject_tokenso an agent cannot silently extend access. - Disable refresh tokens on exchanged grants: Force the agent to re-exchange a fresh
subject_tokenrather than holding a long-lived refresh token. Because revocation does not cascade across exchanged access tokens, short TTLs (1–5 minutes) are your primary safety net.
Bottom Line
Autonomous agents are non-human identities, not extensions of your laptop shell. Give every agent its own Keycloak client, use RFC 8693 token exchange to down-scope permissions per downstream tool, and keep token TTLs measured in minutes.