LlamaIndex is a flexible data framework for building LLM applications:
LlamaIndex is a “data framework” to help you build LLM apps. It provides the following tools:
Comet provides a hosted version of the Opik platform, simply create an account and grab your API Key.
You can also run the Opik platform locally, see the installation guide for more information.
To use the Opik integration with LlamaIndex, you’ll need to have both the opik and llama_index packages installed. You can install them using pip:
Configure the Opik Python SDK for your deployment type. See the Python SDK Configuration guide for detailed instructions on:
opik configureopik.configure()In order to use LlamaIndex, you will need to configure your LLM provider API keys. For this example, we’ll use OpenAI. You can find or create your API keys in these pages:
You can set them as environment variables:
Or set them programmatically:
To use the Opik integration with LLamaIndex, you can use the set_global_handler function from the LlamaIndex package to set the global tracer:
Now that the integration is set up, all the LlamaIndex runs will be traced and logged to Opik.
Alternatively, you can configure the callback handler directly for more control:
The skip_index_construction_trace parameter is useful when you want to track only query operations and not the index construction phase (particularly for large document sets or pre-built indexes)
To showcase the integration, we will create a new a query engine that will use Paul Graham’s essays as the data source.
First step: Configure the Opik integration:
Second step: Download the example data:
Third step:
Configure the OpenAI API key:
Fourth step:
We can now load the data, create an index and query engine:
Given that the integration with Opik has been set up, all the traces are logged to the Opik platform:

The LlamaIndex integration seamlessly works with Opik’s @track decorator. When you call LlamaIndex operations inside a tracked function, the LlamaIndex traces will automatically be attached as child spans to your existing trace.
In this example, Opik will create a trace for the my_llm_application function, and all LlamaIndex operations (like the LLM chat call) will appear as nested spans within this trace, giving you a complete view of your application’s execution.
You can also manually create traces using opik.start_as_current_trace() and have LlamaIndex operations nested within:
This approach is useful when you want more control over trace naming and want to group multiple LlamaIndex operations under a single trace.
LlamaIndex workflows are multi-step processing pipelines for LLM applications. To track workflow executions in Opik, you can manually decorate your workflow steps and use opik.start_as_current_span() to wrap the workflow execution.
You can use @opik.track() to decorate your workflow steps and opik.start_as_current_span() to track the workflow execution:
In this example:
@opik.track() to create spans@step decorator is placed before @opik.track() to ensure LlamaIndex can properly discover the workflow stepsopik.start_as_current_span() tracks the overall workflow executionIf you’re certain the workflow is a top-level call and want to create only a trace without an additional span, you can use opik.start_as_current_trace() instead of opik.start_as_current_span(). However, start_as_current_span() is more flexible as it works in both standalone and nested contexts.
@opik.track() to capture each step as a span@step before @opik.track() so LlamaIndex’s workflow engine can properly discover and execute stepsopik.start_as_current_span() to wrap workflow execution - it works in both standalone and nested contextsopik.flush_tracker() at the end to ensure all traces are sentWhen using streaming chat responses with OpenAI models (e.g., llm.stream_chat()), you need to explicitly enable token usage tracking by configuring the stream_options parameter:
Without setting stream_options={'include_usage': True}, streaming responses from OpenAI models will not include token usage information in Opik traces. This is a requirement of OpenAI’s streaming API.
The Opik integration with LlamaIndex automatically tracks token usage and cost for all supported LLM models used within LlamaIndex applications.
Cost information is automatically captured and displayed in the Opik UI, including:
View the complete list of supported models and providers on the Supported Models page.