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

# LlamaIndex

> Auto-instrument LlamaIndex RAG pipelines and query engines

Automatically capture document ingestion, embedding, retrieval, and LLM synthesis within LlamaIndex pipelines.

## Install

> [tab] Python
>
> ```bash theme={null}
> pip install traceroot llama-index-core llama-index-llms-openai llama-index-embeddings-openai
> ```

## Set up

To capture the complete picture—including both the pipeline structure and the underlying token costs—we highly recommend initializing both the LlamaIndex integration and your specific LLM provider (e.g., OpenAI, Anthropic, Google GenAI).

> [tab] Python
>
> ```python theme={null}
> import traceroot
> from traceroot import Integration
>
> # Initialize LlamaIndex alongside your LLM provider to capture tokens and costs
> traceroot.initialize(integrations=[
>     Integration.LLAMA_INDEX,
>     Integration.OPENAI  # Or ANTHROPIC, GOOGLE_GENAI, etc.
> ])
> ```

## Usage

Once initialized, all LlamaIndex operations are captured automatically:

```python theme={null}
from llama_index.core import Document, Settings, VectorStoreIndex
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")

documents = [
    Document(text="traceroot is an open-source AI agent improvement platform."),
    Document(text="traceroot supports OpenTelemetry-compatible tracing via a Python SDK."),
]

# Index construction is automatically traced (SentenceSplitter, Embedding)
index = VectorStoreIndex.from_documents(documents)
query_engine = index.as_query_engine()

# Each query is automatically traced (retrieval + LLM synthesis)
response = query_engine.query("What is traceroot?")
print(response)
```

## Verify

Run your code and confirm the trace arrives:

1. **In the traceroot UI**: Navigate to **Tracing > Traces** in [traceroot Cloud](https://app.traceroot.ai). You should see a trace with root span `RetrieverQueryEngine.query`, the class and method that `as_query_engine().query()` resolves to.
2. **Via the CLI**: Run `traceroot traces list` to inspect your recent traces from the terminal.

## What gets captured

| Attribute | Description |
| - | - |
| Document chunking | Each `SentenceSplitter` call as a span |
| Embedding calls | Each batch embedding with text inputs and vector outputs |
| Retrieval | Top-k retrieval with query and retrieved documents |
| LLM synthesis | Response generation with context and final answer |
| Tokens & Cost | Input/output tokens; cost when the model ID matches traceroot's pricing table |
| Latency | Duration of each pipeline stage |

## Limitations

* Initializing both `Integration.LLAMA_INDEX` and your underlying LLM provider integration (such as `Integration.OPENAI`) is recommended to capture full token usage and pricing.

## Run the example

Clone the repo and run a complete agent end-to-end.

> [card] [Python](https://github.com/traceroot-ai/traceroot/tree/main/examples/python/llamaindex-rag)
>
> Run the Python example

## Next steps

> [card] [Python SDK Reference](https://traceroot.ai/docs/tracing/python-sdk.md)
>
> Explore the full Python SDK reference and instrumentation options.

> [card] [Cost Tracking](https://traceroot.ai/docs/tracing/cost-tracking.md)
>
> Learn how traceroot calculates token costs and tracks model spend.


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.