Read what an agent did
Query the audit log for one agent, read why an action was refused, and export the rows as JSON, from the terminal.
What it shows
- The setup script registers an agent and lets it act: it gets a token, asks for one capability that it holds and one that it does not hold.
- The script lists the rows in which the agent is the actor, from a given time on.
- It reads the refused action in full. The action and the reason are in the context of the row.
- It finds the row that registered the agent. When an API key made the change, the row names the person who created that key.
- It exports the rows of the agent as a JSON file.
Before you start
- An API key with the scopes
admin:audit:readandadmin:users:read. Create it in the portal under Applications & developer tools, API keys. The person who creates the key needs the permissionsaudit.viewandaudit.export. - Check that your plan includes the Admin API and audit log export in the plans matrix.
- The lumo CLI for the first version, curl for the second, and
jqfor both.
| Variable | Value |
|---|---|
LUMOAUTH_URL | The address of LumoAuth: https://app.lumoauth.dev, or https://eu.app.lumoauth.dev for the EU region. |
LUMOAUTH_ORG_ID | Your organization, for example acme-corp. |
LUMOAUTH_API_KEY | An API key with the scopes admin:audit:read and admin:users:read. Its creator needs the permissions audit.view and audit.export. |
AGENT_ID | The ID of the agent whose rows you read. The setup script prints it. |
SINCE | The time from which the rows are read, in ISO 8601. The setup script prints the time before the agent acted. |
AUDIT_FILE | Optional. The file for the export. Default: expense-report-agent-audit.json in the current folder. |
The setup script registers the agent and lets it act: it gets a token, asks for expenses:read, which it holds, and asks for expenses:approve, which it does not hold. Its API key also needs admin:agents:read and admin:agents:write.
node agents/audit-trail/setup.mjs
setup.mjs
// Registers the agent of this recipe, "Expense report agent", and lets it act,
// so that the audit log has rows to read. The agent gets a token, asks for a
// capability that it holds, and asks for one that it does not hold. Run it
// again at any time.
//
// node agents/audit-trail/setup.mjs
//
// The API key needs the scopes admin:agents:read and admin:agents:write.
import { LumoAgent } from '@lumoauth/agent';
import { call, ensure, printEnv } from '../../_lib/admin.mjs';
// The recipe reads the rows from this moment on.
const since = new Date(Date.now() - 1000).toISOString();
const agent = await ensure('/admin/agents', (a) => a.name === 'Expense report agent', {
name: 'Expense report agent',
description: 'Reads expense reports and prepares them for approval. It may not approve them.',
identityType: 'client_secret',
capabilities: ['expenses:read'],
});
// LumoAuth shows a secret once, in the answer that creates it. On a second
// run the script therefore asks for a new secret, and the old one stops.
const secret =
agent.clientSecret ??
(await call('POST', `/admin/agents/${agent.agentId}/rotate-credentials`)).data.clientSecret;
// The agent acts with its own credentials. LumoAuth writes a row for the
// token and for the refusal. An allowed question writes no row.
const expenseReportAgent = new LumoAgent({ clientId: agent.agentId, clientSecret: secret });
for (const action of ['expenses:read', 'expenses:approve']) {
const answer = await expenseReportAgent.ask(action, { report: 'EXP-2291', amount: 640 });
console.log(`The agent asked for ${action}: ${answer.allowed ? 'allowed' : 'denied'}`);
}
printEnv({ AGENT_ID: agent.agentId, SINCE: since });
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.
- lumo CLI
- curl
agents/audit-trail/cli/trail.sh
#!/bin/sh
# Read what one agent did, with the lumo CLI: list its rows of the audit log,
# read the refused action, find the person behind its registration, and
# export the rows as JSON.
#
# sh agents/audit-trail/cli/trail.sh
#
# The script needs the lumo CLI and jq.
set -eu
# The CLI has its own names for the three values of the recipes.
export LUMO_BASE_URL="$LUMOAUTH_URL" LUMO_ORG="$LUMOAUTH_ORG_ID" LUMO_API_KEY="$LUMOAUTH_API_KEY"
FILE="${AUDIT_FILE:-expense-report-agent-audit.json}"
# 1. The rows in which the agent is the actor, from SINCE on, oldest first.
ROWS=$(lumo logs list --user "$AGENT_ID" --from "$SINCE" --limit 100 -o json)
echo "what the agent did:"
printf '%s\n' "$ROWS" | jq -r '.data | sort_by(.id)[] | " \(.eventType) \(.status)"'
# 2. A refusal names the action and the reason in its context. Only the
# single row has the context.
REFUSED=$(printf '%s\n' "$ROWS" | jq -r '[.data[] | select(.eventType == "agent.capability_check_denied") | .id][0] // empty')
if [ -n "$REFUSED" ]; then
lumo logs get "$REFUSED" -o json |
jq -r '.data.context | "refused: \(.requested_action), reason \(.reason), decided by \(.decided_by)"'
fi
# 3. The row that registered the agent names the actor. For an API key, the
# context names the person who created the key.
CREATED=$(lumo api GET /audit-logs --query targetId="$AGENT_ID" --query eventType=agent.created -o json | jq -r '.data[0].id // empty')
if [ -n "$CREATED" ]; then
ENTRY=$(lumo logs get "$CREATED" -o json)
PERSON=$(printf '%s\n' "$ENTRY" | jq -r '.data.context.actor_owner_id // .data.actorId')
EMAIL=$(lumo users get "$PERSON" -o json | jq -r '.data.email')
printf '%s\n' "$ENTRY" | jq -r --arg email "$EMAIL" '.data | "registered by: \(.actorType) \"\(.actorName)\", person \($email)"'
fi
# 4. The same rows as a file, with the context of each row. The export takes
# the filters of the list.
lumo api GET /audit-logs/export --query format=json --query actorId="$AGENT_ID" --query from="$SINCE" -o json > "$FILE"
echo "exported: $(jq '.data | length' "$FILE") rows to $FILE"
sh agents/audit-trail/cli/trail.sh
agents/audit-trail/http/trail.sh
#!/bin/sh
# Read what one agent did, with curl: list its rows of the audit log, read
# the refused action, find the person behind its registration, and export
# the rows as JSON.
#
# sh agents/audit-trail/http/trail.sh
#
# The script needs curl and jq.
set -eu
ADMIN="$LUMOAUTH_URL/orgs/$LUMOAUTH_ORG_ID/api/v1/admin"
FILE="${AUDIT_FILE:-expense-report-agent-audit.json}"
get() { curl --silent --show-error --fail-with-body --get --header "X-API-Key: $LUMOAUTH_API_KEY" "$@"; }
# 1. The rows in which the agent is the actor, from SINCE on, oldest first.
ROWS=$(get "$ADMIN/audit-logs" --data-urlencode "actorId=$AGENT_ID" --data-urlencode "startDate=$SINCE" --data limit=100)
echo "what the agent did:"
printf '%s\n' "$ROWS" | jq -r '.data | sort_by(.id)[] | " \(.eventType) \(.status)"'
# 2. A refusal names the action and the reason in its context. Only the
# single row has the context.
REFUSED=$(printf '%s\n' "$ROWS" | jq -r '[.data[] | select(.eventType == "agent.capability_check_denied") | .id][0] // empty')
if [ -n "$REFUSED" ]; then
get "$ADMIN/audit-logs/$REFUSED" |
jq -r '.data.context | "refused: \(.requested_action), reason \(.reason), decided by \(.decided_by)"'
fi
# 3. The row that registered the agent names the actor. For an API key, the
# context names the person who created the key.
CREATED=$(get "$ADMIN/audit-logs" --data-urlencode "targetId=$AGENT_ID" --data eventType=agent.created | jq -r '.data[0].id // empty')
if [ -n "$CREATED" ]; then
ENTRY=$(get "$ADMIN/audit-logs/$CREATED")
PERSON=$(printf '%s\n' "$ENTRY" | jq -r '.data.context.actor_owner_id // .data.actorId')
EMAIL=$(get "$ADMIN/users/$PERSON" | jq -r '.data.email')
printf '%s\n' "$ENTRY" | jq -r --arg email "$EMAIL" '.data | "registered by: \(.actorType) \"\(.actorName)\", person \($email)"'
fi
# 4. The same rows as a file, with the context of each row. The export takes
# the filters of the list.
get "$ADMIN/audit-logs/export" --data format=json --data-urlencode "actorId=$AGENT_ID" --data-urlencode "startDate=$SINCE" --output "$FILE"
echo "exported: $(jq '.data | length' "$FILE") rows to $FILE"
sh agents/audit-trail/http/trail.sh
Check the result
Output
what the agent did:
oauth.agent_token_issued success
abac_check failure
permission_check failure
abac_check failure
permission_check failure
agent.capability_check_denied success
refused: expenses:approve, reason capability_mismatch, decided by capability
registered by: api_key "Recipes key", person maya.chen@example.com
exported: 6 rows to expense-report-agent-audit.json
- For each question of the agent, LumoAuth also writes the checks of its policy engine,
abac_checkandpermission_check. Their resultfailuremeans that no attribute rule and no permission matched. The capabilities of the agent decided the answers:expenses:readwas allowed, and onlyexpenses:approvewas refused. - An allowed question writes no row of its own. A refused one writes
agent.capability_check_denied, with the resultsuccess. Read the refusal from the event type and the context. The context holds the sameaudit_idthat the agent received in the answer. - In the portal, open Audit logs under Security. Choose the agent under Actor and click Apply filters. Export logs downloads the same rows as CSV.
Limits
- The CLI has no command that follows the log live. To keep every row outside LumoAuth, send the log to a log destination.
- The rows of the actions of an agent name the agent, not a person. Only rows that a person or an API key wrote, such as
agent.created, lead to a person. lumo logs exporttakes no filter for the actor, so the CLI version calls the export endpoint throughlumo api.- The API returns only the entries inside the audit retention of your plan. One export holds at most 10,000 rows.