Python quickstart
You’ll ask Ordainer before one paid tool runs in a Python script and watch the sixth call get blocked, then try shadow mode (record what would be denied, block nothing). Switching between shadow and enforce is a console click, with no redeploy.
You need Python 3.9+. The package uses only Python’s standard library. The optional async client needs httpx, which the [async] extra installs. There is no Django, Flask or FastAPI middleware yet: call client.decide() from your own handler.
-
Get a test key
Sign up and copy your test key
Section titled “Sign up and copy your test key”Sign up with your work email and copy the pilot test key from the welcome page. If you want a one-paste deny first, use Your first deny. Then put the key in your shell:
Terminal window export ORDAINER_KEY=ord_test_...export ORDAINER_BASE_URL=https://api.ordainer.devThe package reads
ORDAINER_KEYand falls back toORDAINER_API_KEYwhenORDAINER_KEYis unset or empty. From 0.2.0, if both are set,ORDAINER_KEYwins; 0.1.0 usedORDAINER_API_KEYin that case. Passingapi_key=overrides both. The console’s .env download names each key after its label (for exampleORDAINER_KEY_PILOT).If you lose a key, create a new test key in Console → Keys. The console never shows a full key again, only its first characters.
-
Install the package
Section titled “Install the package”The package is called
ordainerand is on PyPI (version 0.4.0):Terminal window pip install ordainerFor the async client (
AsyncClient), install the extra instead:Terminal window pip install "ordainer[async]" -
Watch your tool get blocked
Section titled “Watch your tool get blocked”Click Reset sandbox in the console. The console asks you to confirm: Cancel has the focus, and Cancel or Escape changes nothing. Then save and run this script:
decide.py from ordainer import Client, deny_response# Reads ORDAINER_KEY (or ORDAINER_API_KEY) and ORDAINER_BASE_URL. Timeout 0.3 s, fails open on timeouts and outages.client = Client()def run_research():return "expensive research done"for call in range(1, 7):decision = client.decide(tool="research.run") # a new idempotency key per callif decision.allowed:print(call, decision.decision, decision.remaining, run_research())else:blocked = deny_response(decision) # what you send back to your callerprint(call, blocked["status"], blocked["body"]["error"]["code"], decision.reason)print(blocked["body"]["error"]["message"])Terminal window python3 decide.pyCalls 1 to 5 run your tool (
allow, withremainingcounting down). On call 6 the tool does not run:decision.allowedisFalse,decision.reasonisquota_exceeded, anddeny_response(decision)gives you a429 plan_limit_reachedanswer (status,headerswithRetry-After, and a JSONbody) to send to your caller. If your clients treat 429 as “retry soon”, passdeny_status=402toClient. Only429and402are accepted; anything else raisesConfigurationError. All the blocked responses: Errors → SDK blocked responses.Prefer exceptions?
client.decide_or_raise(tool=...)returns the decision when the call may run and raises aBlockedError(.status,.headers,.body,.decision) otherwise.client.guard(fn, tool=...)runsfn(decision)only when allowed. -
Shadow mode for your real plan
Section titled “Shadow mode for your real plan”On a real product, start in shadow: we make the real decision and record it, but your tool runs even when we would say no. To see it on the sandbox: Console → Plans → on the
pilotrow, switch to shadow. Then click Reset sandbox, confirm the dialog (Cancel has the focus; Cancel or Escape changes nothing), and run the script again. Every call runs your tool (decision.allowedisTrue, and from call 6decision.would_denyisTrue), and stderr shows:[ordainer] shadow: would deny tool=research.run reason=quota_exceeded remaining=0/5 request_id=req_gHgqUYUyo96jVrvu (call allowed)Leaving
modeunset means “do what the plan in the console says”. To pin a mode in code instead, passmode="shadow"ormode="enforce"toClient. A pinned mode overrides the console. When the would-denies look right, open Console → Plans and click Enforce in that plan’s Mode column. To send decisions to your own metrics, passon_decision=lambda d: .... More: Shadow vs enforce.
import asynciofrom ordainer import AsyncClient
async def main(): async with AsyncClient() as client: decision = await client.decide(tool="research.run") print(decision.allowed, decision.decision)
asyncio.run(main())Same options, same rules, same messages as Client.
Admin API
Section titled “Admin API”From 0.3.0, OrdainerAdmin (and AsyncOrdainerAdmin) calls the admin API with an admin key (ord_admin_…, from admin_key or ORDAINER_ADMIN_KEY). It does not call decide(), and an admin key does not work on /v1/decide. Create the admin key in Console → Keys. Do not log the admin key, or the agent-key secret from create_key (it is only in the first response).
from ordainer import Client, OrdainerAdmin
# Admin key scopes: usage:read, customers:write, keys:write and keys:revokeadmin = OrdainerAdmin() # admin_key, or ORDAINER_ADMIN_KEY
# Find the plan first, so nothing is created if it isn't there.# "pilot" is a sandbox plan from sign-up; use your own plan's name.plan_id = next((p.id for p in admin.list_plans() if p.name == "pilot"), None)if plan_id is None: raise SystemExit("No plan named pilot")
customer = admin.create_customer("example")try: created = admin.create_key(label="pilot", customer_id=customer.id, plan_id=plan_id) agent_key = created.secret # first response only; do not log it record = admin.get_key(created.id) # no secret; revoked_at is None while the key is active decision = Client(api_key=agent_key).decide(tool="research.run") print(decision.allowed, decision.decision)finally: # Runs even if a step above fails, so no customer is left behind. deleted = admin.delete_customer(customer.id) # needs customers:write and keys:revoke
print(deleted.revoked_key_ids) # the keys this call revoked# deleted.request_id is the delete's X-Request-Id: list only that writechanges = admin.list_changes(request_id=deleted.request_id)print([c.action for c in changes])delete_customer revokes that customer’s active test keys in the same step and returns {id, deleted, revoked_key_ids}. The sandbox customer cannot be deleted. A customer that owns a live key cannot be deleted this way. An unknown id is 404 not_found.
The client also wraps the other admin routes on the Admin API page (plans, list/update, revoke, move, pagination). Writes send Idempotency-Key; pass idempotency_key to choose it. Failures raise AdminError subclasses; match on code. For the async client, pip install "ordainer[async]" and use AsyncOrdainerAdmin / AsyncClient the same way. More: the package README.
Next steps
Section titled “Next steps”- One agent key per customer integration or agent: pass
api_key=per call:client.decide(tool="research.run", api_key=customer_key). Counts are per key. - Retries: each call gets a fresh random idempotency key unless you pass
idempotency_key=built from your own data (your job ID or queue message ID), one key per real operation. A key replays the first answer for 24 hours.retries=n(0 to 5, default 1) retries a timeout, network error or 503 with the same key. More: Idempotency and retries. - Outages: if we’re slow (0.3 s per attempt; at most about 0.65 s with the default retry), unreachable, answer with a 5xx, or send an answer that isn’t a decision, the call is allowed and a warning is logged. The exception is a sticky deny: within 60 seconds (
sticky_deny) of aquota_exceededdeny for the same key and tool, that call is denied instead of running (Sticky deny). Passfail_mode="closed"to block instead. An error answer (any 4xx, such asunknown_key, or a429 rate_limited) is not an outage: it’s blocked in everyfail_mode, except403 subscription_inactive, which followsbilling_lapse(default"open": allowed), and429 safety_limit_reached, which is allowed withdecision.safety_limitedset.billing_lapseis the billing-lapse setting for when your own Ordainer subscription has lapsed:"open"runs calls unchecked,"closed"blocks them. See Fail open vs fail closed. - Options:
api_key,base_url,timeout(seconds, 0.3),fail_mode("open"),retries(1),retry_budget(seconds, 0.65),mode,billing_lapse("open"),deny_status(429),sticky_deny(seconds, 60),logger,on_decision. Full list: the package README. - Going live: Live keys are not available yet. Live keys open when paid plans launch. Your test keys cover everything in your beta.
- Every field and error: Decide API reference and Errors.