Skip to main content
Auth0 for AI Agents leverages Auth0 FGA to provide fine-grained authorization control for AI agents. As a result, when AI agents use Retrieval Augmented Generation (RAG) to provide sophisticated, relevant responses to user queries, they only have access to authorized data. By the end of this quickstart, you should have an AI application that can:
  1. Retrieve authorized data as context for a RAG pipeline.
  2. Use Auth0 FGA to determine if the user has authorization for the data.

Pick your tech stack

Download sample app

Start by downloading and extracting the sample app. Then open in your preferred IDE.

Install dependencies

In the root directory of your project, install the following dependencies:
  • @auth0/ai-langchain: Auth0 AI SDK for LangChain built for AI agents powered by LangChain.
  • @langchain/langgraph: For building stateful, multi-actor applications with LLMs.
  • langchain: The LangChain library.
  • @langchain/core: Core LangChain dependencies.
  • @langchain/openai: OpenAI provider for LangChain.
  • zod: TypeScript-first schema validation library.
  • langgraph-nextjs-api-passthrough: API passthrough for LangGraph.

Update the environment file

Copy the .env.example file to .env.local and update the variables with your Auth0 credentials. You can find your Auth0 domain, client ID, and client secret in the application you created in the Auth0 Dashboard.

Set up an FGA Store

In the Auth0 FGA dashboard:
1
Navigate to Settings. In the Authorized Clients section, click + Create Client.
2
Give your client a name and mark all the client permissions that are required for your use case. For the quickstart, you’ll only need Read and query.
3
Click Create.
Set up FGA Store
Once your client is created, you’ll see a modal containing Store ID, Client ID, and Client Secret. Add or update an .env.local file with the following content to the root directory of the project. Click Continue to see the FGA_API_URL and FGA_API_AUDIENCE.The confirmation dialog will provide you with all the information that you need for your environment file.
.env.local
Next, navigate to Model Explorer. You’ll need to update the model information with this:
Remember to click Save.

Secure the RAG Tool

After configuring your FGA Store, secure the RAG tool using Auth0 FGA and Auth0 AI SDK.The starter application is already configured to handle documents and embeddings.Document Upload and Storage
  • You can upload documents through the UI (src/app/documents/page.tsx).
  • Uploaded documents are processed by the API route (src/app/api/documents/upload/route.ts).
  • APIs for uploading and retrieving documents are defined in (src/lib/actions/documents.ts).
  • Database is defined in src/lib/db.
  • FGA helpers are defined in src/lib/fga.
  • Documents are stored as embeddings in a vector database for efficient retrieval (src/lib/rag/embedding.ts).
Access Control with Auth0 FGA
  • When a document is uploaded, the app automatically creates FGA tuples to define which users can access which documents. A tuple signifies a user’s relation to a given object. For example, the below tuple implies that all users can view the <document name> object.
  • Navigate to the Tuple Management section to see the tuples being added. If you want to add a tuple manually for a document, click + Add Tuple. Fill in the following information:
    • User: user:*
    • Object: select doc and add <document name> in the ID field
    • Relation: viewer

Create a RAG tool

Define a RAG tool that uses the FGAFilter to filter authorized data from the vector database:
src/lib/tools/context-docs.ts

Use the RAG tool from AI agent

Call the tool from your AI agent to get data from documents. First, update the /src/app/api/chat/[..._path]/route.ts file with the following code to pass the user credentials to your agent:
src/app/api/chat/[..._path]/route.ts
Next, add the following code to src/lib/auth0.ts:
src/lib/auth0.ts

Add Custom Authentication

For more information on how to add custom authentication for your LangGraph Platform application, read the Custom Auth guide.
In your langgraph.json, add the path to your auth file:
langgraph.json
Then, in your auth.ts file, add your auth logic:
src/lib/auth.ts
Now, update the src/lib/agent.ts file with the following code to add the tool to your agent:
src/lib/agent.ts

Test your application

Start the database and create required tables:
Start the application with npm run all:dev. Then, navigate to http://localhost:3000.
This will open the LangGraph Studio in a new tab. You can close it as we won’t require it for testing the application.
Upload a document from the documents tab and ask your AI agent a question about the document. You should get a response with the relevant information.Go to an incognito window, log in as a different user, and ask it the same question. You should not get a response.Share the document from the documents page to the second user and try again. You should see the information now.That’s it! You successfully integrated RAG protected by Auth0 FGA into your project.Explore the example app on GitHub.

Next steps