Skip to main content

Restrict agent sessions

Clerk can give an AI agent a browser session that's signed in as one of your users through:

Clerk can also restrict what an agent session does with Clerk itself. A session from the create_browser_session tool, for instance, can't change the user's profile or sign out. An Agent Task you create yourself gets the same restrictions only when you set the Backend API's session_restricted parameter to true. Your app's backend still sees an ordinary signed-in session, so what agents can do with your own features is up to you.

Before you use any of them, update your app to restrict what agents can do when they act for your users. For example, you might block payments or account deletion during an agent session.

Check for an agent session

The Auth object has an agent property. It's undefined for an ordinary session, and holds details about the agent when an agent is using the session. It requires @clerk/nextjs 7.10.0 or later, @clerk/express 2.1.90 or later, or @clerk/backend 3.29.0 or later.

The following example blocks agents from a billing route:

app/api/billing/route.ts
import { auth } from '@clerk/nextjs/server'

export async function POST() {
  const { userId, agent } = await auth()

  if (!userId) {
    return new Response('Unauthorized', { status: 401 })
  }

  if (agent) {
    return new Response('Agents cannot change billing', { status: 403 })
  }

  // Update billing for the user
}

The example uses Next.js's auth() helper. The Auth object from other Clerk SDKs has the same agent property, such as the one getAuth(req) returns in Express.

If your backend verifies session tokens without a Clerk SDK, check the session token for the agt claim instead. Clerk adds it only when an agent is using the session.

Feedback

What did you think of this content?

Last updated on