# Haystack cost tracking

A Haystack component records the cost of each reply from an OpenAI-compatible chat generator. Connect it after the generator; it passes the replies through unchanged.

## Install

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

## Add the component

```python
from haystack import Pipeline
from haystack.components.generators.chat import OpenAIChatGenerator
from veqen.integrations.haystack import VeqenOpenAICostTracker

pipeline = Pipeline()
pipeline.add_component("llm", OpenAIChatGenerator(model="gpt-4o-mini"))
pipeline.add_component("cost", VeqenOpenAICostTracker(tags={"feature": "assistant"}))
pipeline.connect("llm.replies", "cost.replies")

result = pipeline.run({"llm": {"messages": messages}, "cost": {"customer": org.id}})
```

The component's outputs say whether the call was recorded (`recorded`) and why not when it wasn't (`recording_status`). For another OpenAI-compatible host, pass `provider="together"` (or the host's name) so the model is priced at that host.

## Limits

It records after the reply, so it doesn't stop a call that would go over a budget. Use one reply per request (`n=1`): several alternatives can repeat the request's total usage, so they are not summed. Needs Python 3.11 or later.

## Next

- [Python SDK](/docs/python): `wrap()` the client inside your own components instead.
- [Tags](/docs/tags): what each tag is for.
