> ## Documentation Index
> Fetch the complete documentation index at: https://auth0.com/ai/docs/llms.txt
> Use this file to discover all available pages before exploring further.

# Authorization for RAG

> Authorization for Retrieval-Augmented Generation (RAG) ensures that when an AI agent retrieves and uses your company's internal data to answer user questions, it only accesses information that the specific user is permitted to see. This prevents the AI model from inadvertently [exposing sensitive or confidential data](https://genai.owasp.org/llmrisk/llm022025-sensitive-information-disclosure/) to unauthorized individuals.

<Frame>
  <iframe src="https://play.vidyard.com/TCAzaPdj4SEp9yXTbgPP1W.html" title="Authorization for RAG using Auth0 FGA" frameborder="0" allow="accelerometer; autoplay; gyroscope; picture-in-picture; fullscreen" referrerpolicy="no-referrer-when-downgrade" allowfullscreen className="aspect-video object-cover w-full h-full" />
</Frame>

## The challenge: Securing data in RAG pipelines

Retrieval-Augmented Generation (RAG) is a powerful technique that enhances Large Language Models (LLMs) by providing them with relevant, up-to-date information from external data sources, such as a company's internal knowledge base or document repository.

However, without proper access controls, a RAG pipeline could retrieve documents containing sensitive information (e.g., financial reports, HR documents, strategic plans) and use them to generate a response for a user who should not have access to that data. This could lead to serious data breaches and compliance violations. Simply filtering based on user roles is often insufficient for managing the complex, relationship-based permissions found in real-world applications.

## The solution: Auth0 Fine-Grained Authorization (FGA)

To solve this challenge, Auth0 for AI Agents uses [**Auth0 Fine-Grained Authorization (FGA)**](https://auth0.com/fine-grained-authorization). Auth0 FGA is a flexible, high-performance authorization service for applications that require a sophisticated permissions system. It implements Relationship-Based Access Control (ReBAC) to manage permissions at large-scale. Auth0 FGA is built on top of [OpenFGA](https://openfga.dev/), created by Auth0, which is a [CNCF](https://cncf.io/) sandbox project.

Auth0 FGA allows you to decouple your authorization logic from your application code. Instead of embedding complex permission rules directly into your application, you define an authorization model and store relationship data in Auth0 FGA. Your application can then query Auth0 FGA at runtime to make real-time access decisions.

### How it works with RAG

Integrating Auth0 FGA into your RAG pipeline ensures that every document is checked against the user's permissions before it's passed to the LLM.

<Frame caption="Authorization for RAG">
  <img className="hidden dark:block" src="https://mintlify.s3.us-west-1.amazonaws.com/auth0-genai/img/intro/how_it_works_with_rag_diagram_dark.png" alt="Authorization for RAG" />

  <img className="block dark:hidden" src="https://mintlify.s3.us-west-1.amazonaws.com/auth0-genai/img/intro/how_it_works_with_rag_diagram_light.png" alt="Authorization for RAG" />
</Frame>

The process works as follows:

<Steps>
  <Step title="Authorization model">
    First, you define your authorization model in Auth0 FGA. This model
    specifies the types of objects (e.g., `document`), the possible
    relationships between users and objects (e.g., `owner`, `editor`, `viewer`),
    and the rules that govern access.
  </Step>

  <Step title="Store relationships">
    You store permissions as 'tuples' in Auth0 FGA. A tuple is the core data
    element, representing a specific relationship in the format of `(user,
            relation, object)`. For example, `user:anne` is a `viewer` of
    `document:2024-financials`.
  </Step>

  <Step title="Fetch and filter">
    When a user submits a query to your AI agent, your backend first fetches
    relevant documents from a vector database and then makes a permission check
    call to Auth0 FGA. This call asks, "Is this user allowed to view these
    documents?". Our AI framework SDKs abstract this and make it as easy as
    plugging in a filter in your retriever tool.
  </Step>

  <Step title="Secure retrieval">
    Auth0 FGA determines if the user is authorized to access the documents. Your
    application backend uses this data to filter the results from the vector
    database and only sends the authorized documents to the LLM.
  </Step>
</Steps>

## Get started

To begin using Auth0 FGA in your AI agents, refer to the following resources:

### Quickstarts

<Columns cols={2}>
  <Card title="Authorization for RAG" href="/get-started/authorization-for-rag" icon="key" horizontal />

  <Card title="Auth0 FGA Getting Started" href="https://docs.fga.dev/getting-started" icon="gear" horizontal />
</Columns>

### Sample Apps

<Columns cols={2}>
  <Card
    title="Assistant0: Next.js + Vercel AI SDK"
    href="https://github.com/auth0-samples/auth0-assistant0/tree/main/ts-vercel-ai"
    icon={
  <img
    src="https://mintlify-assets.b-cdn.net/auth0/vercel.svg"
    alt="Vercel AI"
  />
}
    horizontal
  />

  <Card
    title="Assistant0: Next.js + LangGraph"
    href="https://github.com/auth0-samples/auth0-assistant0/tree/main/ts-langchain"
    icon={
  <img
    src="https://mintlify-assets.b-cdn.net/auth0/langchain.svg"
    alt="LangChain"
  />
}
    horizontal
  />

  <Card
    title="Assistant0: FastAPI + LangGraph"
    href="https://github.com/auth0-samples/auth0-assistant0/tree/main/py-langchain"
    icon={
  <img
    src="https://mintlify-assets.b-cdn.net/auth0/langchain.svg"
    alt="LangChain"
  />
}
    horizontal
  />

  <Card
    title="Assistant0: Next.js + LlamaIndex"
    href="https://github.com/auth0-samples/auth0-assistant0/tree/main/ts-llamaindex"
    icon={
  <img
    src="https://mintlify-assets.b-cdn.net/auth0/llamadex.svg"
    alt="LlamaIndex"
  />
}
    horizontal
  />

  <Card title="SmartHR Assistant: Next.js + LangChain" href="https://github.com/auth0-samples/auth0-ai-smart-hr-assistant" icon="github" horizontal />

  <Card title="AI Samples: Multiple frameworks" href="https://github.com/auth0-samples/auth0-ai-samples/tree/main/authorization-for-rag" icon="github" horizontal />

  <Card title="Auth0 AI SDK TypeScript samples" href="https://github.com/auth0/auth0-ai-js/tree/main/examples/authorization-for-rag" icon="code" horizontal />

  <Card title="Auth0 AI SDK Python samples" href="https://github.com/auth0/auth0-ai-python/tree/main/examples/authorization-for-rag" icon="python" horizontal />
</Columns>

### Learn more

<Columns cols={2}>
  <Card title="Auth0 FGA Documentation" href="https://docs.fga.dev/" icon="gear" horizontal />

  <Card title="OpenFGA Documentation" href="https://openfga.dev/" icon="book" horizontal />
</Columns>
