Quick setup
1
Create an auth file
Create a file (e.g.,
my_auth.py) in your project root:2
Add auth to aegra.json
3
Start the server
The authenticate handler
The@auth.authenticate decorator registers your authentication function. It receives the request headers and must return a dictionary with user data.
Required fields
Optional fields
Any additional fields you return (like
role, team_id, etc.) are preserved and accessible via ctx.user in authorization handlers and via the User model in custom routes.
Who can use your server
Yourauthenticate handler is the only gate. For each request it does one of two things:
Aegra does not look at
is_authenticated to decide. It is information for your own code: authorization handlers, graphs and custom routes. A user returned with "is_authenticated": False still gets in.
Pick one of the two setups below.
Signed-in users only
Raise for every request you don’t trust. Most deployments want this.Signed-in users and guests
Return a guest user when there is no token, marked with"is_authenticated": False. Then decide in an authorization handler what guests may do.
- All guests share the
guestidentity, so they see each other’s threads and store items. To keep guests apart, give each one its own identity, for example from a session ID your frontend sends. @auth.onis the fallback handler. A more specific handler such as@auth.on.threads.createruns instead of it, so repeat the guest check there.- A guest that can start runs spends your model budget. Allow that only on purpose.
Authorization handlers
Authorization handlers give you fine-grained access control for specific resources and actions.Handler types
Handlers can:- Allow — Return
NoneorTrue(default behavior) - Deny — Return
False(returns 403 Forbidden) - Filter — Return a dictionary with filters to apply to queries
- Modify — Modify the
valuedict (e.g., inject metadata)
Resolution priority
Handlers are matched from most specific to least specific:@auth.on.threads.create— Resource + action@auth.on.threads— Resource only@auth.on.*.create— Action only@auth.on— Global fallback
Examples
Restrict deletion to admins:Handler context
Thectx parameter provides:
ctx.user— Authenticated user object (with all fields you returned fromauthenticate)ctx.resource— Resource name ("threads","assistants", etc.)ctx.action— Action name ("create","read","update","delete","search")ctx.permissions— User permissions list
Return values
Which routes a handler covers
Every Agent Protocol route dispatches to your handlers. Sub-resources authorize as their parent, so one rule covers a whole family of endpoints:
There is no
runs resource: the Agent Protocol authorizes run operations under
the thread that owns them, and the SDK’s @auth.on has no runs attribute.
Stateless runs create an ephemeral thread, so they authorize as
threads.create_run. Omitting the thread_id does not bypass your thread rules.
A filter dict returned from a threads handler is applied to thread queries,
including by-id reads and deletes, not only to search. On threads.read,
threads.search and threads.delete it also applies to every route under a
thread: state, history, the run list, a run, its /join and /stream, run
deletes and the v2 event stream. Starting a run on an existing thread also
checks threads.read, because a run returns and streams the thread’s state. A
thread the filter excludes is a 404 there, the same answer GET /threads/{id}
gives.
A dict returned from threads.update or crons.update is not a filter.
PATCH /threads/{id} merges its metadata into the request, and the other
update routes ignore it. To block an update, return False.
An assistants.read filter also applies when a run or a cron uses the
assistant, and a crons handler’s filter applies to cron search, count and
delete.
Your handler runs once per event. A request that legitimately touches
several resources still checks each one: creating a cron authorizes
crons.create, then assistants.read, then the thread, then threads.create_run
with the run the cron fires, so you can deny at any layer. A cron update authorizes
threads.create_run again with the updated run when the cron is enabled after the
update or the update changes the run (input, config, context and the like).
A config or context the create_run handler returns is stored on the cron and
applies to every firing. Creating a run likewise authorizes threads.create_run, then
assistants.read for the assistant it uses.
The v2 run.start and input.respond commands authorize the same way: the
threads.create_run handler gets the run fields (assistant_id, input,
config, context) at the top level of value, not the command envelope, and
any config or context it returns is applied to the run. A denied command
returns a permission_denied error envelope.
Handlers may also inject data by mutating value in place. Setting
value["metadata"]["team_id"] in an @auth.on.threads.create handler stores
that metadata on the new thread.
Route-to-handler mapping lives in
core/auth_registry.py. A route that is not
registered there fails the test suite, so a new endpoint cannot silently skip
your handlers.Accessing user data in your graph
When auth is enabled, Aegra automatically injects the authenticated user’s data intoconfig["configurable"]["langgraph_auth_user"] before graph execution. This happens
server-side, so the client cannot tamper with it.
This works with all graph types: custom StateGraph graphs, create_react_agent,
create_agent, or any compiled graph.
From tools
- InjectedToolArg (recommended)
- ToolRuntime
InjectedToolArg tells the LLM to ignore this parameter (it won’t try to fill it).
Aegra injects the config automatically at runtime.From graph nodes
Graph nodes can accept aconfig: RunnableConfig parameter. Aegra injects
the authenticated user into config["configurable"]["langgraph_auth_user"]
before the graph executes:
The
Runtime object (from get_runtime()) does not include config.
To access config from nodes, add a config: RunnableConfig parameter to
your node function. In tools, use InjectedToolArg or ToolRuntime instead.From factory graphs
Factory graphs receive aServerRuntime
object that includes runtime.user with the authenticated user’s data. This is
available at factory time (when deciding graph structure) before the graph executes:
runtime.user object is the full User model with all fields from your
@auth.authenticate handler. Standard fields (identity, display_name,
permissions) and custom fields (role, team_id, etc.) are all accessible
as attributes or via dict-style access:
Factory graphs get user data in two places:
- Factory time (
runtime.useronServerRuntime): for structural decisions like which tools to include, which nodes to add, or which model to use. - Execution time (
config["configurable"]["langgraph_auth_user"]): available inside nodes and tools during the actual graph run (same as static graphs).
Available fields
Thelanggraph_auth_user dict contains everything your @auth.authenticate
handler returns, including any custom fields:
config["configurable"]:
config["configurable"]["user_id"]— the user’s identityconfig["configurable"]["user_display_name"]— the user’s display name
Provider examples
- JWT
- OAuth
- Firebase
Auth on custom routes
Custom routes can use authentication via therequire_auth dependency:
enable_custom_route_auth in your config:
No-auth mode
If no auth is configured, Aegra runs in no-auth mode:- All requests are allowed
- User is set to
anonymous, withis_authenticatedset toFalse - Authorization handlers are not called
anonymous user and its data, so don’t expose a server without auth to the internet.
No-auth mode applies only when auth.path is not set and AUTH_TYPE is noop. The server stops at startup with the error instead of serving requests as anonymous when:
auth.pathis set and the module cannot be loaded (the file is missing, the import raises, or the variable is not anAuthinstance)authis not an object, has a key other thanpathanddisable_studio_auth, orauth.pathis not a string. LangGraph’sopenapiandcachekeys are accepted and have no effectAUTH_TYPEiscustomandauth.pathis not set, orAUTH_TYPEis notnooporcustomAEGRA_CONFIGnames a file that does not exist. Only the defaultaegra.jsonfalls back tolanggraph.json
Configuration options
Non-interruptive design
Authorization handlers are additive by default:- If no auth is configured, requests are allowed
- If no handlers are defined, requests are allowed
- Handlers only restrict access when they explicitly deny