Detect AI agents on your login page
More of your customers now sign in through an AI assistant that acts for them. Fraud scripts pretend to be those assistants. This recipe adds a login decision that tells them apart, and shows how to treat each one. Every decision names which agent and which device is acting, never who the person is.
1. Tag the login form
Section titled “1. Tag the login form”<script src="https://c.opencontext.com/t.js" data-key="oc_pk_test_..." async></script>
<form method="post" action="/login" data-collect="login"> <input name="email" type="email" autocomplete="username" required> <input name="password" type="password" autocomplete="current-password" required> <button>Sign in</button></form>2. Decide after the password checks out
Section titled “2. Decide after the password checks out”Call the decision API after the credentials are valid and before you start the session. Forward any Web Bot Auth signature headers: agents that sign their requests can then be verified rather than only declared or inferred.
app.post("/login", async (req, res) => { const user = await checkPassword(req.body.email, req.body.password); if (!user) return res.status(401).send("Email or password is incorrect.");
const sig = req.get("signature"); let decision = { recommendation: "allow" }; // if the call fails, most teams allow logins try { const r = await fetch("https://api.opencontext.com/v1/decision", { method: "POST", headers: { Authorization: `Bearer ${process.env.DECISION_SECRET_KEY}`, "Content-Type": "application/json" }, body: JSON.stringify({ request_id: req.body.request_id, event_type: "login", account_id: user.id, ip: req.ip, user_agent: req.get("user-agent"), agent_signature: sig && { signature: sig, signature_input: req.get("signature-input"), signature_agent: req.get("signature-agent"), authority: req.get("host"), }, }), signal: AbortSignal.timeout(1500), }); if (r.ok) decision = await r.json(); } catch {}
return act(decision, user, req, res);});3. Read the bot block
Section titled “3. Read the bot block”"bot": { "verdict": "ai_agent", "agent_type": "consumer_assistant", "operator": "anthropic", "acting_for": "user", "evidence": "verified", "confidence": 0.95}verdict:human,ai_agent,good_bot, orbad_bot.evidence:verified(a signature or the operator’s published network checked out),declared(it named itself), orinferred(how it behaves).acting_for:userwhen it works for a person.- A session that claims to be a known agent but cannot back it up gets
BOT_AGENT_SPOOFED.
The full field list is in AI agents.
4. Act on it
Section titled “4. Act on it”Your login ruleset already sets recommendation, so the simplest policy is to follow it. The starter rules step up unverified agents, ask the person to confirm the first login by a verified agent (after that, on the same device, the agent signs in without friction), and deny spoofed agents and hidden automation.
function act(decision, user, req, res) { switch (decision.recommendation) { case "allow": return startSession(user, res, decision.decision_id); case "step_up": // For an agent, decision.bot.step_up === "verify_agent": ask the person it // acts for to confirm (a push or email link), or ask for a signed identity. return askToConfirm(user, res, decision.decision_id); case "review": return startSession(user, res, decision.decision_id, { limited: true }); default: return res.status(403).send("We could not sign you in. Please try again later."); }}To change the policy, for example to let verified assistants straight in, edit the login ruleset in the console. Your code does not change. See Rules.
5. Publish your agent policy
Section titled “5. Publish your agent policy”Tell agents how to identify themselves on your site at /.well-known/agent-policy.json. The tag’s hidden notice points agents there. A starting point is in AI agents.
6. Tell us what was true
Section titled “6. Tell us what was true”When the person confirms (or denies) that their agent was allowed to act, report it as an outcome with bot_truth set to agent_authorized or agent_unauthorized. See Outcomes.