Running agents
Agents for your customers
Give one agent to all your customers, with each request seeing only that customer's rows, and tell Lokra who the customer is with a login token, an app-signed token or a Lokra token.
A support bot on your website talks to many customers, and it must never show one customer another's data. With Lokra, one agent serves all of them: each request says which customer it is for, and Postgres returns only that customer's rows. A prompt that talks the bot into asking for someone else's orders gets nothing back, because the database refuses it, not the bot's instructions.
How it works#
- A customer signs in to your app, as they do today.
- They ask your agent something. Your agent's backend calls Lokra with the agent's own token and something that says who the customer is.
- Lokra checks both and works out the customer's account, then hands Postgres that account, signed, before the agent's SQL runs.
- Postgres row-level security returns only rows for that account, after checking the signature. See what Postgres checks.
Lokra never signs anyone in and does not replace your login. It reads the proof of login your app already has.
1. Lock rows per request#
In the agent's policy, set per_request: true on its row lock instead of fixed values:
agents:
support-bot:
rows:
column: account_id
per_request: true
read:
customers: "*"
orders: "*"
notes: "*"
Then run lokra provision. It installs the lokra schema that checks each request's scope, and locks every table in the agent's policy that has an account_id column. See row locks for the details.
2. Say who the customer is#
| Your app | Use |
|---|---|
| Signs users in with Auth0, Clerk, Okta, Entra ID, Cognito, Supabase Auth or another OpenID Connect provider | The user's login token |
| Has its own login, such as Django, Rails or Express sessions | A token your app signs |
| Is a test, or a desktop client for one account | A Lokra token |
Your users sign in with an identity provider#
Tell Lokra which provider to trust, and which claim of its tokens holds the account:
identity:
issuer: https://acme.au.auth0.com/
audience: https://api.acme.com
agents:
support-bot:
rows:
column: account_id
claim: org_id
Your agent's backend forwards the signed-in user's token in an X-Lokra-User header, next to the agent's own token. Lokra checks the token's signature against the keys the provider publishes, its issuer, audience and expiry, then reads the account from the claim. The user's email, or their sub if the token has no email, is recorded as who the request was on behalf of.
issuermust match the token'sissclaim exactly, including any trailing slash.- Set
audienceto your API's identifier whenever the provider issues tokens for several apps. If the provider's tokens carry noaudclaim, leave it out. - A claim with dots, such as
app_metadata.tenant_id, is read as a path when no claim has that exact name. Auth0's namespaced claims, such ashttps://acme.com/org_id, are matched by their exact name. - Lokra accepts only tokens signed with the provider's public keys (RS256, ES256, PS256 or EdDSA). A token signed with a shared secret is refused.
- Lokra fetches the keys over HTTPS from
<issuer>/.well-known/openid-configuration, so the gate needs outbound access to your provider. Setjwks_urlif your provider publishes its keys somewhere else.
A few providers need one setting first:
- Auth0 issues an access token Lokra can read only when your app requests an API audience. Add the account to the token with an Action, as a namespaced claim.
- Supabase Auth projects need asymmetric JWT signing keys, not the legacy shared secret. The issuer is
https://<project>.supabase.co/auth/v1, and the account usually sits inapp_metadata.
To accept tokens from more than one provider, for example customers on Clerk and staff on Okta, make identity a list.
Your app has its own login#
Your app signs short-lived tokens itself, with a key Lokra gives it. First list the app in the policy, with the agents it may mint tokens for:
issuers:
webapp:
agents: [support-bot]
Then create its key and store it in your app's secrets:
lokra issuer-key webapp
Lokra keeps the key in .lokra/issuers.json. lokra issuer-key webapp --rotate replaces it, and every token signed with the old key stops working.
Your app mints a standard JWT for each customer session, signed with HS256, with kid set to the issuer's name. It must carry agent and exp, and the account under rows, or under the claim you set with claim:. It can last at most 8 hours. With PyJWT:
import os
import time
import jwt
token = jwt.encode(
{"agent": "support-bot", "sub": user.email, "rows": [account.id], "exp": int(time.time()) + 900},
os.environ["LOKRA_ISSUER_KEY"], algorithm="HS256", headers={"kid": "webapp"})
With jose in Node:
import { SignJWT } from "jose";
const token = await new SignJWT({ agent: "support-bot", rows: [account.id] })
.setProtectedHeader({ alg: "HS256", kid: "webapp" })
.setSubject(user.email)
.setExpirationTime("15m")
.sign(new TextEncoder().encode(process.env.LOKRA_ISSUER_KEY));
This token is the agent's token, so it goes in the Authorization header and no X-Lokra-User is needed. The key can mint tokens only for the agents its issuer lists. It is not the gate's signing key.
Testing, and desktop clients#
A Lokra token can name the account itself:
lokra mint-token support-bot --rows 42
lokra try support-bot "SELECT …" --rows 42 runs a query as account 42 from the terminal. To give a desktop client such as Claude Desktop or Cursor one account's view, pass it to lokra connect:
lokra connect support-bot --client claude-desktop --rows 42
3. Call Lokra from your agent#
Serve Lokra over HTTP, as in run in production, and have your agent's backend send both tokens with every request. With the MCP Python SDK:
from mcp import ClientSession
from mcp.client.streamable_http import create_mcp_http_client, streamable_http_client
LOKRA_URL = "https://lokra.internal.example.com/mcp"
async def ask_lokra(sql: str, agent_token: str, login_token: str):
headers = {"Authorization": f"Bearer {agent_token}", "X-Lokra-User": login_token}
async with create_mcp_http_client(headers=headers) as http:
async with streamable_http_client(LOKRA_URL, http_client=http) as (read, write):
async with ClientSession(read, write) as session:
await session.initialize()
return await session.call_tool("query", {"sql": sql})
agent_token comes from lokra mint-token support-bot. Mint it for the backend and renew it before it expires. login_token is the signed-in user's token, which your backend already has from the request.
If your agent's MCP client can send only one token, such as the Anthropic API's MCP connector with its authorization_token, use a token your app signs: it names both the agent and the account.
What is recorded#
- Every ledger entry for the request carries
row_scope, the account it was locked to, andon_behalf_of, the user.lokra exposure --rows 42uses them to list everything that could have touched account 42; see whose data an agent touched. whoamitells the agent which rows it can see, andlist_tablessays so in its note.- A proposed write keeps its account. Approvers see
only rows where account_id 42in the terminal, Slack and the dashboard. It runs for that account, and requests for any other account cannot see it or run it.
Errors#
| Error | Cause |
|---|---|
support-bot only sees the rows each request names, and this request names none |
No login token, and the agent's token has no rows. |
the login token has no org_id claim, which support-bot locks rows by |
The provider's token lacks the claim named in claim:. |
the login token comes from 'https://…', which the policy does not trust |
The token's iss matches no identity entry. Check the trailing slash. |
the login token is not valid: Audience doesn't match |
audience is set, and the token's aud does not include it. |
the agent's token and the login token name different rows |
Both name an account, and they disagree. |
the token names issuer 'webapp', which this gate does not know |
The issuer is not under issuers, or lokra issuer-key webapp has not been run on this gate. |
issuer webapp may not mint tokens for ops-bot |
The issuer's agents does not list that agent. |
the token from webapp lasts longer than 8 hours |
The app's token has an exp too far ahead. |
database refused the query: Lokra: the row scope of this request is not valid |
The scope did not come from this gate. Run lokra provision again if the gate's .lokra folder was replaced. |