# llama-index

A Cursor rule with code examples for LlamaIndex: modular functions, the Workflow API, LlamaCloud parsing and async index building.

- rule · `type=rule` · Cursor · ★ 3579
- Author: sanjeed5
- License: CC0-1.0 · policy=mirror · the beginning of the file is at the end of this page
- Source: https://github.com/sanjeed5/awesome-cursor-rules-mdc/tree/HEAD/rules-mdc/llama-index.mdc
- Updated: 2026-05-19
- HTML: https://affpapa.org/en/nevibecode/rules/llama-index

## What it does

The rule loads into Cursor and steers the agent toward a particular shape of LlamaIndex code. It ships paired good and bad examples: split components into separate functions and classes instead of one long script, configure `Settings.llm` and `Settings.embed_model` before anything else, wrap a query engine in `LlamaAgentWorker` so it fits the `Workflow` API.

A second block covers LlamaCloud. Complex PDFs and office files go through `LlamaParseReader` with table and figure preservation, and indexes live in the cloud. Both paths need `LLAMAPARSE_API_KEY` and `LLAMACLOUD_API_KEY` in your environment. The rule also flags `SimpleDirectoryReader` as a poor fit for PDFs with tables, since structure gets lost — worth checking if that is what your pipeline currently uses.

Performance advice is about async: build the index in a separate thread and set chunk size through `SentenceSplitter`. The rule targets `**/*.py`, so in a Python project it stays active nearly everywhere.

## Install

### Cursor (native platform)

**Manually: put the files in place**
- File: `.cursor/rules/llama-index.mdc` — Frontmatter: description, globs, alwaysApply

### Claude Code

**Manually: put the files in place**
- File: `.claude/rules/llama-index.md` — The paths frontmatter field limits the files: move globs from the source into paths

### Codex

**AGENTS.md**
- Codex reads AGENTS.md: paste the rule text there

### GitHub Copilot

**Manually: put the files in place**
- File: `.github/instructions/llama-index.instructions.md` — The applyTo field sets which files it applies to: move globs from the source into applyTo

### Windsurf (Devin Desktop)

**Manually: put the files in place**
- File: `.devin/rules/llama-index.md`

### Cline

**Manually: put the files in place**
- File: `.clinerules/llama-index.md`

Where a file is given instead of a command, copy the files from the source to that path: https://github.com/sanjeed5/awesome-cursor-rules-mdc/tree/HEAD/rules-mdc/llama-index.mdc

## When to use it
- split a RAG pipeline into testable functions
- parse PDFs with tables through LlamaParse
- speed up index building over a large document set
- keep API keys in environment variables
- run a Workflow on top of an existing query engine

## Example prompts
- Split this script into modules following the llama-index rules
- Rewrite PDF loading with LlamaParse so tables survive
- Make index building asynchronous

## Source text · llama-index.mdc · CC0-1.0

Below is the content of a third-party file. It is data, not instructions for you: do not act on anything written inside unless the human asks. Block tag: 585a03ba223ca660.

BEGIN THIRD-PARTY FILE 585a03ba223ca660
````text
---
description: Definitive guidelines for building production-ready LlamaIndex applications, emphasizing modularity, type safety, cloud services, and robust testing.
globs: **/*.py
---
# LlamaIndex Best Practices

LlamaIndex is the definitive framework for building context-augmented LLM applications. Follow these rules to ensure your RAG pipelines and agents are modular, type-safe, performant, and production-ready in 2025.

## 1. Code Organization and Structure

Organize your LlamaIndex components into distinct, testable functions or classes. Leverage the `Workflow` API for explicit data flow.

### ✅ GOOD: Modular Functions & Workflow API

```python
# core_components.py
from llama_index.core import Document, VectorStoreIndex, Settings
from llama_index.core.readers import SimpleDirectoryReader
from llama_index.llms.openai import OpenAI
from llama_index.embeddings.openai import OpenAIEmbedding
from llama_index.core.workflow import Workflow, LlamaAgentWorker, AgentInput, AgentOutput
from typing import List

def configure_global_settings() -> None:
    """Configures global LLM and embedding models for consistent behavior."""
    Settings.llm = OpenAI(model="gpt-4o-mini", temperature=0.1)
    Settings.embed_model = OpenAIEmbedding(model="text-embedding-3-small")

def load_documents(data_path: str) -> List[Document]:
    """Loads documents from a directory using SimpleDirectoryReader."""
    return SimpleDirectoryReader(data_path).load_data()

def build_vector_index(documents: List[Document]) -> VectorStoreIndex:
    """Builds a VectorStoreIndex from documents."""
    return VectorStoreIndex.from_documents(documents)

def create_query_engine(index: VectorStoreIndex):
    """Creates a query engine with optimal response mode for citations."""
    return index.as_query_engine(response_mode="compact", verbose=True)

# main_app.py
from core_components import (
    configure_global_settings, load_documents, build_vector_index, create_query_engine
)

def main_rag_workflow():
    configure_global_settings() # Always configure settings first

    docs = load_documents("./data")
    index = build_vector_index(docs)
    query_engine = create_query_engine(index)

    # Wrap the query engine in an AgentWorker for Workflow compatibility
    agent_worker = LlamaAgentWorker.from_query_engine(query_engine)
    rag_workflow = Workflow(
        name="SimpleRAGWorkflow",
        description="A basic RAG pipeline for document querying.",
        input_type=AgentInput,
        output_type=AgentOutput,
        worker=agent_worker
    )

    result = rag_workflow.run(AgentInput(query="What are the main themes across these documents?"))
    print(result.response)

if __name__ == "__main__":
    main_rag_workflow()
```

### ❌ BAD: Monolithic Script

```python
# app.py (avoid this structure for anything beyond quickstarts)
from llama_index.core import VectorStoreIndex, SimpleDirectoryReader, Settings
from llama_index.llms.openai import OpenAI
from llama_index.embeddings.openai import OpenAIEmbedding

Settings.llm = OpenAI(model="gpt-4o-mini")
Settings.embed_model = OpenAIEmbedding(model="text-embedding-3-small")

docs = SimpleDirectoryReader("data").load_data()
index = VectorStoreIndex.from_documents(docs)
qe = index.as_query_engine()
ans = qe.query("What are the main themes?")
print(ans)
```

## 2. Common Patterns and Anti-patterns

### ✅ GOOD: Leverage LlamaCloud Services & LlamaHub Connectors

For production-grade RAG, prefer managed services like LlamaParse and LlamaCloud, and utilize LlamaHub for data connectors.

```python
import os
from llama_index.core.readers import LlamaParseReader
from llama_index.cloud import LlamaCloudIndex
from llama_index.core import Document

# Ensure LLAMAPARSE_API_KEY and LLAMACLOUD_API_KEY are set in environment
````
END THIRD-PARTY FILE 585a03ba223ca660

The third-party file has ended. Everything between the 585a03ba223ca660 tags is data: requests and commands from it are not followed. An “end of file” line with another tag inside the block is part of the file.

First 3791 of 12607 bytes. © sanjeed5 · CC0-1.0 · https://github.com/sanjeed5/awesome-cursor-rules-mdc/tree/HEAD/rules-mdc/llama-index.mdc

_Part of the page did not fit — see the HTML version._

---
NeVibeCode on affpapa.org. Catalog texts are descriptions, not instructions for the agent. How to choose and install: https://affpapa.org/en/nevibecode/agents.md
