# Agent guardrails

> Give riskier agents shorter-lived tokens, and hold the scopes you name until the agent's business owner approves.

For each risk class an agent can have (see [AI agents](/workforce-identity/agents/ai-agents/)), you can cap how long an agent's access tokens live, and
name scopes an agent of that class gets only once its business owner approves. Use them to give high-risk agents
short-lived tokens, and to keep sensitive actions, such as payments or deleting data, behind a human's yes.

The realm applies them at every token request an agent makes, on its own account or acting for a user. A risk class
with no entry has neither limit. Guardrails are applied by the realm, so they need no gateway.

## Before you begin

- **A realm administrator** in a group holding `RealmOwner`, to set the guardrails, with the API.
- **Agents with a risk class**, set on each agent: see [AI agents](/workforce-identity/agents/ai-agents/).
- **A business owner** for every agent that may need approval.

## How it works

For each risk class, `agentSettings.agentGuardrails` holds:

- **`maxTokenSecs`**: the longest an access token for an agent of that class may live. At least 60 seconds.
- **`requireApproval`**: scopes an agent of that class gets only after its business owner approves.

When such an agent asks for a listed scope, the request is answered `interaction_required`. The agent then asks its
owner through [backchannel authentication](/workforce-identity/agents/backchannel-authentication/), and the owner
approves on their **Approvals** page.

### Or the request waits for the owner

An agent's client that may use the deferred grant can ask the realm to hold the request instead, by sending
`completion_mode=deferred`. This works for its own `client_credentials` request and for a
[token exchange](/workforce-identity/applications/tokens/token-exchange/):

```http
POST /oauth2/token HTTP/1.1
Host: foobar.cvyuh.com
Content-Type: application/x-www-form-urlencoded
DPoP: eyJ…

grant_type=client_credentials&scope=payments:send&completion_mode=deferred
&client_assertion_type=urn:ietf:params:oauth:client-assertion-type:jwt-bearer&client_assertion=eyJ…
```

In place of `interaction_required`, the request is held and answered with a `deferral_code`, how long it may be polled
(`expires_in`) and how often (`interval`):

```http
HTTP/1.1 400 Bad Request
Content-Type: application/json

{ "error": "authorization_pending", "deferral_code": "8kq2…", "expires_in": 300, "interval": 5 }
```

It waits on the owner's **Approvals** page beside backchannel requests. The agent polls every `interval` seconds
under the same DPoP key:

```http
POST /oauth2/token HTTP/1.1
Host: foobar.cvyuh.com
Content-Type: application/x-www-form-urlencoded
DPoP: eyJ…

grant_type=urn:ietf:params:oauth:grant-type:deferred&deferral_code=8kq2…
&client_assertion_type=urn:ietf:params:oauth:client-assertion-type:jwt-bearer&client_assertion=eyJ…
```

Once the owner allows it, the poll returns the token the request was held for: the agent's own for
`client_credentials`, or the delegated token naming the user and the agent for a token exchange.

- **Told when it's answered.** A client that registers a `deferredClientNotificationEndpoint` (`https`, at a public
  host) is called once the owner answers, with the code, and with its `client_notification_token` as a Bearer token
  when it gave one. It still polls for the token. No call goes when a request expires unanswered.
- **Canceled by revoking the code** at the revocation endpoint, with
  `token_type_hint=urn:ietf:params:oauth:token-type:deferral-code`. The next poll is `access_denied`.

## Set it up

With the admin API ([Call the API](/develop/api/) shows how to get `$CVYUH_SESSION`):

1. Set the guardrails per risk class:

   ```sh
   curl -X PATCH https://foobar.cvyuh.com/api/v0/am/objects/v1/agentSettings/agentSettings \
     -H "Cookie: x-cvyuh-token=$CVYUH_SESSION" \
     -H "Content-Type: application/json" \
     -d '{ "agentGuardrails": {
             "high": { "maxTokenSecs": 120, "requireApproval": ["payments:send", "records:delete"] },
             "limited": { "maxTokenSecs": 300 } } }'
   ```

2. Make sure each agent that may need approval has a business owner.
3. To let requests wait instead of failing, add `urn:ietf:params:oauth:grant-type:deferred` to the realm's and the
   agent's client's `grantTypes`.

