# LlamaIndex cost tracking

LlamaIndex reports each finished LLM call to its instrumentation dispatcher. Veqen's event handler listens there, so every call is priced whichever LLM class made it, with no change to your indexes, query engines or agents.

## Install

```bash
pip install "veqen[llamaindex]"
```

## Record every call

```python
import veqen
from llama_index.core.instrumentation import get_dispatcher
from veqen.integrations.llamaindex import VeqenEventHandler

veqen.init()  # reads VEQEN_API_KEY, an ingest key
get_dispatcher().add_event_handler(VeqenEventHandler(tags={"feature": "docs-search"}))

with veqen.tags(customer=org.id):
    answer = query_engine.query(question)
```

Register the handler once at startup, on the root dispatcher. Tags set with `veqen.tags(...)` around a query apply to every LLM call inside it.

## What it reads

The usage in the provider's own response, which LlamaIndex keeps on each chat and completion response: OpenAI and OpenAI-compatible hosts, Anthropic, and Google GenAI, with cached input kept apart from fresh input. For an OpenAI-compatible host, name it so the call is priced and billed as that host's:

```python
VeqenEventHandler(provider="together", tags={"feature": "rag"})
```

Responses that carry no usage, such as a stream with usage reporting off, are skipped rather than estimated. Embedding calls don't report token counts to LlamaIndex's events, so record those with [`veqen.record()`](/docs/python) or wrap the embedding client.

## Next

- [Tags](/docs/tags): customer, feature, user, project, environment and run.
- [Custom prices](/docs/custom-prices): models you host yourself behind LlamaIndex.
