> ## Documentation Index
> Fetch the complete documentation index at: https://auth0-genai-fix-a4aa-connections-content-audit.mintlify.site/llms.txt
> Use this file to discover all available pages before exploring further.

# Authorization for RAG

> Enforce fine-grained, document-level access control in RAG pipelines with Auth0 FGA, ensuring AI agents only retrieve data the current user is authorized to see.

<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>

## What is RAG?

Retrieval-Augmented Generation (RAG) provides Large Language Models (LLMs) with relevant, up-to-date information from external data sources, such as a company's internal knowledge base or document repository.

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.

## What is Authorization for RAG?

[Auth0 Fine-Grained Authorization (FGA)](https://auth0.com/fine-grained-authorization) implements Relationship-Based Access Control (ReBAC) control for your RAG pipelines by decoupling 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

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-fix-a4aa-connections-content-audit/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-fix-a4aa-connections-content-audit/img/intro/how_it_works_with_rag_diagram_light.png" alt="Authorization for RAG" />
</Frame>

The Auth0 FGA flow has the following steps:

<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="Assistant0: FastAPI + LlamaIndex"
    href="https://github.com/auth0-samples/auth0-assistant0/tree/main/py-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>
