Skip to main content

Ask before acting

Before each action, the agent asks LumoAuth whether it may do it, and receives the answer with the reason.

RecipeReadyView source
Works with
JavaScript / TypeScriptPythonGoJavaC# / .NETPlain HTTP

What it shows​

  1. An agent, "Billing assistant", holds one capability: invoices:read.
  2. Before it reads an invoice, the agent asks LumoAuth. The answer is yes.
  3. Before it refunds the same invoice, it asks again. The answer is no.
  4. Each answer carries a reason and the name of the check that decided: capability, pdp or deny_override.
  5. On a no, the agent stops and can show the reason to the person.

The agent sends the action and a context: the invoice and the amount. Your attribute rules can read that context.

Before you start​

  • An agent in the registry with the capability invoices:read, and its client ID and secret. The setup script registers the agent and prints both values.
  • The setup script needs an API key with the scopes admin:agents:read and admin:agents:write. Create it in the portal under Applications & developer tools, API keys.
VariableValue
LUMOAUTH_URLThe address of LumoAuth: https://app.lumoauth.dev, or https://eu.app.lumoauth.dev for the EU region.
LUMOAUTH_ORG_IDYour organization, for example acme-corp.
LUMOAUTH_API_KEYFor the setup script only: an API key with the scopes admin:agents:read and admin:agents:write.
AGENT_CLIENT_IDThe client ID of the agent. The setup script prints it.
AGENT_CLIENT_SECRETThe client secret of the agent. The setup script prints it once.

The setup script creates what the recipe needs, through the Admin API. You can run it again at any time.

node agents/ask-before-acting/setup.mjs
setup.mjs
// Registers the agent of the recipe, "Billing assistant", with one capability,
// and prints its client ID and secret. Run it again at any time.
//
// node agents/ask-before-acting/setup.mjs
//
// The API key needs the scopes admin:agents:read and admin:agents:write.
import { call, list, printEnv } from '../../_lib/admin.mjs';

const name = 'Billing assistant';
const capabilities = ['invoices:read'];

let agent = (await list('/admin/agents')).find((a) => a.name === name);
let secret;
if (agent) {
// LumoAuth shows a secret once. A second run therefore makes a new secret,
// and the old one stops working.
await call('PUT', `/admin/agents/${agent.agentId}/scopes`, { scopes: capabilities });
secret = (await call('POST', `/admin/agents/${agent.agentId}/rotate-credentials`)).data.clientSecret;
} else {
agent = (
await call('POST', '/admin/agents', {
name,
description: 'Reads invoices. It may not refund them.',
identityType: 'client_secret',
capabilities,
})
).data;
secret = agent.clientSecret;
}

console.log(`Agent "${name}" (${agent.agentId}) holds: ${capabilities.join(', ')}`);
printEnv({ AGENT_CLIENT_ID: agent.agentId, AGENT_CLIENT_SECRET: secret });

Code​

Run each command from the recipes folder of the examples repository. Set the folder up once for your language. Every version below was run against a LumoAuth organization. The curl version prints the two answers as JSON.

agents/ask-before-acting/js/ask.mjs
// Before each action, the agent asks LumoAuth whether it may do it.
//
// node agents/ask-before-acting/js/ask.mjs
import { LumoAgent } from '@lumoauth/agent';

// Reads LUMOAUTH_URL, LUMOAUTH_ORG_ID, AGENT_CLIENT_ID and AGENT_CLIENT_SECRET.
const agent = new LumoAgent();

const invoice = { invoice: 'INV-7741', amount: 120 };

for (const action of ['invoices:read', 'invoices:refund']) {
// The context describes this one action. LumoAuth passes it to your rules.
const answer = await agent.ask(action, invoice);
console.log(`${action}: ${answer.allowed ? 'allowed' : 'denied'}, decided by ${answer.decided_by}`);
console.log(` reason: ${answer.reason}`);
if (!answer.allowed) {
// The agent stops here. It can show the reason to the person.
console.log(` audit ID: ${answer.audit_id}`);
}
}
node agents/ask-before-acting/js/ask.mjs

Check the result​

Output
invoices:read: allowed, decided by capability
reason: Agent has authorized capability for 'invoices:read'.
invoices:refund: denied, decided by capability
reason: Agent lacks required capability for 'invoices:refund'.
audit ID: 2a36ff5b41052fed
  • Open Audit logs in the portal and filter by the event type agent.capability_check_denied. The refusal is there, with the agent as the actor.
  • Add invoices:refund to the agent's capabilities and run the program again. Both answers are now "allowed".
  • The Test this agent section on the agent's page in the portal makes the same check.

Limits​

  • LumoAuth writes a refusal to the audit log. It does not write an allowed answer, because agents ask at every step.
  • An agent writes the action with :. An action with a ., such as invoices.refund, gets 400 invalid_action.
  • Each question counts against the budget of the agent, when it has one.
  • The number of questions for each month depends on the plan. Check that your plan includes the Ask API in the plans matrix.
  • For a check without a network call, see Check capabilities without a network call.