Session leases
A session lease marks one running session, such as a headless browser session, a long-running job or an AI agent run. Each agent key can have a session limit: how many leased sessions may run at once. The limit is off until you set it. It is not the in-flight decide cap (that one stays a 429). A lease does not hold a database connection.
When there is no lease
Section titled “When there is no lease”A decide that omits lease is not part of this limit: it is decided exactly as before (plan cap, tool, test limit) and never gets concurrency_exhausted, even when the key is already at its session limit. Leave the key’s session limit unset and no lease is stored.
If Ordainer cannot be reached, fail open the way you already do: the call runs, and it runs with no lease. Nothing was recorded, so there is nothing to release. A 429 from Ordainer’s own in-flight line still blocks. That line is separate from this limit.
curl -s -X POST "$ORDAINER_BASE_URL/v1/decide" \ -H "Authorization: Bearer $ORDAINER_KEY" \ -H "Content-Type: application/json" \ -H "Idempotency-Key: session-42" \ -d '{"tool":"agent.run","lease":{"ttl_s":120}}'On Windows? use the PowerShell tab.
ttl_s is optional. The default is 120 seconds. The maximum is 3600. Under the limit, an allow looks like this (live capture, a plan with no cap and session_limit 1):
{"decision":"allow","reason":"ok","remaining":null,"limit":null,"resets_at":"2026-12-01T00:00:00.000Z","mode":"enforce","request_id":"req_ysHAzYtH3nC3Kg0c","lease":{"id":"45204108-232d-456f-8538-9bf43335b13e","expires_at":"2026-11-13T14:36:47.566Z","running":1,"limit":1}}running counts this lease. lease.limit is the key’s session limit, not the plan’s limit at the top level. Top-level remaining and limit are the plan’s (here null: this plan has no cap).
The same idempotency key returns that same lease id and does not take a second lease. running in the replay is the count now, and the lease may have ended in the meantime.
For a decide that sent lease, at the limit enforce is HTTP 200 with decision: "deny" and reason: "concurrency_exhausted". That is a normal deny: one decision row, not counted, and no lease is taken. The plan cap is checked first. A cap deny takes no lease. A decide that omitted lease is decided as before and is never denied for the session limit.
In shadow the same situation is a would-deny (decision: "deny", mode: "shadow") and the lease is taken, so the running count matches what is actually running when you switch the plan to enforce.
Finish and extend
Section titled “Finish and extend”When the session ends, release it. A second release is a 200 and does not drive the count below zero.
curl -s -X POST "$ORDAINER_BASE_URL/v1/leases/$LEASE_ID/release" \ -H "Authorization: Bearer $ORDAINER_KEY" \ -H "Content-Type: application/json" \ -d '{}'{"id":"45204108-232d-456f-8538-9bf43335b13e","released":true,"running":0,"limit":1}A session can outlast its TTL (long-running jobs and agent runs often do). Extend the same lease before it expires. ttl_s (default 120, at most 3600) is counted from now, not added to the old expiry:
curl -s -X POST "$ORDAINER_BASE_URL/v1/leases/$LEASE_ID/extend" \ -H "Authorization: Bearer $ORDAINER_KEY" \ -H "Content-Type: application/json" \ -d '{"ttl_s":120}'{"id":"45204108-232d-456f-8538-9bf43335b13e","expires_at":"2026-11-13T14:36:47.786Z","running":1,"limit":1}Extending a lease that has already ended is 409 lease_inactive. Start a new one with decide. If you never release and never extend, the lease ends at expires_at and the slot frees itself. An unknown lease id, or one that isn’t a UUID, is 404 not_found (No such lease.). A bad ttl_s (not a whole number from 1 to 3600) on decide or extend is 400 invalid_request. Release and extend take an empty body or {}.
Use the same agent key that started the lease. Another organization’s lease is 404, the same as an unknown id, and so is another agent key in the same organization. A held key is 403 key_on_hold. A revoked key is 401 revoked_key. When support puts a key on hold, its running leases are released, and after the hold is lifted the key has its full session limit again. Revoking a key or deleting its customer also releases its leases. Any lease still open after that is released automatically shortly after, and is no longer counted in running.
Release and extend use the same per-address failed-auth limit as decide, and the same per-key request limit. A missing, unknown, or malformed key counts toward that address budget and then gets 429 too_many_failed_auth. A valid key does not. Over the key’s request rate, the call is 429 rate_limited and is not run. They are not part of the per-key in-flight line, so they never get 429 concurrency_limited: a full line can still finish its sessions.
Set the limit
Section titled “Set the limit”The limit is per agent key, not per customer. It applies only to decides that send lease. A decide without lease is decided as before and is not limited by the session limit. Null means off. Lowering the limit doesn’t end running leases: new leased decides get concurrency_exhausted until running drops below the new limit.
- Admin API:
PATCH /v1/admin/keys/:idwith{ "session_limit": 3 }ornull, scopekeys:write, needsIdempotency-Key, test keys only.GET /v1/admin/keys/:idandGET /v1/admin/keys(scopekeys:write,keys:revokeorusage:read) returnsession_limitandrunning. The Node and Python admin clients can’t setsession_limityet: use plain HTTP or the console. - Console: Keys, the Sessions column: enter a number and click Save. Leave it blank to turn it off.
The value is a whole number from 1 to 100.
The Node SDK (1.4.0 and later) and the Python package (0.4.0 and later) send lease, return the lease, and have release and extend calls. The Ruby gem (0.1.1) doesn’t send lease yet, and neither do Node 1.3.0 and Python 0.3.0: there, decide() never starts a lease, is never limited by the session limit, and never gets concurrency_exhausted. From Ruby, call POST /v1/decide with lease from your own HTTP client, as above.
Keep the lease id and release it when the session ends, not when the HTTP request that started it ends.
const { Ordainer, OrdainerLeaseError } = require('ordainer');const ordainer = new Ordainer(); // reads ORDAINER_KEY
async function startSession() { const d = await ordainer.decide({ tool: 'agent.run', lease: { ttlS: 300 } }); // lease: true for the default 120 s if (!d.allowed) return d.reason; // 'concurrency_exhausted' when this key already has its session limit running try { await runSession(); // long session? await ordainer.extend(d.lease.id, 300) before d.lease.expiresAt } finally { if (d.lease) { await ordainer.release(d.lease.id).catch((err) => { if (!(err instanceof OrdainerLeaseError)) throw err; // err.code: 'not_found', 'timeout', ... }); // if this fails, the lease still ends at expiresAt } }}ordainer.wrap(fn, { tool: 'agent.run', lease: true }) does the same for one function: the wrapped function takes the lease, runs fn only on an allow, and releases the lease in a finally block. A failed release there is logged, not thrown.
from ordainer import Client, LeaseError
client = Client() # reads ORDAINER_KEY
d = client.decide(tool="agent.run", lease={"ttl_s": 300}) # lease=True for the default 120 sif d.allowed: # d.reason is "concurrency_exhausted" when the key is at its session limit try: run_session() # long session? client.extend(d.lease.id, 300) before d.lease.expires_at finally: if d.lease is not None: try: client.release(d.lease.id) except LeaseError: pass # the lease still ends at expires_atclient.guard(fn, tool="agent.run", lease=True) does the same for one function: it runs fn(decision) only on an allow (else raises like decide_or_raise) and releases the lease in a finally block. A failed release there is logged, not raised. AsyncClient has the same calls, awaited.
- The lease on the result. Node:
d.leaseis{ id, expiresAt, running, limit }. Python:d.leasehasid,expires_at,runningandlimit. It is empty (null/None) when you didn’t ask for one, the key has no session limit, the answer was a deny (except a shadowconcurrency_exhaustedwould-deny, which still takes and returns the lease), or Ordainer couldn’t be reached. A fail-open runs with no lease, so there is nothing to release. concurrency_exhaustedis a normal deny. The call is blocked in enforce whateverfailMode/fail_modesays, and it isn’t an error or a rejection. Fail-open and fail-closed work exactly as they do withoutlease.- Helpers answer it as
403 plan_denied.decide()returnsconcurrency_exhaustedas a normal deny result. When Node’swrap()or Python’sguard()/deny_response()turn that deny into a blocked answer for your caller, they use403with codeplan_denied. The NGINX gateway recipe answers the same deny with429. Check the decision’sreasonif you need to tell them apart. - A slow release adds time on the way out.
wrap()andguard()wait for the release before they return. A release that times out is retried (timeouts, network errors and503only), so in the worst case it adds about 0.7 s to that return. The release can’t change the result you already have. - Release and extend.
release(id)is idempotent: a second release succeeds again.extend(id, ttl)counts from now. When either one doesn’t succeed, Node throwsOrdainerLeaseErrorand Python raisesLeaseError. Match on the code:not_found(404, PythonLeaseNotFoundError) orlease_inactive(409on extend, PythonLeaseInactiveError: start a new lease with decide). Other codes are the API’s own, such asrevoked_keyandrate_limited, plustimeoutandnetwork_error. A timeout, a network error or a503is retried first, as decide is. Neither call changes a decide result you already have. - The Node middleware doesn’t take leases. A session usually outlives the HTTP request that starts it. Use
decide()orwrap().