# AI agents

> Give each AI agent an identity of its own, with an owner, a risk class and a status, no secret to leak, and one change that stops it and everything under it.

An agent is an AI agent or other automation registered in your realm as an identity of its own. Each one has a
purpose, a risk class, a business owner who answers for it, and a status you can change at any time. It gets
short-lived tokens with no password or shared secret, and every token tells your APIs that an agent is calling,
which one, how risky it is and who owns it.

Register every agent that calls your APIs, whether it works on its own account or for a user. Then a user can let it
act for them through [token exchange](../../applications/tokens/token-exchange/), it can ask its owner before a sensitive
action through [backchannel authentication](../backchannel-authentication/), and you can stop it with one change.

## Before you begin

- **Who can do it:** a realm administrator: a member of a group that holds `RealmOwner` in this realm or a realm above
  it ([Delegated administration](/platform/realms/delegated-administration/)). `RealmViewer` can read agents but not
  change them.
- **Where:** today this is done through the API ([Call the API](/develop/api/) shows how to get `$CVYUH_SESSION`).
  Users see the agents acting for them on their own **AI Agents** page.
- **First:** the user who will be the agent's business owner exists, and your API's scopes are among the realm's
  (`scopes` in `oauth2Settings`).

## How it works

### What an agent is

| Field | What it holds |
|---|---|
| `uid` | Its ID, unique in the realm. Required |
| `displayName` | Its name. Required |
| `purpose` | What it's for, at most 512 characters. Required |
| `riskClass` | `minimal`, `limited`, `high` or `unacceptable`. Required |
| `autonomyTier` | 1 to 4: how far it acts without a human. Required |
| `status` | `active`, `quarantined`, `killed` or `retired`. Required |
| `statusChangedAt` | When its status last moved. The realm stamps it; a write that sets it is refused |
| `expiresAt` | When it stops, if ever |
| `businessOwner` | The one user accountable for it, who approves what it asks |
| `technicalOwner` | The users or teams who run it; an agent may have several |
| `parent` | The agent or user it was created under. Set once, never changed |
| `framework` | Names you keep for your own records |

### How it gets tokens

An agent holds **no password and no secret**. It gets tokens through an OAuth client linked to it, proving itself
with a signed assertion only: with a key it holds (`registeredKey`), with a one-time assertion you mint for it
(`amBootstrap`), or with its platform's workload identity (`spiffe`, see [Workload identity](../workload-identity/)).

Its tokens carry claims your APIs and the gateway can decide on:

```json
{
  "sub": "6f1c2a9e-4b7d-4e3a-9c51-2d8f0b7a1e44",
  "client_id": "invoice-agent-client",
  "principal_type": "agent",
  "mode": "autonomous",
  "risk_class": "high",
  "autonomy_tier": 2,
  "business_owner": "maya"
}
```

| Claim | What it says |
|---|---|
| `sub` | The agent's ID in the realm, which never changes |
| `principal_type` | `agent` for an agent, `user` for a user |
| `mode` | `autonomous` when the agent acts on its own account; `delegated` when it acts for a user through [token exchange](../../applications/tokens/token-exchange/) |
| `risk_class`, `autonomy_tier` | Copied from the agent |
| `business_owner` | The owner, by the realm's naming attribute, such as their username. Absent when the agent has none |

When it acts for a user, the user is `sub` and the agent is in `act`, with the same claims.

### Quarantine and release

**Quarantine** an agent to pause it: it gets no new tokens and acts for no one, and neither does any agent created
under it. Set it back to `active` to release it. A quarantine is sent as account-disabled, and a release as
account-enabled, to anyone following the agent through
[Send signals](../../signals/shared-signals/).

### Kill and retire

**Kill** an agent to stop it for good: it and every agent created under it get no new tokens, ever, and the tokens
already issued across that whole subtree are revoked. They read inactive at
[Introspection](../../applications/revoke-access/introspection/) and are refused by the gateway. **Retire** an agent
you're done with; like a kill, it's final.

Killed and retired are final: no one can bring the agent back or move it to another parent. To replace one, create a
new agent. Every status change is written to the audit trail, and a kill is sent as account-disabled, as a quarantine
is.

### The chain check

Every token request walks the agent's chain up through `parent` to the user at the top. **Every link must be active
and unexpired, and that user's account must be active.** So one change stops a whole tree: deactivate the user at
the top, and every agent under them stops at its next request. The check also refuses a chain with a loop, or a link
that is missing.

