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

# LlamaIndex

> Trace LlamaIndex RAG pipelines with Netra auto-instrumentation. Monitor query engines, retrievers, embeddings, and LLM calls in every pipeline run.

<img src="https://mintcdn.com/netra/u6ajHWd7ki_9CRWQ/images/integration-logos/ai-frameworks/llamaindex.png?fit=max&auto=format&n=u6ajHWd7ki_9CRWQ&q=85&s=38b21b2a1746003a532700262a4893f6" alt="LlamaIndex" width="509" height="80" data-path="images/integration-logos/ai-frameworks/llamaindex.png" />

## Installation

Install both the Netra SDK and LlamaIndex:

<CodeGroup>
  ```bash Python theme={null}
  pip install netra-sdk llama-index
  ```

  ```bash Typescript theme={null}
  npm install netra-sdk llamaindex
  ```
</CodeGroup>

## Usage

Initialize the Netra SDK to automatically trace all LlamaIndex operations:

<CodeGroup>
  ```python Python theme={null}
  from netra import Netra
  from llama_index.core import VectorStoreIndex, Document
  import os

  # Initialize Netra
  Netra.init(
      headers=f"x-api-key={os.environ.get('NETRA_API_KEY')}",
      trace_content=True
  )

  # Use LlamaIndex as normal - automatically traced
  documents = [
      Document(text="LlamaIndex is a data framework for LLM applications.")
  ]
  index = VectorStoreIndex.from_documents(documents)
  query_engine = index.as_query_engine()
  response = query_engine.query("What is LlamaIndex?")
  ```

  ```typescript Typescript theme={null}
  import { Netra } from "netra-sdk";
  import { VectorStoreIndex, Document } from "llamaindex";

  // Initialize Netra
  await Netra.init({
    headers: `x-api-key=${process.env.NETRA_API_KEY}`,
    traceContent: true
  });

  // Use LlamaIndex as normal - automatically traced
  const documents = [
    new Document({ text: "LlamaIndex is a data framework for LLM applications." })
  ];
  const index = await VectorStoreIndex.fromDocuments(documents);
  const queryEngine = index.asQueryEngine();
  const response = await queryEngine.query("What is LlamaIndex?");
  ```
</CodeGroup>

### Core Features

Trace indexing and retrieval workflows:

<CodeGroup>
  ```python Python theme={null}
  from netra.decorators import workflow, task
  from netra import SpanWrapper

  @workflow()
  def build_rag_pipeline(documents: list[Document]):
      index_span = SpanWrapper("build-index", {
          "documents.count": len(documents)
      }).start()
      
      index = VectorStoreIndex.from_documents(documents)
      index_span.end()
      
      return index

  @task()
  def query_with_retrieval(query_engine, question: str):
      query_span = SpanWrapper("query-execution", {
          "query.text": question
      }).start()
      
      response = query_engine.query(question)
      query_span.set_attribute("response.sources", len(response.source_nodes or []))
      query_span.end()
      
      return response
  ```

  ```typescript Typescript theme={null}
  import { workflow, task, SpanWrapper } from "netra-sdk";

  @workflow()
  async function buildRAGPipeline(documents: Document[]) {
    const indexSpan = new SpanWrapper("build-index", {
      "documents.count": documents.length
    }).start();
    
    const index = await VectorStoreIndex.fromDocuments(documents);
    indexSpan.end();
    
    return index;
  }

  @task()
  async function queryWithRetrieval(queryEngine: any, question: string) {
    const querySpan = new SpanWrapper("query-execution", {
      "query.text": question
    }).start();
    
    const response = await queryEngine.query(question);
    querySpan.setAttribute("response.sources", response.sourceNodes?.length || 0);
    querySpan.end();
    
    return response;
  }
  ```
</CodeGroup>

### Query Examples

Trace complex query patterns:

<CodeGroup>
  ```python Python theme={null}
  from netra.decorators import agent
  from netra import SpanWrapper

  @agent()
  def multi_step_query(index: VectorStoreIndex, queries: list[str]):
      query_engine = index.as_query_engine()
      results = []
      
      for query in queries:
          span = SpanWrapper(f"query-{query}", {
              "query.text": query
          }).start()
          
          response = query_engine.query(query)
          span.set_attribute("response.text", str(response))
          span.end()
          
          results.append(response)
      
      return results
  ```

  ```typescript Typescript theme={null}
  import { agent } from "netra-sdk";

  @agent()
  async function multiStepQuery(index: VectorStoreIndex, queries: string[]) {
    const queryEngine = index.asQueryEngine();
    const results = [];
    
    for (const query of queries) {
      const span = new SpanWrapper(`query-${query}`, {
        "query.text": query
      }).start();
      
      const response = await queryEngine.query(query);
      span.setAttribute("response.text", response.toString());
      span.end();
      
      results.push(response);
    }
    
    return results;
  }
  ```
</CodeGroup>

### Streaming Responses

Trace streaming query responses:

<CodeGroup>
  ```python Python theme={null}
  from netra.decorators import task

  @task()
  def stream_query(query_engine, question: str):
      streaming_response = query_engine.query(question, streaming=True)
      
      for chunk in streaming_response.response_gen:
          print(chunk, end="", flush=True)
  ```

  ```typescript Typescript theme={null}
  import { task } from "netra-sdk";

  @task()
  async function streamQuery(queryEngine: any, question: string) {
    const streamingResponse = await queryEngine.query(question, {
      streaming: true
    });
    
    for await (const chunk of streamingResponse) {
      process.stdout.write(chunk);
    }
  }
  ```
</CodeGroup>

## Next Steps

* [Quick Start Guide](https://docs.getnetra.ai/quick-start/python) - Complete setup and configuration
* [Decorators](https://docs.getnetra.ai/tracing/decorators) - Add custom tracing with `@workflow`, `@agent`, and `@task` decorators
* [Session Tracking](https://docs.getnetra.ai/tracing/session) - Track user sessions and conversations
* [LlamaIndex Documentation](https://docs.llamaindex.ai/) - Official LlamaIndex documentation
