Launch Week 02 wrapped — explore all five launches

LlamaIndex

Use Confident AI for LLM observability and evals for LlamaIndex

Overview

LlamaIndex is an LLM framework that makes it easy to build knowledge agents from complex data. Confident AI allows you to trace and evaluate LlamaIndex agents in just a few lines of code.

Tracing Quickstart

  1. Install Dependencies

    Run the following command to install the required packages:

    pip install -U deepeval llama-index
  2. Setup Confident AI Key

    Login to Confident AI using your Confident API key.

    deepeval login
    import deepeval
    
    deepeval.login("<your-confident-api-key>")
  3. Instrument LlamaIndex

    Call instrument_llama_index once at startup, passing LlamaIndex's root dispatcher. Every subsequent LlamaIndex call in your application will automatically be traced and sent to Confident AI.

    main.py
    import asyncio
    from llama_index.llms.openai import OpenAI
    from llama_index.core.agent import FunctionAgent
    import llama_index.core.instrumentation as instrument
    
    from deepeval.integrations.llama_index import instrument_llama_index
    instrument_llama_index(instrument.get_dispatcher())
    
    def multiply(a: float, b: float) -> float:
        """Useful for multiplying two numbers."""
        return a * b
    
    agent = FunctionAgent(
        tools=[multiply],
        llm=OpenAI(model="gpt-4o-mini"),
        system_prompt="You are a helpful assistant that can perform calculations.",
    )
    
    async def main():
        return await agent.run("What is 3 * 12?")
    
    asyncio.run(main())

    You can directly view the traces on Confident AI by clicking on the link printed in the console output.

What Gets Traced

The integration captures the following span types automatically:

Span typeWhen it is created
AgentAny Workflow.run() or FunctionAgent.run() call
LLMEach LLMChatStartEvent / LLMChatEndEvent pair — includes input messages, model name, and the inferred provider (e.g. OpenAI, Anthropic)
ToolAny call_tool / acall_tool / acall invocation — captures tool name, inputs, and outputs
GenericAll other instrumented LlamaIndex methods

Retrieval context from RetrievalEndEvent is automatically attached to the enclosing span, making it available for retrieval-based metrics.

Each span is tagged with integration: "LlamaIndex" so you can filter by framework on Confident AI.

Advanced Features

Set trace attributes

You can attach metadata, user identifiers, and other attributes to a trace by wrapping your LlamaIndex call inside the trace context manager.

main.py
import asyncio
from llama_index.core.agent import FunctionAgent
from llama_index.llms.openai import OpenAI
import llama_index.core.instrumentation as instrument
from deepeval.integrations.llama_index import instrument_llama_index
from deepeval.tracing import trace

instrument_llama_index(instrument.get_dispatcher())

agent = FunctionAgent(
    tools=[],
    llm=OpenAI(model="gpt-4o-mini"),
    system_prompt="You are a helpful assistant.",
)

async def handle_request(user_input: str, user_id: str, thread_id: str):
    with trace(
        user_id=user_id,
        thread_id=thread_id,
        tags=["production", "v2"],
        metadata={"environment": "prod"},
    ):
        return await agent.run(user_input)

asyncio.run(handle_request("Hello!", user_id="user-42", thread_id="conv-99"))
View Trace Attributes

namestr

The name of the trace. Learn more.

tagsList[str]

Tags are string labels that help you group related traces. Learn more.

metadataDict

Attach arbitrary metadata to the trace. Learn more.

thread_idstr

Supply the thread or conversation ID to view and evaluate conversations. Learn more.

user_idstr

Supply the user ID to enable user analytics. Learn more.

inputAny

Override the top-level input recorded for this trace.

outputAny

Override the top-level output recorded for this trace.

retrieval_contextList[str]

Explicitly set the retrieval context for this trace.

contextList[str]

Contextual information available to the model at inference time.

expected_outputstr

The expected or ground-truth output for this trace.