## Set it up

Each call sends your session as `$CVYUH_SESSION` ([Call the API](/develop/api/)).

<Steps>

1. Create the agent. The required fields are enough to start:

   ```sh
   curl -X POST "https://foobar.cvyuh.com/api/v0/am/objects/v1/agent?id=invoice-agent" \
     -H "Cookie: x-cvyuh-token=$CVYUH_SESSION" \
     -H "Content-Type: application/json" \
     -d '{ "uid": "invoice-agent", "displayName": "Invoice agent",
           "purpose": "Pays approved supplier invoices",
           "riskClass": "high", "autonomyTier": 2, "status": "active" }'
   ```

2. Name `maya` as its business owner:

   ```sh
   curl -X POST https://foobar.cvyuh.com/api/v0/am/objects/v1/agent/invoice-agent/relationships/businessOwner \
     -H "Cookie: x-cvyuh-token=$CVYUH_SESSION" \
     -H "Content-Type: application/json" \
     -d '{ "targetId": "maya" }'
   ```

3. Give it a client that authenticates by assertion, here with a key it registers. Each key must state when it ends
   (`exp`), and each of its `scopes` must be one the realm offers (`scopes` in `oauth2Settings`), so add your API's
   scopes, such as `invoices:read`, there first:

   ```sh
   curl -X POST "https://foobar.cvyuh.com/api/v0/am/objects/v1/oauth2Client?id=invoice-agent-client" \
     -H "Cookie: x-cvyuh-token=$CVYUH_SESSION" \
     -H "Content-Type: application/json" \
     -d '{ "status": "active",
           "tokenEndpointAuthMethod": "private_key_jwt",
           "assertionTrust": "registeredKey",
           "grantTypes": ["client_credentials"],
           "scopes": ["invoices:read"],
           "allowedResources": ["https://app.example.com/api"],
           "defaultAudience": "https://app.example.com/api",
           "jwks": { "keys": [
             { "kty": "EC", "crv": "P-256", "kid": "agent-1", "x": "…", "y": "…",
               "exp": <unix time, at most 90 days ahead> } ] } }'
   ```

4. Link the client to the agent:

   ```sh
   curl -X POST https://foobar.cvyuh.com/api/v0/am/objects/v1/oauth2Client/invoice-agent-client/relationships/agent \
     -H "Cookie: x-cvyuh-token=$CVYUH_SESSION" \
     -H "Content-Type: application/json" \
     -d '{ "targetId": "invoice-agent" }'
   ```

5. The agent asks for a token at `https://foobar.cvyuh.com/oauth2/token` with an assertion signed by its key, as any
   `private_key_jwt` client does, with a [DPoP](../../applications/client-security/dpop/) proof.

</Steps>

[Register an agent](../../guides/agents/register-an-agent/) walks through every step, including the
one-time bootstrap assertion.

## Limits and defaults

| Setting or limit | Default | Range |
|---|---|---|
| `purpose` | Required | At most 512 characters |
| `autonomyTier` | Required | 1 to 4 |
| An agent key's end (`agentKeyMaxLifetimeSecs`) | 90 days | 1 hour to 1 year |
| A bootstrap assertion's life (`bootstrapAssertionLifetimeSecs`) | 300 seconds, used once | At most 3,600 seconds |
| Agents acting in turn for one user (`maxActDepth`) | 4 | 1 to 16 |
| Acting as the user (`tokenExchangeImpersonation`) | Off | On or off |

## Troubleshooting

| Symptom | Cause | Fix |
|---|---|---|
| The agent gets `invalid_client` it didn't get before | A link in its chain stopped: the agent, an agent above it or the user at the top is no longer active, or one is past its `expiresAt`; or its key has ended | Read the agent and each `parent` up to the user; release or reactivate the link that stopped, or register a new key |
| Writing the client's keys is refused | A key has no `exp`, or ends further off than `agentKeyMaxLifetimeSecs` | Give each key an `exp` within the realm's maximum |
| A status change is refused `400` | The agent is `killed` or `retired`, which it never leaves | Create a new agent |
| Its token request is refused `unauthorized_client` | The client asked for a grant a user signs in with | Use `client_credentials`, [token exchange](../../applications/tokens/token-exchange/) or [backchannel authentication](../backchannel-authentication/) |

## Audit record

