# Agents acting for users

> Decide which agents may act for which users, until when and with which scopes at most, with the realm's own delegation policies having the final say.

An assistant that reads a user's mail, or an agent that pays their invoices, acts for that user. The realm lets it do
so only when two things say yes: the agent's link to the user, which sets a window and the most its tokens may hold,
and the realm's own **delegation policies**, which you write. The agent then gets a token that names the user as the
subject and itself as the actor, so every API and every audit record sees both.

Neither yes is enough alone. A user can't let an agent act for them against the realm's rules, and a policy can't let
an agent act for a user it has no link to.

## Before you begin

- **Who can do it:** a realm administrator (a group holding `RealmOwner`) writes the links and the delegation
  policies; `RealmViewer` can read them. Users take a link back themselves, on their **AI Agents** page.
- **Where:** today this is done through the API ([Call the API](/develop/api/) shows how to get `$CVYUH_SESSION`).
  Users see and stop the agents acting for them on their own **AI Agents** page.
- **First:** the agent and its client are registered ([AI agents](../ai-agents/)), and the realm and the client offer
  [token exchange](../../applications/tokens/token-exchange/).

## How it works

### The agent's link to the user

On the agent, an `actsFor` link to the user (or to another agent) it may act for, with:

| Property | What it does |
|---|---|
| `validFrom`, `validTo` | When the link is open. Outside that window it doesn't exist |
| `scopes` | The most the agent's token may hold. With none, the user's own scopes bound it |
| `impersonate` | Whether the agent may also act *as* the user, with no record of itself in the token |
| `grantedBy` | Who granted it |

Whoever may write the agent may write its links.

### The realm's delegation policies

The policy set named by `authorizationSettings.delegationPolicySet` (by default `delegation`) is asked about the
resource `delegation://<type>/<id>` (the type of the one acted for, such as `user` or `agent`, and its id), for the
action `delegate` (act for) or `impersonate` (act as). The agent is the subject, with the claims its own token
carries: its risk class, autonomy tier and owner among them. The set is born **empty**, so no agent acts for anyone
until you write a policy.

Only when both say yes does [Token exchange](../../applications/tokens/token-exchange/) issue the token: the user as
`sub`, the agent in `act`, and scopes no wider than the user's token, the link's `scopes` and the client's.

### Acting as the user

A token that names only the user, with no `act`, needs three yeses: the realm's
`agentSettings.tokenExchangeImpersonation`, the link's `impersonate`, and a policy allowing `impersonate`. Without
all three, the token names the agent.

## Set it up

Let `invoice-agent` act for `maya` until a date you choose, reading invoices only. Each call sends your session as
`$CVYUH_SESSION` ([Call the API](/develop/api/)).

```sh
curl -X POST https://foobar.cvyuh.com/api/v0/am/objects/v1/agent/invoice-agent/relationships/actsFor \
  -H "Cookie: x-cvyuh-token=$CVYUH_SESSION" \
  -H "Content-Type: application/json" \
  -d '{ "targetId": "maya", "targetType": "user",
        "properties": { "validTo": "<RFC 3339 time>", "scopes": ["invoices:read"] } }'
```

Then allow delegation by a policy in the `delegation` set, such as:

```json
{ "active": true,
  "resources": ["delegation://user/*"],
  "actionValues": { "delegate": true },
  "subject": { "type": "JwtClaim", "claimName": "principal_type",
               "claimValue": "agent" } }
```

[Let an agent act for a user](../../guides/agents/agent-acts-for-a-person/) walks through every step, including
offering token exchange and linking the policy to its set.

## What the user sees

The user sees each agent allowed to act for them on their **AI Agents** page, with what it may do and until when, and
can **Stop** any of them: see [Let users remove agents](../../guides/agents/people-remove-agents/).

## Limits and defaults

| Setting or limit | Default | Range |
|---|---|---|
| Delegation policy set (`delegationPolicySet`) | `delegation`, born empty: no agent acts for anyone | Any policy set in the realm |
| Agents acting in turn (`maxActDepth`) | 4 | 1 to 16 |
| Acting as the user (`tokenExchangeImpersonation`) | Off | On or off |

## Troubleshooting

| Symptom | Cause | Fix |
|---|---|---|
| Every exchange is refused, though the link exists | No policy in the delegation set allows `delegate` for this agent | Write and link an allowing policy ([Let an agent act for a user](../../guides/agents/agent-acts-for-a-person/)) |
| Refused after working for a while | The link's `validTo` has passed, or the user took the agent back | Write a new link; ask the user |
| The token names the agent in `act` when you wanted the user alone | One of the three yeses for acting as the user is missing | Turn on `tokenExchangeImpersonation`, the link's `impersonate`, and a policy allowing `impersonate` |
| The token holds fewer scopes than asked | Scopes are narrowed to the user's token, the link's `scopes` and the client's | Widen the narrowest of the three |

## Audit record

Every exchange is a `TOKEN_EXCHANGED` record, allowed or refused, keyed by the user, with the agents in the chain and
the token it came from. Every delegation decision is a `POLICY_DECIDED` record. A user taking an agent back is
`AGENT_TAKEN_BACK`. Changes to an agent's links are in its change history: **Audit → Config**.

## Good to know

- **Anything but an allow refuses.** A deny, no matching policy, an undecided condition, or a realm without the set:
  no token.
- **An agent doesn't act for itself**, and an agent already in a token's `act` chain doesn't act in it again.
- **Chains are bounded.** At most `agentSettings.maxActDepth` agents may act in turn (default `4`, at most `16`).
- **Taking it back stops the tokens.** A user who takes back an agent's delegation stops every token in which that agent
  acts for them, at the realm and at the gateway. Stopping the agent stops every token it's in, at any depth. See
  [What can be revoked](/gateway/revocation/what-can-be-revoked/).
- **On the audit trail.** Every decision an agent is in goes on the realm's audit trail, allow or deny.

## Reference

### Fields

| Where | Field | What it sets |
|---|---|---|
| Agent | `actsFor` link, with the properties above | Whom it may act for, when, and how far |
| Realm, `authorizationSettings` | `delegationPolicySet` (`delegation`) | The policy set that decides |
| Realm, `agentSettings` | `maxActDepth` (`4`, up to `16`) | How many agents may act in turn |
| Realm, `agentSettings` | `tokenExchangeImpersonation` (`false`) | Whether acting as a user is possible at all |
| Policy in the delegation set | `resources` such as `delegation://user/*`, `actionValues` such as `{ "delegate": true }`, a `subject` | Which agents may act for whom |

## Related

- [Let an agent act for a user](../../guides/agents/agent-acts-for-a-person/): the task, start to finish.
- [AI agents](../ai-agents/): the agent itself, its owner and its risk class.
- [Token exchange](../../applications/tokens/token-exchange/): how the delegated token is issued.
- [Policies](../../authorization/policies/): how delegation policies are written.
- [Agent guardrails](../../../gateway/guardrails/agent-guardrails/): scopes an agent gets only once its owner approves.
