Guides
Give an AI Agent Scoped Access
Provision a managed agent, bind it to credentials, and let it call providers — with its own identity and audit trail.
By the end of this guide, an AI agent has its own API key, can call third-party APIs through Alter, and reaches only the credentials explicitly bound to it — not the app’s full grant store.
The flow:
- The operator provisions a managed agent.
- The operator binds credentials to the agent (a managed-secret grant directly, or a user delegates an OAuth connection to the agent).
- The agent process loads its own API key and calls
agent.proxy_request()for provider execution.
Prerequisites
Section titled “Prerequisites”- An app with a runtime API key (
alter_rk_…). The operator calls in this guide need these scopes on that key:agents:writeto create an agent,agents:adminto suspend or resume one, andconnect:initiateplusgrants:writefor the Connect session in Path A. The portal’s Backend service key template does not include the agent scopes; add them when you create the key. An app key in the olderalter_key_…format was created before the September 2026 key-security upgrade and no longer authenticates — mint a new one. - For OAuth delegation flows: at least one provider configured and an identity provider wired for the app.
- For managed-secret flows: at least one managed-secret provider configured.
Walkthrough
Section titled “Walkthrough”1. Provision the agent
Section titled “1. Provision the agent”In the developer portal: Agent Identity → Create agent. Pick a name (research-bot) and an explicit capability allowlist, then click Create.
The allowlist must cover everything a delegation would hand the agent: for an OAuth provider, every scope the connection holds — which is every scope the application requests from that provider, unless the delegation is narrowed with a scope_constraint — and for a managed secret, the secret’s slug under managed_secret_slugs. A delegation the allowlist cannot use is refused when it is created (agent_allowlist_exceeded), never left in place for the agent’s calls to fail. An OAuth Connect session for the agent is checked before the user is sent to the provider: a provider the session names (in allowed_providers, or as a required_scopes key) that the agent cannot use refuses the session; when the session omits allowed_providers, it drops the other providers the agent cannot use, names each in the session’s scope_constraint_warnings (scopeConstraintWarnings in TypeScript) with an agent_allowlist: prefix, lists each in dropped_providers (droppedProviders), and is refused only when none remain. While a narrowing of the agent’s allowlist is pending, OAuth delegations are checked against the narrowed allowlist, and the refusal names when the narrowing applies. The refusal applies to every delegation to the agent, even though at request time the allowlist is checked only for calls made with the agent’s own key: a call made with an application key, including through an Agent from app.get_agent() / app.getAgent(), is not checked against it. When you widen the scopes an application requests from a provider, widen the allowlists of the agents that use it too.
The form also takes Tags: key/value labels for filtering the agent list, grants and audit logs. There are at most 50 per agent. A key is 1 to 128 characters and a value 0 to 256, using letters, digits, spaces and _ . / + - @ (a value may also use : and =). Tags never change what an agent may do. Editing an agent’s tags replaces the whole set. A delegation starts with its parent grant’s tags and shows them, with its label and its rule count, in the grant drawer on the user’s page; rules a delegating agent attached read as agent-authored there.
Equivalent SDK call:
The examples use a managed-secret provider whose slug is tavily-production and a
Google connection authorized for read-only Calendar access. The operator
snippets in steps 1 and 2 are fragments: they run inside an async function
that holds an App client (async with App(api_key=os.environ["ALTER_API_KEY"]) as app:
in Python, const app = new App({ apiKey: process.env.ALTER_API_KEY! }) in
TypeScript). See Running the examples in the
Python and
TypeScript SDK references.
import os
result = await app.agents.create( name="research-bot", scopes={ "google": [ "openid", "email", "https://www.googleapis.com/auth/calendar.readonly", ], "managed_secret_slugs": ["tavily-production"], },)print("agent id:", result.id)
# result.api_key is the plaintext, returned exactly ONCE — it is None on an# idempotent replay, so read it back from wherever the first call stored it.# Hand it straight to the secret store the agent process reads from — do not# print it. Anything written to stdout survives in terminal scrollback, shell# history and CI logs, which is where leaked credentials are usually found.if result.api_key is None: raise RuntimeError("agent already exists — use the key stored at creation")os.environ["AGENT_API_KEY"] = result.api_keyconst result = await app.agents.create({ name: "research-bot", scopes: { google: [ "openid", "email", "https://www.googleapis.com/auth/calendar.readonly", ], managed_secret_slugs: ["tavily-production"], },});console.log("agent id:", result.id);
// result.apiKey is the plaintext, returned exactly ONCE — it is null on an// idempotent replay, so read it back from wherever the first call stored it.// Hand it straight to the secret store the agent process reads from — do not// log it. Anything written to stdout survives in terminal scrollback, shell// history and CI logs, which is where leaked credentials are usually found.if (result.apiKey === null) { throw new Error("agent already exists — use the key stored at creation");}process.env.AGENT_API_KEY = result.apiKey;The plaintext API key is returned exactly once — agents.create is a key-minting call, not only a create call. Store it where the agent process will read it (a secret manager, an environment variable, the runtime’s secrets API), and never print or log it. Per-agent keys currently mint with the legacy alter_key_… prefix; a scoped alter_ak_… key from App → API Keys is agent-typed but is bound to no managed-agent record, so it cannot stand in for this one.
Using the CLI instead, redirect the output rather than letting it print:
alter agents create --name research-bot --scopes '<allowlist-json>' \ --output json > .alter-agent-key.json # add to .gitignore FIRSTThe later snippets refer to the new agent’s ID as agent_id (agentId in TypeScript): that is result.id from the call above, or the id field of the CLI’s JSON output.
When you need to freeze the workload without deleting its identity, suspend the agent instead of revoking it:
await app.agents.suspend(agent_id)await app.agents.resume(agent_id)await app.agents.suspend(agentId);await app.agents.resume(agentId);2. Bind credentials to the agent
Section titled “2. Bind credentials to the agent”Two paths, depending on whether the credential belongs to a user or the operator.
Path A — user delegates a connection. When the user runs Connect, pass agent=<agent_id>:
session = await app.create_connect_session( allowed_providers=["google"], user_token=user.jwt, agent=agent_id, # consent screen will show "research-bot is requesting access")const session = await app.createConnectSession({ allowedProviders: ["google"], userToken: user.jwt, agent: agentId,});On Approve, Alter creates or reuses the requested grant and creates a delegation binding that grant to the named agent. If the user is not connected yet, the OAuth flow creates the connection first.
Two optional knobs on that same call shape the delegated grant:
delegable=True(delegable: truein TypeScript) asks the user to let the recipient agent later re-delegate the grant onward to another agent — see Delegation chains below. It is a request: the consent screen shows the user a May share this access with other agents checkbox, and only the user’s choice is stored. The user signs in through the application’s identity provider before choosing (the screen after a provider login shows Sign in to choose first; embedded Connect shows that screen in the window where the user signed in to the provider, so the choice is offered there too). A screen shown inside the embedded Connect frame, such as the approval step for an account the user already connected, or an identity provider that cannot sign users in, offers no checkbox: a new delegation is created as a leaf and an existing one keeps its setting. The checkbox appears only when the provider’s Agent sharing setting is on (Developer Portal, the provider’s page under OAuth, oralter providers set-delegation-policy --delegable), except that an existing delegation that can already be shared onward keeps it, turned on, after the setting is turned off, and the user can then only turn it off; a provider configured after agent sharing was introduced starts with the setting off. Settingdelegable=Truealso shows the consent screen for adirectsession. The stored outcome comes back on each result’sdelegation(agent_id,delegable, andpolicy_refusedwhen the provider’s setting refused the user’s choice as the connection was confirmed). A new delegation is a leaf whendelegableis omitted: onward delegation is opt-in at every hop. Omitting it when the user re-consents for an agent that already holds a delegation keeps that delegation’s existing setting;delegable=Falseturns it off, with no checkbox.scope_constraint=[...](scopeConstraintin TypeScript) clamps the agent’s grant to a subset of the credential’s provider scopes (least privilege). A scope-narrowed grant is proxy-only, and if the constraint admits nothing against a provider’s required scopes, the returned session’sscope_constraint_warningssays so.
Path B — operator issues a managed-secret grant to the agent. From the Developer Portal: Managed Secrets → [Provider] → Add Grant → Agent → research-bot (the Alter CLI’s managed-secrets grant commands support agent principals too). Agent-bound grants are an operator action: the SDK’s create_managed_secret_grant / createManagedSecretGrant methods accept an agent principal at the type level, but the backend rejects it (HTTP 422) — use the Portal or CLI. The resulting grant_id is what the agent process uses in request(grant_id=...).
3. Call providers from the agent
Section titled “3. Call providers from the agent”The agent process loads its own key and calls request() exactly as an app would. The example reads three environment variables: AGENT_API_KEY (the key from step 1), TAVILY_GRANT_ID (the grant_id of the agent-bound managed-secret grant from Path B), and, for the delegated call, USER_JWT (the end user’s IDP-issued JWT; in a running service it comes from the incoming request rather than the environment):
from alter_sdk import PendingApprovalimport asyncio, osfrom alter_sdk import Agent, HttpMethod
async def main(): async with Agent(api_key=os.environ["AGENT_API_KEY"]) as agent: # Use a managed-secret grant. The json body is forwarded to the provider # verbatim — use the provider's wire field names. response = await agent.proxy_request( HttpMethod.POST, "https://api.tavily.com/search", grant_id=os.environ["TAVILY_GRANT_ID"], json={ "query": "Latest developments in retrieval-augmented generation", "max_results": 5, }, ) if isinstance(response, PendingApproval): for gate in response.gates: print("Approval required:", gate.approval_url) response = await agent.await_approval(response.approval_id) print(response.status_code, response.body_json())
# Or use a delegated OAuth grant via provider+user_token, where # user_jwt is the end user's IDP-issued JWT. user_jwt = os.environ["USER_JWT"] response = await agent.proxy_request( HttpMethod.GET, "https://www.googleapis.com/calendar/v3/calendars/primary/events", provider="google", user_token=user_jwt, ) print(response.status_code)
asyncio.run(main())import { Agent, HttpMethod } from "@alter-ai/alter-sdk";
const agent = new Agent({ apiKey: process.env.AGENT_API_KEY! });
try { // The json body is forwarded to the provider verbatim — use the provider's // wire field names. let response = await agent.proxyRequest({ method: HttpMethod.POST, url: "https://api.tavily.com/search", grantId: process.env.TAVILY_GRANT_ID!, json: { query: "Latest developments in retrieval-augmented generation", max_results: 5, }, }); if ("status" in response) { for (const gate of response.gates) { console.log("Approval required:", gate.approvalUrl); } response = await agent.awaitApproval(response.approvalId); } console.log(response.statusCode, await response.bodyJson());} finally { await agent.close();}The audit events carry the agent identity, the user identity (if a delegation was used), and the provider.
Patterns
Section titled “Patterns”Listing the agent’s bound grants
Section titled “Listing the agent’s bound grants”page = await agent.list_grants()for g in page.grants: print(g.grant_kind, g.provider_id if g.grant_kind == "oauth" else g.managed_secret_slug)OAuth and managed-secret grants come back in one merged response, discriminated by grant_kind.
Delegation chains and scope narrowing
Section titled “Delegation chains and scope narrowing”When the user allowed onward sharing for the first-hop grant, the holding agent can hand a narrowed child to another agent — without the credential owner re-consenting. The credential’s operator ceiling (the OAuth provider’s Agent sharing setting, or a managed-secret provider’s delegation policy) must still allow it on every call, and its depth bounds how far the chain can go:
result = await agent.delegate( grant_id, # a grant THIS agent holds downstream_agent_id, scope_constraint=["chat:write"], # subset of the parent's provider scopes ttl_seconds=3600, # child expiry ≤ min(this, parent's expiry))print(result.grant_id, result.depth) # depth 2+ = an onward hopconst result = await agent.delegate(grantId, downstreamAgentId, { scopeConstraint: ["chat:write"], ttlSeconds: 3600,});console.log(result.grantId, result.depth);A child can preserve or narrow the parent’s scope ceiling and can never outlive its parent; it cannot widen either boundary. Each new hop is itself non-delegable unless delegable is set again; re-delegating to an agent that already holds a child of the grant without delegable keeps that child’s setting. Delegating a grant that was not minted delegable, or one whose operator ceiling is now off, raises GrantNotDelegableError. Revoking any grant cascades down its whole chain.
The recipient agent’s capability allowlist only needs to cover the narrowed scope set, not the credential’s full scope list — a child narrowed to read:user runs on an agent allowlisted for exactly ["read:user"] (least privilege end to end). An allowlist entry that subsumes the narrowed scope (for example GitHub’s user, which grants read:user) also qualifies.
Requests through a narrowed grant are checked per operation against the narrowed set. An operation whose provider-scope requirement cannot be determined is refused (scope_narrowing_unenforceable) rather than let through — unless a restriction policy rule on the delegated grant (or its holding agent) explicitly pins the allowed methods and endpoints, in which case that pin bounds the traffic. Rules attached at the application or organization level do not lift this refusal: only a pin authored on the delegation itself does.
A launcher that creates run agents and delegates to them
Section titled “A launcher that creates run agents and delegates to them”A common shape is a backend service (a launcher) that creates a short-lived agent for each run and hands it delegations from a longer-lived agent that already holds the grants. The launcher’s key needs two scopes:
agents:write, to create the run agent (app.agents.create(...)/alter agents create). Only an app key can create an agent.grants:write, to delegate.delegate()must be called by the agent that holds the grant: either with that agent’s own key, or with an app key acting as the holding agent:app.get_agent(holder_agent_id).delegate(...)in Python,app.getAgent(holderAgentId).delegate(...)in TypeScript, whereappis theAppbuilt from the launcher’s key. A key withagents:writealone cannot delegate, and an app key that is not acting as an agent is refused as well.
Delegate with rules and tags
Section titled “Delegate with rules and tags”A delegation can carry its own rules, so an orchestrator hands a worker exactly what one run needs. The rules are created with the delegation, in one step: the recipient never holds the grant without them.
from alter_sdk import operation_allowlist_rule
child = await agent.delegate( grant_id, worker_agent_id, ttl_seconds=3600, label="run-42", tags={"run": "42"}, # added to the tags the child inherits rules=[ operation_allowlist_rule(families=["read"]), {"rule_type": "quota", "rule_body": {"limit": 100, "period": "day"}}, ],)print(child.grant_id, child.label, child.tags, [r.rule_id for r in child.rules])print(child.approver_warnings)import { operationAllowlistRule } from "@alter-ai/alter-sdk";
const child = await agent.delegate(grantId, workerAgentId, { ttlSeconds: 3600, label: "run-42", tags: { run: "42" }, // added to the tags the child inherits rules: [ operationAllowlistRule({ families: ["read"] }), { ruleType: "quota", ruleBody: { limit: 100, period: "day" } }, ],});console.log(child.grantId, child.label, child.tags, child.rules);console.log(child.approverWarnings);From a script, alter grants delegate does the same with --rules-file and --tag.
Rules can only narrow. An agent may author json_match, require_approval, time_window, quota, content_match, operation_allowlist, request_allowlist, ip_allowlist and restriction. Every rule joins the chain and applies at runtime, and a request must satisfy all of them, so no rule can add access. The delegation is refused, and nothing is delegated, in these cases:
| Code | Meaning |
|---|---|
delegation_rule_widens (422) | An operation_allowlist admits something an allowlist already on the chain does not, a quota is above a chain quota of the same period, or a max_pending is above a chain value. This is an authoring check for those three types; every rule still applies at runtime. Also a require_approval rule naming an approver that no require_approval rule on the chain names (reason approver_outside_chain). |
delegation_rule_type_not_allowed (422) | An agent may not author that rule type, such as credential_minting. |
delegation_rule_invalid (422) | The rule body is not valid for its type (including a rule that names an operation Alter has not reviewed by its bare id; the message lists the provider-qualified <provider>:<operation> names to use), or the agent would hold more than 20 of its own rules on the child (reason agent_rule_ceiling_reached), or the child would pass 100 rules. |
delegation_tag_not_writable (422) | A tag the child already carries with another value. |
delegation_label_taken (409) | Another agent’s delegation to the same recipient, for the same provider or managed secret, already carries the label, in any status. |
invalid_tags (422) | A tag does not fit the grammar. |
service_unavailable (503) | The Alter instance that received the call already had too many delegations with rules in progress, from this application or in all, or no rule check freed up within a second; ordinary bursts of a few delegations wait their turn instead. Transient: wait the Retry-After interval (1 second) and delegate again. |
Delegating to the same agent again updates the existing child: rules already on it are kept rather than duplicated, and a rule of the agent’s that an operator turned off stays off.
Approvers come from the chain. A require_approval rule an agent attaches may only name approvers that a require_approval rule already applying to the chain names, so a delegating agent can never send an approval request to someone the operator or end user did not choose, and never learns anything about the directory from the answer. Any other approver refuses the delegation (delegation_rule_widens, reason approver_outside_chain); the refusal does not repeat the address. Each rule naming approvers adds an entry to approver_warnings on the result (approverWarnings in TypeScript), and the CLI prints it on stderr.
An agent’s rules do not lift a scope pin. An operation whose provider-scope requirement cannot be determined is refused on a narrowed grant (scope_narrowing_unenforceable) unless a restriction rule on the chain pins its methods and endpoints. A restriction an agent attached through delegate() never counts, even though it still applies: a party under a ceiling cannot author what waives it.
Tags are a validated key/value map on agents and grants: at most 50 keys, a key of 1-128 characters using letters, digits, space and _ . / + - @, and a value of 0-256 characters that may also use : and =. A child starts from its parent’s tags, and a delegation can only add keys: the same value again is a no-op, and a different value for a key the child carries is refused with delegation_tag_not_writable, so an agent never changes or drops a tag an operator set. Operators change tags with update_grant_tags(). Tags select and filter, on list_grants(), agents.list(), and the audit log, where a row matches through the grant or agent it names. They never affect what a credential may do.
A customer tags a connection when it is created, with tags on create_connect_session() and create_managed_secret_connect_session() (or alter connect create --tag), and afterwards with app.update_grant_tags() (or alter grants tag), which replaces the grant’s whole map. A delegation inherits the tags its parent has when it is created; a Connect session’s or delegate()’s tags only add keys and can never change a tag the parent passes down. A later change to the parent does not reach existing delegations, and a grant a session reuses is unchanged.
Selecting a delegation. An agent holding several delegations of one provider selects one by the label it was given, or by account, on proxy_request() and list_grants(). A delegated grant is proxy-only, so request() never reaches one. A real grant label always wins: while the agent holds its own grant with that label, in any status, the name selects that grant and never a delegation, so a revoked grant stays a not-found rather than quietly becoming someone else’s credential. An account no delegation matches is a not-found too, never a different account.
await worker.proxy_request("GET", url, provider="google", label="run-42")await worker.proxy_request("GET", url, provider="google", account="work@example.com")await worker.proxyRequest({ method: "GET", url, provider: "google", label: "run-42",});await worker.proxyRequest({ method: "GET", url, provider: "google", account: "work@example.com",});Restoring a revoked delegation
Section titled “Restoring a revoked delegation”A delegation that was revoked while the user’s labelled grant stayed is restored by connecting the same agent again with the same requested_grant label: the new delegation goes under the same grant, so the grant id and the rules bound to it carry over, and the audit trail records delegation.restored. A grant last delegated to a different agent refuses the label (sibling_label_conflict). A grant whose time limit has passed is not restored; a fresh grant is created instead. For an approval requirement that must hold whatever grant the agent is given, bind the require_approval rule to the agent, not to the grant. See Restoring a revoked delegation.
Self-introspection
Section titled “Self-introspection”me = await agent.me()print(me.name, me.status, me.scopes)me() works even if the agent is paused — useful for self-diagnostics. Every other call from a paused agent’s key — request(), proxy_request(), list_grants(), delegate(), Connect session minting, key derivation, catalog reads — raises AgentInactiveError: suspension is containment, so a paused agent cannot enumerate its grants or acquire new access, whichever resolution mode the call uses. Two deliberate carve-outs besides me(): span emission and the SDK’s automatic provider-call audit reporting keep working while paused, so suspending an agent never blinds the audit trail it is being investigated with.
Trace-scoped audit identity
Section titled “Trace-scoped audit identity”For multi-step workflows where each step should be attributed to a sub-agent:
async with researcher.trace(run_id="run_abc", role="research"): # Every nested request() is tagged with researcher + run_abc. await researcher.proxy_request(...)See Agent.trace() in the Python SDK reference.
Choosing the agent process boundary
Section titled “Choosing the agent process boundary”Two shapes of agent process:
- Standalone — the agent runs in its own process / container / sandbox with only its agent key. Best for untrusted runtimes (user-installed MCP servers, sandboxed code).
- Embedded — the agent runs inside the main app process; the app holds the agent key alongside its app key. Best for backends where adding another key boundary buys nothing.
Both shapes produce the same backend identity. The decision is purely a blast-radius question.
Troubleshooting
Section titled “Troubleshooting”| Error | Likely cause | Fix |
|---|---|---|
NoDelegatedGrantError | The agent has no delegation for the requested provider. | Run a Connect session with agent=<this_agent_id> so a user can delegate, have the agent ask the user for a managed-secret grant with agent.create_managed_secret_connect_session() / agent.createManagedSecretConnectSession(), or issue a managed-secret grant to the agent. |
GrantNotDelegableError from delegate() | detail_reason names the cause: holder_not_delegable (the user did not allow onward sharing for the held grant), ceiling_off (the credential’s operator ceiling is off: the OAuth provider’s Agent sharing setting, or the managed-secret provider’s delegation policy), no_consenting_user (the held grant was made directly to an agent, with no consenting user), or no_onward_hop (delegable=True asked for a child the depth ceiling leaves no further hop). | holder_not_delegable: ask the user again through a Connect session with delegable=True. ceiling_off: an administrator turns sharing on for the provider (alter providers set-delegation-policy --delegable) or the managed-secret provider (alter managed-secrets set-delegation-policy <provider> --delegable). no_consenting_user: such a grant can never be shared onward; have a user delegate it. no_onward_hop: call again without delegable=True, or an administrator raises the depth ceiling. |
AgentInactiveError | The operator paused the agent. | Resume from the portal. |
AgentRevokedError | The operator hard-revoked the agent. | Provision a new agent. |
KeyRevokedError | The specific API key was revoked. | Mint a fresh key for the agent. |
InsufficientScopeError | The API key’s Alter scope set does not cover the SDK route. | Use a key whose Alter scopes include the missing capability. |
ConnectConfigError (or BackendError from delegate()) with agent_allowlist_exceeded | The agent’s capability allowlist cannot use what the delegation would hand it; nothing was delegated. Where the check is reported depends on the call. Session creation (create_connect_session() / createConnectSession(), and connect() before the browser opens) and delegate(): details["details"]["detail_reason"] (Python) / details.details.detail_reason (TypeScript) is provider_not_allowlisted (the allowlist has no entry for the provider), scopes_not_allowlisted (the connection holds scopes the allowlist does not name) or grant_scopes_malformed (the connection’s scopes cannot be evaluated); delegate() can also report ceiling_empty (the scope_constraint leaves the agent none of the connection’s scopes — at session creation that is invalid_scope_constraint), and a managed-secret delegate() reports managed_secret_not_allowlisted. Polling the session (poll_connect_session() / pollConnectSession(), and connect() once the session exists): details["error_code"] / details.error_code, with the same details["details"] / details.details (detail_reason, provider_id, uncovered_providers). The missing scopes are never in the error; alter audit explain <trace-id> prints them from the refusal’s audit record. | --scopes replaces the whole allowlist, so read it first (alter agents show --agent <agent-id> --output json), add the missing scopes to the full scopes object, and pass all of it to alter agents update --agent <agent-id> --scopes '<full json>' --expected-version <version>, or widen it in the portal. Alternatively narrow the scopes the application requests from the provider, or pass a scope_constraint the allowlist covers. Then start a new session. |
PolicyViolationError with policy_error == "agent_scope_not_allowed" | The agent’s provider or managed-secret allowlist does not cover the resolved grant’s effective scopes (for a scope-narrowed or delegated grant, the narrowed set — not the credential’s full list). | Update the agent’s capability allowlist in the portal or through app.agents.update(). |