Interview's MateAI Engineering
Interview's Mate

AI Engineering

LLMs, RAG, agents & evals

Visualizer

DSA Visualizer
All 32 lessons
Custom Playground
Try your own examples

Learning

System Design
31 architecture topics
SQL Learning
160 foundations → production topics
AI Engineering
160 LLM → backend topics
Data Science
160 Python → MLOps topics
Data Engineer
160 SQL → streaming topics
Docker
160 Docker learning topics
Kubernetes
160 K8s fundamentals → cloud
Java LearnSoon
Coming soon
Python Learn
Interview Q&A & internals
JavaScript Learn
Closures, async & DOM Q&A
Tech Learn
Search, practise & track Q&A
AI DSA Practice
Solve coding problems
Theme
Home
AI Engineering topics
Lesson 113/ 160
06. AI Security

Authorization for Agents

Map end-user identity to tool permissions so agents cannot escalate privileges.

AdvancedUser→Agent AuthZ · RBAC3 quiz questions
Practice Generative AI Q&A
In plain wordsDiagramTheoryStep by stepWhen to use itInterview cluesMistakes to avoidHands-onQuiz (3)Practice

In plain words

The AI should act with the same permissions as the person using it, so even a tricked AI can't get into things that person couldn't open.

Think of it like…

A hotel concierge who can get you into any room because they hold the master key: if a stranger asks nicely for room 412, a naive concierge opens it. Safer is a concierge who can only request that each room's own electronic lock honour your card.

Where you’ve seen it

An HR assistant lets employees ask about their pay and team. With a service account that can read every payroll record, Alice (an engineer) asks, "Show Bob's salary", and the model, nudged by a persuasive prompt, calls get_payroll(employee_id="bob"), which succeeds: a confused deputy. The redesign passes Alice's delegated token to the payroll API (the agent obtains it via OAuth token exchange with scope payroll:read:self), so the API returns 403 for Bob's record regardless of what the model asks. Managers get the scope payroll:read:reports that the API evaluates against the reporting line; sensitive operations such as exporting all salaries require step-up authentication and approval. A test matrix of 5 roles × 12 tools (60 cells) asserts the expected allow/deny result, and a month of logs (1,000 sampled tool calls) shows 7 calls (0.7%) that attempted to touch resources outside the user's scope; every one was denied by the API, and none relied on the model's refusal. The audit record for each call stores user, agent, tool, resource, decision and token scope.

Words you’ll hear

Confused deputy
A privileged component tricked into misusing its authority on behalf of a less-privileged party.
On-behalf-of / token exchange
Obtaining a token that carries the user's identity and limited scopes for a downstream call.
RBAC and ABAC
Role-based and attribute-based access control models.
Step-up authorization
Extra authentication or approval for sensitive actions.

Visual diagram

Loading interactive diagram

Theory & deep dive

Identity, delegation and where checks belong

An agent request involves several identities: the end user, the agent or application, the tool or API and, for multi-agent systems, other agents. Authorization must answer "may *this user* perform *this action* on *this resource*?", and the answer must come from a system that knows the real permissions. Two patterns exist. Service-account mode (the agent acts as itself with broad rights) is simple but makes the agent the weakest link: any manipulation yields the agent's full power. Delegated mode (the agent acts on behalf of the user) passes the user's identity, typically through OAuth 2.0 token exchange (RFC 8693) or on-behalf-of flows, so downstream services authorise against the user's actual entitlements. Prefer delegation for user-facing agents; reserve service accounts for background tasks, with their own narrow scopes and approvals.

Enforce at the resource, not in the agent: each API checks token scopes, role or attribute rules (department, region, reporting line), resource ownership (the record belongs to the caller) and context (time, device, risk score). Tokens should be short-lived and audience-restricted to the specific tool. Never let the model supply identity or tenant parameters that the server trusts (user_id must come from the session, not from the tool arguments). Model the policy explicitly (RBAC for coarse roles, ABAC or relationship-based access control for fine-grained rules) and keep it in a policy engine or the service's authorization layer.

Scopes

Alice: payroll:read:self. Manager Dana: payroll:read:reports (evaluated against the org chart). Finance admin: payroll:read:all with step-up approval and a 15-minute token. get_payroll(employee_id=bob) called with Alice's token returns 403 whatever the model says.

Key takeaway: Delegate the user's identity with minimal scopes and enforce authorization at the resource, never through the model.

Sensitive actions, multi-agent chains and testing