tools_calledList[ToolCall]

Manually specify the tools called during this trace.

expected_toolsList[ToolCall]

The expected tools that should have been called.

Evals Usage

Online evals

You can run online evals on your LlamaIndex application to evaluate all incoming traces on Confident AI's servers. This approach is recommended when your application is in production.

  1. Create metric collection

    Create a metric collection on Confident AI with the metrics you wish to use to evaluate your LlamaIndex application.

    Create metric collection
  2. Run evals

    Pass metric_collection to the trace context manager to evaluate the entire trace with your chosen metric collection.

    main.py
    import asyncio
    from llama_index.llms.openai import OpenAI
    from llama_index.core.agent import FunctionAgent
    import llama_index.core.instrumentation as instrument
    from deepeval.integrations.llama_index import instrument_llama_index
    from deepeval.tracing import trace
    
    instrument_llama_index(instrument.get_dispatcher())
    
    def multiply(a: float, b: float) -> float:
        """Useful for multiplying two numbers."""
        return a * b
    
    agent = FunctionAgent(
        tools=[multiply],
        llm=OpenAI(model="gpt-4o-mini"),
        system_prompt="You are a helpful assistant.",
    )
    
    async def llm_app(user_input: str):
        with trace(metric_collection="my_metric_collection"):
            return await agent.run(user_input)
    
    asyncio.run(llm_app("What is 3 * 12?"))

Span-level evals

For finer-grained control, you can attach metrics or a metric collection directly to individual Agent or LLM spans using AgentSpanContext or LlmSpanContext. This lets you evaluate specific spans independently.

main.py
import asyncio
from llama_index.llms.openai import OpenAI
from llama_index.core.agent import FunctionAgent
import llama_index.core.instrumentation as instrument
from deepeval.integrations.llama_index import instrument_llama_index
from deepeval.tracing import trace
from deepeval.tracing.trace_context import AgentSpanContext
from deepeval.metrics import AnswerRelevancyMetric

instrument_llama_index(instrument.get_dispatcher())

def multiply(a: float, b: float) -> float:
    """Useful for multiplying two numbers."""
    return a * b

agent = FunctionAgent(
    tools=[multiply],
    llm=OpenAI(model="gpt-4o-mini"),
    system_prompt="You are a helpful assistant that can perform calculations.",
)

answer_relevancy = AnswerRelevancyMetric()

async def llm_app(user_input: str):
    agent_span_context = AgentSpanContext(
        metrics=[answer_relevancy],
    )
    with trace(agent_span_context=agent_span_context):
        return await agent.run(user_input)

asyncio.run(llm_app("What is 3 * 12?"))
View Span Context Parameters

AgentSpanContext — applied to Agent spans (i.e. workflow / agent .run() calls):

metricsList[BaseMetric]

A list of DeepEval metric instances to evaluate this agent span with.

metric_collectionstr

Name of a metric collection on Confident AI to use for evaluation.

expected_outputstr

The expected output for the agent span.

expected_toolsList[ToolCall]

The expected tools that should have been called.

contextList[str]

Contextual information for the span.

retrieval_contextList[str]

Retrieved documents or chunks for the span.


LlmSpanContext — applied to LLM spans (i.e. individual LLM calls):

metricsList[BaseMetric]

A list of DeepEval metric instances to evaluate this LLM span with.

metric_collectionstr

Name of a metric collection on Confident AI to use for evaluation.

promptPrompt

A Prompt object from deepeval.prompt to associate a managed prompt with this LLM span.

expected_outputstr

The expected output for the LLM span.

expected_toolsList[ToolCall]

The expected tools that should have been called.

contextList[str]

Contextual information for the span.

retrieval_contextList[str]

Retrieved documents or chunks for the span.

View on Confident AI

You can view the evals on Confident AI by clicking on the link in the output printed in the console.

Need help wiring this into your stack?Bring traces and evals into the tools your team already usesTalk to an expert
Built byConfident AI