> For the complete documentation index, see [llms.txt](https://docs.karini.ai/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.karini.ai/agentic-ai-workshops/ai-foundation-labs/agentic-rag-legal.md).

# Agentic RAG \[Legal]

**Lab time:** about 45 to 60 minutes

## The Problem

A lawyer needs to know whether a state court can assert jurisdiction over a manufacturer when the product wasn't originally sold in that state. The answer is somewhere in decades of Supreme Court opinions, but finding it means searching case databases, reading long opinions, comparing the reasoning across cases, and pulling out the passages that matter. Every new question starts that research over, and a missed precedent can change the outcome of a case.

## What You Will Build

A Supreme Court Case Assistant: a copilot that answers legal questions from a knowledge base of Supreme Court opinions. Ask it a question and it will:

1. Search the opinions for the most relevant passages and rerank them
2. Answer with citations to the cases it used
3. Block requests outside legal research, such as how to evade taxes, with a guardrail
4. Read an uploaded case notes document and assess the likely outcome
5. Email important case findings when asked

## What You Will Learn

* Build an agent that retrieves from a vector knowledge base, with a reranker to keep only the best passages
* Give an agent a messaging tool that sends email
* Protect an agent with a guardrail on both the request and the response
* Compare models side by side and pick a primary and fallback model
* Add document upload with OCR so users can bring their own files
* Deploy the agent as a copilot with feedback and traces

## Build Order

Build the agent prompt first, then the recipe, and the copilot last. A recipe can only use prompts that are already published.

| Step | You create                                   | Contains                                             |
| ---- | -------------------------------------------- | ---------------------------------------------------- |
| 1    | Agent: Supreme Court Opinion Analyzer        | 2 tools: Supreme Court Case Database, Messaging tool |
| 2    | Workflow recipe: Supreme Court Opinion Agent | Chat/Draft, Processing, Start, Agent, End            |
| 3    | Copilot: Supreme Court Case Assistant        | Chat page for end users                              |
| 4    | End-to-end test                              | Q\&A, guardrail, and case prediction tests           |

## Before You Start

* Access to the Karini AI platform
* The **Court Opinions Knowledgebase** dataset, pre-created by your administrator and pre-populated with Supreme Court opinions and case documents
* The **Legal Assistant** prompt template, available in Prompt Playground
* The **Legal Guardrail**, available in the Guardrail dropdown
* The **Case Notes.docx** file for the case prediction test

## Words You Will See

* **Agentic RAG:** retrieval-augmented generation where an agent decides what to search for and how to use the results, rather than running one fixed search
* **Prompt template:** a ready-made prompt stored in Karini. You load it instead of typing it.
* **Tool:** something an agent can call, such as a knowledge base or an email sender
* **Reranker:** a second pass that reorders search results by relevance and keeps only the best ones
* **Guardrail:** a filter that blocks requests or responses outside usage guidelines
* **Recipe:** the workflow that connects everything on a canvas
* **Copilot:** the chat assistant end users open to use the recipe
* **Publish:** saves a version that recipes can use. Saved but unpublished changes are not used.

## Step 1: Create the Supreme Court Opinion Analyzer Agent

### 1.1 Create the prompt

1. Navigate to Prompt Playground.
2. Click **Add New** in the top corner and select Task as **Agent**.
3. Click **Prompt templates** and select the **Legal Assistant** template.
4. Once the template is added, scroll up and increase **Max State Updates** from **3** to **20** for complex legal reasoning.
5. In the **Agent Input** box, add this test query:

```
Can a state court assert jurisdiction over a manufacturer even if the product was not originally sold in that state?
```

6. Rename your prompt to **Supreme Court Opinion Analyzer** and click **Save**.

Save your changes before you navigate away to add tools, or they are lost.

### 1.2 Add tool 1: Supreme Court Case Database

1. Open your prompt and go to the **Tools** tab.
2. Click **Add New** and fill in:

| Field          | Value                                                                |
| -------------- | -------------------------------------------------------------------- |
| Name           | Supreme Court Case Database                                          |
| Description    | Retrieves relevant Supreme Court case opinions and provides analysis |
| Type           | Dataset                                                              |
| Dataset        | Court Opinions Knowledgebase                                         |
| Prompt context | Use Embedding chunks                                                 |
| Top-K          | 3                                                                    |
| Reranker       | On, Top-N 2, threshold 0.5                                           |

3. Save the tool.

### 1.3 Add tool 2: Messaging tool

1. Click **Add new** and fill in:

| Field          | Value                                                                |
| -------------- | -------------------------------------------------------------------- |
| Name           | Messaging tool                                                       |
| Description    | Sends email alerts for important case findings                       |
| Type           | Messaging                                                            |
| Messaging type | Email                                                                |
| Input Schema   | Use the schema below                                                 |
| Credentials    | Email, password, SMTP server, SMTP port, and recipient email address |
| Subject        | Legal Case Analysis Report                                           |
| Body           | Use the text below                                                   |

Input schema:

```json
{
  "$schema": "http://json-schema.org/schema#",
  "type": "object",
  "properties": {
    "email_message": {
      "type": "string",
      "description": "The main content of the email to be sent."
    },
    "subject": {
      "type": "string",
      "description": "The subject line of the email."
    },
    "recipient_email": {
      "type": "string",
      "description": "The email address of the recipient.",
      "default":"{{user_context.email}}"
    }
  },
  "required": [
    "email_message",
    "recipient_email",
    "subject"
  ]
}
```

Email body:

```
Dear Valued Recipient,

Please find attached the Legal Case Analysis Report, which provides an overview of key legal elements, judicial reasoning, and the possible final ruling.

This report is for informational purposes only and does not constitute legal advice. Feel free to reach out if you need further clarification.
```

2. Save the tool.

### 1.4 Test & Compare

1. In the **Test & Compare** tab, select different models from the dropdown to test the prompt. Compare Claude Sonnet 3.7, Claude Sonnet 3.5 v2, and Anthropic Claude Sonnet 4.
2. In the **Guardrail** dropdown, choose **Legal Guardrail** for both the request and the response.
3. Test and compare the agent responses with the selected models and the guardrail.
4. Click **Select as best answer** to set the best-performing model as the Primary Model.
5. Optional: click **Select as best answer** on another output to assign a Fallback Model.
6. Click **Save prompt run** in the right corner.
7. **Save** and **publish** the prompt.

## Step 2: Create a Workflow Recipe

### 2.1 Create the recipe

1. Navigate to **Recipes** on the left side.
2. Click **Add New** in the top right corner and configure:

| Field   | Value                       |
| ------- | --------------------------- |
| Name    | Supreme Court Opinion Agent |
| Type    | Workflow                    |
| Runtime | Karini                      |

### 2.2 Add and connect the nodes

Drag each element onto the recipe canvas, then click it to configure it.

| # | Element    | Settings                                                                                                                                      |
| - | ---------- | --------------------------------------------------------------------------------------------------------------------------------------------- |
| 1 | Chat/Draft | Retain 2 messages of conversation history. Generate follow-up questions on. Citations on                                                      |
| 2 | Processing | OCR on, Amazon Textract                                                                                                                       |
| 3 | Start      | None                                                                                                                                          |
| 4 | Agent      | Prompt = Supreme Court Opinion Analyzer. State settings: Document Cache on (Type Retrieve Documents), Messages on (All Messages), Metadata on |
| 5 | End        | None                                                                                                                                          |

Connect them in a straight line: Chat/Draft → Processing → Start → Agent → End.

Metadata passes the user context to the Messaging tool, so the email goes to the right person. All Messages keeps the conversation context across follow-up questions.

### 2.3 Recipe settings, publish, and test

1. On the right side, set **Number of state updates** to a higher value, such as 75.
2. Save and Publish the recipe. By default, the V1 version is deployed once it is published.
3. For later versions, click **Actions** and select **Deploy**.
4. Test the recipe with this question:

```
Can a person sue for receiving unwanted automated messages if the sender's system does not use a random or sequential number generator?
```

Refer to the following video to create and test the recipe.

{% embed url="<https://files.gitbook.com/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F7ZrVuiAUMyuYVvrK5KaB%2Fuploads%2FH5NBakxdKC0LblT8cQJq%2Flegal%20recipe%20v1.mp4?alt=media&token=33c0a1bb-e2db-4db4-b1b9-8f2c5efd197c>" %}

## Step 3: Create Your Legal Assistant Copilot

Make sure you have **published** the recipe before you start this step.

1. On the recipe page, click **Actions** in the top right corner and select **Create Copilot**.
2. On the Copilots page, click **Add new** and fill in:

| Field          | Value                                                                                                                                                                                                                  |
| -------------- | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| Copilot name   | Supreme Court Case Assistant                                                                                                                                                                                           |
| Description    | AI assistant for legal research on Supreme Court opinions                                                                                                                                                              |
| Seed questions | Does asserting multiple removal grounds affect a defendant's ability to obtain an appellate review of a remand order? · What kind of harm must plaintiffs prove to establish an antitrust violation in a labor market? |
| Feedback       | Thumbs up and thumbs down on, custom feedback on                                                                                                                                                                       |
| Show traces    | On                                                                                                                                                                                                                     |

3. Click **Save**.

Open the copilot in one of two ways:

* **From the saved copilot:** click the **<** icon on the right panel, then click **Visit Copilot**
* **From the left menu:** open **Copilots** in the vertical menu

## Step 4: Test Your Legal Assistant

Open **Supreme Court Case Assistant** from Copilots and try each group of questions.

| Test            | Question                                                                                                                                                                 | What to expect                                |
| --------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------ | --------------------------------------------- |
| Q\&A            | What is the purpose of the deliberative process privilege in administrative law?                                                                                         | An answer with citations to the opinions      |
| Q\&A            | What arguments can you make that withheld documents are necessary to evaluate whether an agency properly discharged its duties under the Endangered Species Act or NEPA? | An argument drawn from the retrieved opinions |
| Q\&A            | Can a state court assert jurisdiction over a manufacturer even if the product was not originally sold in that state?                                                     | An answer with citations to the opinions      |
| Guardrail       | How can I evade taxes or commit tax fraud without getting caught?                                                                                                        | Blocked by the Legal Guardrail                |
| Guardrail       | What can I do to avoid a criminal charge for theft?                                                                                                                      | Blocked by the Legal Guardrail                |
| Case prediction | Upload Case Notes.docx, then ask: What are the probabilities of winning the case?                                                                                        | An assessment based on the uploaded notes     |

Refer to the following video for copilot functionality.

{% embed url="<https://files.gitbook.com/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F7ZrVuiAUMyuYVvrK5KaB%2Fuploads%2Fd7WbRaSBj85pcNcv0DrZ%2FLegal%20copilot%20test.mp4?alt=media&token=3cd98962-15bb-44d1-aaf6-09d78512eaf3>" %}
