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

# LlamaIndex

> Trace LlamaIndex query engines, agents, and retrievers with one function.

## Install

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

  ```bash TypeScript theme={null}
  npm install @trulayer/sdk llamaindex
  ```
</CodeGroup>

## Instrument

<CodeGroup>
  ```python Python theme={null}
  import os
  import trulayer

  trulayer.init(api_key=os.environ["TRULAYER_API_KEY"], project_name="my-app")
  trulayer.instrument_llamaindex()
  ```

  ```typescript TypeScript theme={null}
  import { TruLayer, instrumentLlamaindex } from "@trulayer/sdk";

  const tl = new TruLayer({
    apiKey: process.env.TRULAYER_API_KEY!,
    projectName: "my-app",
  });

  instrumentLlamaindex(tl);
  ```
</CodeGroup>

After instrumentation every query, retriever, LLM call, and agent step is captured as a span — no per-callsite changes needed.

## Minimal example

<CodeGroup>
  ```python Python theme={null}
  from llama_index.core import VectorStoreIndex, SimpleDirectoryReader

  docs = SimpleDirectoryReader("./data").load_data()
  index = VectorStoreIndex.from_documents(docs)
  engine = index.as_query_engine()

  engine.query("What does the onboarding doc say about SSO?")
  # A trace appears in the dashboard with `query`, `retriever`, and `llm` spans.
  ```

  ```typescript TypeScript theme={null}
  import { Document, VectorStoreIndex } from "llamaindex";

  const docs = [new Document({ text: "..." })];
  const index = await VectorStoreIndex.fromDocuments(docs);
  const engine = index.asQueryEngine();

  await engine.query({ query: "What does the onboarding doc say about SSO?" });
  ```
</CodeGroup>

## What gets captured

* `query` spans for every `QueryEngine.query` / `ChatEngine.chat`
* `retriever` spans with the retrieved `NodeWithScore` list attached as output
* `llm` spans for synthesizer calls with prompt, response, and token counts
* `agent` spans for ReAct and OpenAI agents, with each reasoning step as a child span

Embeddings calls used for both indexing and retrieval appear as `embedding` spans with input text and model metadata.
