LlamaIndex cost tracking: route LlamaIndex LLMs and embeddings through an OpenAI-compatible gateway
Use OpenAILike and OpenAIEmbedding to send LlamaIndex queries, agents and ingestion through one LLM gateway. Track RAG cost per feature and per user, use any provider, and cap spend before the call.
LlamaIndex apps spend money in two places: embedding documents (often in large batch jobs) and generating answers. Routing both through the gateway shows them separately and lets you cap each.
Setup
Use OpenAILike, not OpenAI, for non-OpenAI models
LlamaIndex's OpenAI class looks up context sizes by OpenAI model name and raises an error for names it doesn't know, such as claude-sonnet-4-6. OpenAILike takes context_window and capability flags from you instead.
Why split embed and answer costs
Re-indexing a large corpus can cost more in one night than a month of queries. With separate feature tags you can give the ingestion job its own budget, see whether answers or embeddings drive the bill, and spot an ingestion job that re-embeds unchanged documents.
Per-user cost
For a multi-tenant RAG app, set X-Cost-User or X-Cost-Customer per request by building the LLM per request (it is a light object) or by keeping one per tenant. Customer Margin (Growth plan) then shows AI cost per customer next to their revenue.
Related articles