Classify tools by impact: read own data, read others' data, reversible writes, irreversible or financial actions. Apply step-up controls for the higher tiers: re-authentication, human approval with the exact parameters shown (see human approval), dual control for money movement and tighter rate limits. In multi-agent or agent-to-agent setups, preserve the delegation chain: a sub-agent should receive a reduced token (never more scope than its caller), and downstream services should be able to see the original user and each hop for audit. Be wary of token passthrough: an MCP server or tool that blindly forwards a broad token becomes a confused deputy itself; use audience-bound tokens and per-tool scopes.

Test authorization like any security feature: a matrix of roles and tools with expected allow or deny (5 roles × 12 tools = 60 cases), negative tests that try cross-user and cross-tenant access with realistic prompts and direct API calls, and adversarial prompts that attempt to induce forbidden calls (verifying the API denies them even if the model complies). Log every decision (user, agent, tool, resource, scopes, allow or deny, reason) and alert on spikes in denials, which indicate probing. Measure the share of denials that the model itself prevented (should be treated as bonus) versus those stopped by the API (the real control). Review scopes regularly and remove unused ones.

Authorization test matrix

5 roles (employee, manager, HR partner, finance admin, contractor) × 12 tools = 60 cells, each with an expected allow or deny. CI fails if any cell changes. In production sampling, 7 of 1,000 calls were out of scope and all 7 were denied by the API.

Key takeaway: Tier actions by risk with step-up controls, shrink scope down delegation chains and test with matrices and adversarial prompts.

How it works, step by step

Step 1 of 4
Architect

1. Choose delegated identity

Decide that user-facing agents act on behalf of the user with token exchange; reserve service accounts for scoped background jobs.

OAuth token exchange · scope payroll:read:self
Key numbers at this step
Short-lived
audience-bound tokens

When to use it & trade-offs

What it is

Authorization for agents ensures that an AI agent can do only what the end user it acts for is allowed to do. The core risk is the confused deputy: an agent running with a powerful service account will happily perform an action the user could never do directly, simply because the model was persuaded to ask. The fix is to propagate the user's identity (not the model's judgement) into every tool call using delegated tokens (OAuth 2.0 on-behalf-of or token exchange), enforce role-based and attribute-based access control and resource ownership checks in the tool or API layer, grant minimal scopes, require step-up approval for sensitive actions and keep an audit trail of the delegation chain (user, agent, tool, decision).

Why it matters: LLMs can be manipulated, and they have no built-in notion of who is allowed to see what. If authorization depends on the model refusing, an attacker who phrases a request cleverly can read other users' data or perform privileged actions. Pushing the decision into deterministic code that uses the caller's real permissions makes privilege escalation through the agent impossible by construction, and gives security and compliance teams the same access-control story they already understand for human-driven apps.

When to reach for it

  • Assistants that read or modify user or company data
  • Multi-tenant and multi-agent systems
  • Compliance and audit requirements

When to avoid or be careful

  • Letting the model choose user or tenant ids
  • Relying on the model to refuse unauthorised requests
  • Passing broad tokens through tools unchanged

Agent identity models

Delegated user identity (on-behalf-of)
Best for: User-facing assistants accessing user data

Strengths: Privilege can't exceed the user's; clear audit trail.

Watch out: Token plumbing and per-tool scope design.

Service account with broad rights
Best for: Rarely appropriate for interactive agents

Strengths: Simple to set up.

Watch out: Confused-deputy risk; blast radius is everything.

Service account with narrow scopes plus approvals
Best for: Background automation

Strengths: Predictable and auditable.

Watch out: Needs strict scoping and monitoring.

Interview signals & numbers to know

When this topic comes up

  • Confused deputy
  • “The agent can see everyone's data”
  • OAuth token exchange
  • RBAC and ABAC
  • Step-up authentication

Benchmarks & rules of thumb

5 × 12 = 60 -
role-by-tool test matrix cells
7 of 1,000 -
sampled tool calls outside the user's scope, all denied
15 minutes -
example token lifetime for a privileged action

Common mistakes & follow-ups

Common interview mistakes

  • Using one admin token for all users because it is easier.
  • Authorising in the prompt ("only answer about the current user").
  • Not logging the delegation chain.

Likely interviewer follow-ups

  • What is the confused deputy problem for agents?
  • How would you authorise an agent's calls to an internal API?
  • How do you test agent authorization?
Interview tip: Say "the model proposes, the API decides with the user's token". It is the clearest one-line architecture for agent authorization.
Loading hands-on exercise

Knowledge check

Loading quiz

Put it into practice

Practice related interview Q&A

Apply Authorization for Agents with generative AI interview Q&A and hands-on practice.

Open Generative AI Q&A →Tech Learn Q&A
Previous topic
API Key Rotation
Next topic
Human Approval for Actions