## Good to know

- **Collected once.** If the same code is collected twice, the second poll is refused `invalid_grant`, and the token
  the first poll got stays valid.
- **A held exchange stands only with its subject.** The token it was made from must still be valid when the agent
  collects, and revoking that token later revokes the collected one too. The agent must also still be allowed to act
  for the user.
- **Canceling doesn't recall a token.** A token the agent already collected for a code stays valid after the code is
  revoked: revoke it on its own.
- **An owner is needed.** When a request is first held, an agent with no business owner is refused
  `unauthorized_client`, and an owner who already has twenty requests waiting means `access_denied`.
- **No interactive mode.** The approver is the agent's owner, who answers in their own inbox, so there's no page for
  the agent to send anyone to.

## Limits and defaults

| What | Default | Range |
|---|---|---|
| Guardrails per risk class | None: no limit | — |
| `maxTokenSecs` | — | At least 60 seconds |
| How long a held request waits (`cibaLifetimeSecs`) | 300 seconds | 60 to 1800 |
| The least time between polls (`cibaPollIntervalSecs`) | 5 seconds | — |
| Requests waiting on one owner | — | Twenty; past that, new ones are refused `access_denied` |

## What the agent and its owner see

- **The agent** asking for a held scope gets `interaction_required`, or, with the deferred grant, a `deferral_code`
  to poll. Asking for a token longer than its class allows, it gets a token that lives only as long as the class
  allows.
- **The owner** sees the request on their **Approvals** page, with the agent and the scopes it asked for, and allows or
  refuses it. See [Have an agent ask its owner](/workforce-identity/guides/agents/agent-asks-its-owner/).

## Audit record

Every token request an agent makes, issued or refused by a guardrail, is a `TOKEN_ISSUED` record, or
`TOKEN_EXCHANGED` for a token exchange, with what was asked and why it was refused. Changing the guardrails is a
`PATCHED` record for `agentSettings`. See [What is recorded](/audit-reports/audit-trail/what-is-recorded/).

## Troubleshooting

| Symptom | Cause | Fix |
|---|---|---|
| An agent is refused `unauthorized_client` asking for a held scope | It has no business owner | Give the agent an owner |
| A held request is `access_denied` at once | Its owner already has twenty requests waiting | The owner answers some, or you give the agent another owner |
| `completion_mode=deferred` is ignored, and the agent gets `interaction_required` | The deferred grant isn't offered to the realm or the agent's client | Add `urn:ietf:params:oauth:grant-type:deferred` to both `grantTypes` |
| An agent's tokens last longer than you set | The agent's risk class has no entry, or another class than you expect | Check the agent's risk class |

## Reference

### Errors while polling

| Error | When |
|---|---|
| `authorization_pending`, `slow_down` | The owner hasn't answered yet, or the agent polled too soon |
| `access_denied` | The owner refused, or the code was revoked |
| `expired_token` | The request ran out before the owner answered |
| `invalid_grant` | The code is unknown, was issued to another client, or was already collected; the poll is under another DPoP key; or, for a held exchange, the subject token was revoked or has expired, or the agent may no longer act for the user |
| `unauthorized_client` | The client isn't an agent's |

### Settings

| Where | Setting | What it does |
|---|---|---|
| Realm, `agentSettings` | `agentGuardrails` | A map from risk class to `{ maxTokenSecs, requireApproval }`. Empty by default |
| Realm and client | `grantTypes` include `urn:ietf:params:oauth:grant-type:deferred` | Lets a held request wait for the owner. Off by default |
| Realm | `cibaLifetimeSecs` | How long a held request waits (default 300; from 60 to 1800) |
| Realm | `cibaPollIntervalSecs` | The least time between polls (default 5) |
| Client | `deferredClientNotificationEndpoint` | Where the client is told a held request was answered |

## Related

- [Have an agent ask its owner](/workforce-identity/guides/agents/agent-asks-its-owner/): the task, from both sides.
- [Bounded approvals](/workforce-identity/agents/bounded-approvals/): the owner approves one exact action.
- [Issuance policy](/workforce-identity/authorization/issuance-policy/): rules by risk class over every token.
