TokenPolice
Docs
Integrations

LlamaIndex

LlamaIndex needs the Python extra, or — on native ESM in Node — the per-provider modules passed to instrumentModules.

Python

Install the llamaindex extra and init as usual — see init parameters. The per-provider LlamaIndex LLMs are metered, and tool calls get their own steps in the trace.

pip install "token-police[llamaindex]"

Node

LlamaIndex works on the base install with CommonJS. On native ESM, import order can leave the per-provider LlamaIndex modules un-instrumented — pass them explicitly under instrumentModules.llamaIndex:

import * as tp from "token-police";
import * as openaiModule from "@llamaindex/openai";
import * as anthropicModule from "@llamaindex/anthropic";

tp.init({
  /* … see init parameters … */
  instrumentModules: {
    llamaIndex: { openaiModule, anthropicModule },
  },
});

Pass only the provider modules you use — openaiModule, anthropicModule, geminiModule. Each one is a namespace import.

On CommonJS (require) you don't need instrumentModules at all. See Node & ESM for when import order matters.

A startup line you can ignore

Node apps that have @llamaindex/* installed may print llamaindex was already imported. This breaks constructor checks and will lead to issues! once when init() runs. Token Police prepares both module formats of each LlamaIndex provider package so it can instrument whichever one your app uses, and LlamaIndex flags the second load. Your app only ever runs through its own copy, so nothing is affected: metering, tool steps and blocking are unchanged.

Reroute does not switch the model on LlamaIndex

A Reroute rule does not switch the model on LlamaIndex — those calls still run on the original model. Metering and blocking work normally. See Actions.

Next

On this page