WRITE · TRACE · UNDERSTAND

Authorize a tool call outside the model

Implement tool_allowed(call,tenant,scopes,policy).

Read lesson: Authorize a tool call outside the model →

Your experiment

Implement tool_allowed(call,tenant,scopes,policy). call has tool,tenant. policy maps tool->required scope. Return True only for a known tool, matching tenant and granted required scope; missing fields fail closed.

Python runs in your browser. Your draft stays on this device.

What to look for

The executor owns authorization. This deliberately small policy does not implement a full identity system, object ACLs or human confirmation; each must be supplied by trusted application infrastructure, never inferred from model output.

Watch your code work

Ready to experiment

Write a solution or load the worked answer, then choose Trace & check.

Step through arrays, variables and training curves produced by your own Python.

About this algorithm

How Authorize a tool call outside the model works

The idea

A language model can request any tool call, including one it was talked into by text it read. The decision to run a tool therefore should not belong to the model. It belongs to ordinary code that sits between the request and the tool, and that code trusts only facts supplied by the application: who the current tenant is and which scopes the session holds.

tool_allowed(call, tenant, scopes, policy) is that check. The policy dict maps each tool name to the one scope it requires. A call passes only if the tool is in the policy, the tenant named in the call equals the session tenant, and the required scope is in scopes. Anything missing or unknown is denied.

How it works

  1. Read the tool name with call.get('tool'), so a missing field becomes None instead of raising.
  2. Check that the name is a key of policy, because a tool the policy does not list is denied.
  3. Compare call.get('tenant') with the trusted tenant argument, which rejects calls aimed at another tenant's data.
  4. Look up the scope that the tool requires and check that it is in scopes.
  5. Return True only when all three checks pass and False in every other case.

What it costs

Per call
O(number of scopes)
Memory
one small set

The check is two dictionary lookups and one set membership test, so it adds almost nothing to the cost of a tool call.

Where it is used

  • OAuth 2.0 APIs check that an access token carries the required scope before serving a request, such as repo for GitHub repository endpoints.
  • AWS IAM evaluates every API request against policies and denies it unless a statement explicitly allows the action.
  • Coding agents such as Claude Code check allow and deny rules from a settings file before running a tool the model asked for.

Watch out for

  • Trusting a tenant or scope written inside the model's call: only the tenant and scopes arguments come from the application.
  • Allowing tools that are missing from policy, as if an unlisted tool needed no scope.
  • Indexing call['tool'] directly, so a malformed call raises an exception instead of returning False.