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

# LlamaIndex

> Integrating with LlamaIndex

## Scope of the integration

If you use LlamaIndex with OpenAI or Anthropic as the LLM backend then Baserun will log API calls
generating embeddings and text completions for indexing, querying, retrieval, and response synthesis.
Additionally, Baserun will add logs to a trace with more information on a retrieval e.g.
which nodes were selected and what score they were assigned.

### Example

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


  @baserun.trace
  def llama():
      # load documents
      documents = SimpleDirectoryReader("recipes").load_data()
      # create index
      index = VectorStoreIndex.from_documents(documents)
      # make a query
      query_engine = index.as_query_engine()
      response = query_engine.query("I have flour, sugar and butter. What am I missing if I want to bake oatmeal cookies?")
      print(response)


  if __name__ == "__main__":
      baserun.init()
      llama()
  ```

  ```typescript typescript theme={null}
  import { baserun } from "baserun";
  import fs from "node:fs/promises";
  import {VectorStoreIndex, Document} from "llamaindex";

  async function llama() {
    // load documents
    const dir = "recipes"
    const documents: Document[] = []
    for (const path of await fs.readdir(dir)) {
      const recipe = await fs.readFile(dir + "/" + path, "utf-8");
      const document = new Document({ text: recipe, id_: path });
      documents.push(document)
    }
    // create index
    const index = await VectorStoreIndex.fromDocuments(documents);
    // make a query
    const queryEngine = index.asQueryEngine();
    const response = await queryEngine.query({
      query: "I have flour, sugar and butter. What am I missing if I want to bake oatmeal cookies?",
    });
    console.log(response.toString());
  }

  async function main() {

    await baserun.init();

    await baserun.trace(async () => {
      await llama();
    })();
  }

  main();
  ```
</CodeGroup>

Running this code should result in a trace looking like this:

<Frame>
  <img src="https://mintcdn.com/baserun/sfThaUjl8v9XCswy/images/integrations-llamaindex-trace.png?fit=max&auto=format&n=sfThaUjl8v9XCswy&q=85&s=a682ff33adbce12e6a39454834ae3d96" alt="Trace Details" width="943" height="688" data-path="images/integrations-llamaindex-trace.png" />
</Frame>
