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

# LlamaIndex

> Trace LlamaIndex LLM calls, tool use, and agent steps with Zespan using the callback handler.

<Note>
  Available for: **Python** and **TypeScript**.
</Note>

<Tabs>
  <Tab title="Python">
    ## Installation

    ```bash theme={"theme":{"light":"github-light","dark":"one-dark-pro"}}
    pip install zespan llama-index
    ```

    ## Usage

    Register `ZespanLlamaIndexCallbackHandler` with LlamaIndex's global callback manager.

    ```python theme={"theme":{"light":"github-light","dark":"one-dark-pro"}}
    import zespan
    from zespan import ZespanLlamaIndexCallbackHandler
    from llama_index.core import Settings
    from llama_index.core.callbacks import CallbackManager

    zespan.init(api_key="zsp_your_api_key_here")

    handler = ZespanLlamaIndexCallbackHandler()
    Settings.callback_manager = CallbackManager([handler])
    ```

    All LlamaIndex LLM calls, retrievals, and agent steps made after this point are traced automatically.

    ```python theme={"theme":{"light":"github-light","dark":"one-dark-pro"}}
    from llama_index.core import VectorStoreIndex, SimpleDirectoryReader

    documents = SimpleDirectoryReader("data").load_data()
    index = VectorStoreIndex.from_documents(documents)
    query_engine = index.as_query_engine()

    response = query_engine.query("What are the key points?")
    print(response)
    ```
  </Tab>

  <Tab title="TypeScript">
    ## Installation

    ```bash theme={"theme":{"light":"github-light","dark":"one-dark-pro"}}
    npm install @zespan/sdk llamaindex
    ```

    ## Usage

    Create a `ZespanLlamaIndexHandler` and attach it to your LlamaIndex callback manager.

    ```typescript theme={"theme":{"light":"github-light","dark":"one-dark-pro"}}
    import { ZespanLlamaIndexHandler, zespan } from "@zespan/sdk";
    import { Settings } from "llamaindex";

    zespan.init({ apiKey: process.env.ZESPAN_API_KEY! });

    const handler = new ZespanLlamaIndexHandler();
    handler.attach(Settings.callbackManager);
    ```

    All LlamaIndex LLM calls, tool use, and agent steps are traced automatically after attaching.

    ```typescript theme={"theme":{"light":"github-light","dark":"one-dark-pro"}}
    import { VectorStoreIndex, SimpleDirectoryReader } from "llamaindex";

    const documents = await new SimpleDirectoryReader().loadData({ directoryPath: "./data" });
    const index = await VectorStoreIndex.fromDocuments(documents);
    const queryEngine = index.asQueryEngine();

    const response = await queryEngine.query({ query: "What are the key points?" });
    console.log(response.toString());
    ```

    To stop tracing:

    ```typescript theme={"theme":{"light":"github-light","dark":"one-dark-pro"}}
    handler.detach(Settings.callbackManager);
    ```
  </Tab>
</Tabs>
