LlamaIndex Integration

Trace LlamaIndex queries, LLM calls, retriever operations, and synthesis steps using a callback handler.

Installation

pip install twosignal[llamaindex]

Usage

from twosignal import TwoSignal
from twosignal.wrappers.llamaindex import TwoSignalHandler
from llama_index.core import Settings, VectorStoreIndex, SimpleDirectoryReader
from llama_index.core.callbacks import CallbackManager

ts = TwoSignal()
handler = TwoSignalHandler()
Settings.callback_manager = CallbackManager([handler])

# load and index documents
documents = SimpleDirectoryReader("data").load_data()
index = VectorStoreIndex.from_documents(documents)

# every query is now traced
query_engine = index.as_query_engine()
response = query_engine.query("What is 2Signal?")

What Gets Traced

LlamaIndex EventSpan TypeDetails
LLM callLLMModel, prompt, completion, tokens, cost
RetrievalRETRIEVALQuery, retrieved nodes
Query / SynthesisCHAINQuery input, synthesized output
EmbeddingCHAINEmbedding operation

With Chat Engine

chat_engine = index.as_chat_engine()

# conversation turns are traced
response = chat_engine.chat("Hello")
response = chat_engine.chat("Tell me more")

How It Works

The TwoSignalHandler implements LlamaIndex's callback interface (on_event_start / on_event_end). It automatically captures LLM calls with token usage and cost, retriever queries with returned documents, and synthesis steps. Spans nest hierarchically to match the LlamaIndex execution tree.

Have questions? Join our community!

Connect with other developers and the 2Signal team.

Join Discord