- **Every status move** is recorded about the agent, on the trail of whoever made it and of the agent's owner:
  `AGENT_QUARANTINED`, `AGENT_RELEASED`, `AGENT_KILLED` or `AGENT_RETIRED`, with the status before and after, and
  whether an administrator or a received signal made it.
- **Every token request** at the token endpoint is a `TOKEN_ISSUED` record, issued or refused. A refusal is keyed by
  the client that asked, with its error (such as `invalid_client`), but not the link that stopped the chain.
- **Every change to the agent's record** is in its change history, before and after: **Audit → Config** in the
  dashboard.

Read one agent's whole trail with [Follow what an agent did](../../guides/agents/follow-what-an-agent-did/).

## Good to know

- **Assertions only.** An agent's client authenticates by `private_key_jwt` or `attest_jwt_client_auth`, never by a
  secret.
- **Keys must end.** Every key an agent's client registers states an `exp` that hasn't passed and is no further off
  than `agentKeyMaxLifetimeSecs`, 90 days by default. A write that breaks this is refused, naming the key, and a key
  past its end authenticates nothing.
- **A bootstrap assertion works once,** within `bootstrapAssertionLifetimeSecs`, 300 seconds by default.
- **Agents are held to FAPI 2.0 by default.** A client given its `agent` link and naming no `securityProfile` is
  written `fapi2`: its tokens are DPoP-bound and its assertions signed `ES256` or `PS256`. One that authenticates by
  attestation is written `oauth2.1` instead. Name `oauth2.1` or `none` to hold an agent's client to less. See [Security profiles](../../applications/client-security/security-profiles/).
- **An agent never signs a user in.** Its client can't use the authorization code, device or identity assertion
  grants, and can't be a first-party app.
- **Lineage is fixed.** An agent's `parent` and the agents under it are set when it's created and can't be rewritten,
  so a chain of delegation stays as it was made.
- **The chain is checked at every token request,** for status, expiry, loops and type, up to the user at the top.
- **Attestation details are kept, not read.** `attestationType` and `attestation` are stored on the agent as you
  write them; to have an agent prove its software, use [Client attestation](../../applications/client-security/attestation/)
  on its client.

## Reference

### Settings

| Where | Setting | What it does |
|---|---|---|
| Client | `agent` | The agent this client authenticates for |
| Client | `tokenEndpointAuthMethod` | `private_key_jwt` or `attest_jwt_client_auth` |
| Client | `assertionTrust` | `registeredKey`, `amBootstrap` or `spiffe`. A client that sets it authenticates by `private_key_jwt` |
| Client | `securityProfile` | `fapi2` by default for an agent's client; `oauth2.1` or `none` to hold it to less |
| `agentSettings` | `agentKeyMaxLifetimeSecs` (`7776000`, 90 days; 3600 to 31536000) | The furthest end an agent's key may state |
| `oauth2Settings` | `bootstrapAssertionLifetimeSecs` (`300`, at most 3600) | How long a bootstrap assertion stays usable |
| `agentSettings` | `maxActDepth` (`4`; 1 to 16) | How many agents may act in turn for one user in a token |
| `agentSettings` | `tokenExchangeImpersonation` (off) | Whether an agent may get a token as the user itself, where delegation and policy allow |
| `agentSettings` | `agentGuardrails` | Limits by risk class: [Agent guardrails](../../../gateway/guardrails/agent-guardrails/) |

### Errors

- **`invalid_client`:** the agent's chain doesn't stand (an agent in it isn't active or has expired, or the user at
  the top is inactive), the assertion doesn't hold, or the method isn't one of the two above. The reason is never
  returned to the client. The refusal is on the audit trail (see [Audit record](#audit-record)); to find which link
  stopped, see [Troubleshooting](#troubleshooting).
- **`unauthorized_client`:** an agent's client asks for the authorization code, device or identity assertion grant,
  or to sign a user in as a first-party app.

## Related

- [Register an agent](../../guides/agents/register-an-agent/): the task, start to finish.
- [Stop an agent](../../guides/agents/stop-an-agent/): quarantine, kill or retire.
- [Let an agent act for a user](../../guides/agents/agent-acts-for-a-person/): delegation through token
  exchange.
- [Follow what an agent did](../../guides/agents/follow-what-an-agent-did/): the agent's audit trail.
- [Agent guardrails](../../../gateway/guardrails/agent-guardrails/): token lifetimes and approvals by risk class.
