worker_scope =DelegationScope(scopes=["docs:read"],max_actions=10,expires_in_seconds=600)result =await gateway.delegate_to_agent(principal_token=orchestrator_delegation_token,# delegation JWT, not user JWTagent_id="agent:summarizer",task=task,scope=worker_scope,)worker_token = result.unwrap()print(f"Chain depth: {worker_token.chain.depth}")# 3 (user -> orchestrator -> worker)
How Chains Work
Each hop attenuates (narrows) the scope. The worker can never exceed the orchestrator's permissions, and the orchestrator can never exceed the user's permissions.
Chain Properties
Property
Description
chain.depth
Number of principals in the chain (e.g., 3 for user → orchestrator → worker)
chain.root_principal
First link — always the originating user or API key
chain.leaf_principal
Last link — the current agent holding the token
chain.links
Ordered list of DelegationLink objects
chain.effective_scopes()
Intersection of all scopes across the chain
chain.task_id
Task ID shared across the entire chain
chain.max_depth
Maximum allowed chain depth (default: 5)
Step-by-Step
1
Register Orchestrator and Worker
2
User Delegates to Orchestrator
3
Orchestrator Sub-Delegates to Worker
The orchestrator uses its delegation token (not a user token) to sub-delegate:
4
Inspect the Chain
Walk the delegation chain to see all principals:
5
Validate on Worker Service
A separate service receiving the worker's token can validate and inspect the full chain:
You've built a multi-hop delegation chain! The worker agent operates with the intersection of all scopes in the chain.
Cross-Service Delegation
In production, delegation often spans multiple services. The pattern is simple: share the secret_key.
Service B needs only for_agent_auth() — no user database, no session store, just the agent provider and the shared secret.
Depth Limits
Chains have two depth controls:
Control
Set On
Description
max_delegation_depth
AgentRegistration
Per-agent limit (default: 3). The agent cannot appear in a chain deeper than this.
max_depth
DelegationChain
Per-chain limit (default: 5). The entire chain cannot exceed this depth.
When either limit is exceeded, delegation fails with a DELEGATION_DEPTH_EXCEEDED error.
Complete Example
Create delegation_chain.py:
Run it:
Expected output:
Common Pitfalls
Pitfall
Solution
Depth exceeded
Increase max_delegation_depth on the agent or max_depth on the chain
Task ID mismatch
All hops in a chain must use the same TaskContext.id
Scope escalation in chain
Each sub-delegation must use a subset of the parent's scopes
Using user token for sub-delegation
Orchestrator must use its delegation token, not the original user token
Next Steps
Scope & Budget — Deep dive into scope attenuation and action budgets