Tracing and Observability for LlamaIndex with Agenta
Learn how to monitor your LlamaIndex applications using Agenta's observability platform. Get complete visibility into your document search, RAG pipelines, and LLM interactions.
What is Agenta? Agenta is an open-source LLMOps platform designed to streamline the deployment, management, and scaling of large language models. It offers comprehensive observability, testing, and deployment capabilities for AI applications.
What is LlamaIndex? LlamaIndex (GitHub) is a powerful data framework that connects LLMs with your private data sources. It simplifies working with various data formats (PDFs, APIs, databases, documents) and creates searchable indices for context-aware AI applications.
1. Install Required Packages
Install the necessary dependencies for the integration:
pip install agenta llama_index openinference-instrumentation-llama_index
What each package does:
agenta
: Core SDK for observability and prompt managementllama_index
: Framework for building data-aware LLM applicationsopeninference-instrumentation-llama_index
: Automatic tracing for LlamaIndex operations
2. Configure Your Environment
Set up your API credentials and initialize Agenta:
import os
import agenta as ag
# Set your Agenta credentials
os.environ["AGENTA_API_KEY"] = "your_agenta_api_key"
os.environ["AGENTA_HOST"] = "https://cloud.agenta.ai" # Use your self-hosted URL if applicable
# Initialize Agenta SDK
ag.init()
3. Enable Automatic Tracing
Activate monitoring for all LlamaIndex operations:
from openinference.instrumentation.llama_index import LlamaIndexInstrumentor
# Start automatic instrumentation
LlamaIndexInstrumentor().instrument()
Once this is set up, Agenta will automatically capture:
- Document loading and processing steps
- Vector index creation and updates
- Search queries and retrieval operations
- LLM calls for response generation
Build Your Instrumented Application
Here's a complete example of a LlamaIndex application with Agenta instrumentation:
import os
import agenta as ag
from openinference.instrumentation.llama_index import LlamaIndexInstrumentor
from llama_index.core import VectorStoreIndex, SimpleDirectoryReader
# Configuration setup
os.environ["AGENTA_API_KEY"] = "your_agenta_api_key"
os.environ["AGENTA_HOST"] = "https://cloud.agenta.ai" # Optional, defaults to the Agenta cloud API
# Start Agenta observability
ag.init()
# Enable LlamaIndex instrumentation
LlamaIndexInstrumentor().instrument()
@ag.instrument()
def document_search_app(user_query: str):
"""
Document search application using LlamaIndex.
Loads documents, builds a searchable index, and answers user queries.
"""
# Load documents from local directory
docs = SimpleDirectoryReader("data").load_data()
# Build vector search index
search_index = VectorStoreIndex.from_documents(docs)
# Initialize query processor
query_processor = search_index.as_query_engine()
# Process user query
answer = query_processor.query(user_query)
return answer
# Run the application
if __name__ == "__main__":
result = document_search_app("What is Agenta?")
print(f"Answer: {result}")
Understanding Span Types
The @ag.instrument()
decorator helps organize your traces by categorizing different operations. Use the spankind
parameter to classify your functions: