opentoken.init() instruments LlamaIndex: query engines, retrievers, synthesizers and every LLM call with tokens.
1. Install
shell
pip install "getopentoken[llamaindex,bedrock]" llama-index-llms-bedrock-converse2. Environment
Create an ingest key under API keys (scope Ingest) and export it as OT_INGEST_KEY.
.env
# OT_INGEST_KEY: an ingest key from API keys (scope: Ingest). Keep it in your secret store.
export OTEL_EXPORTER_OTLP_ENDPOINT="https://getopentoken.com/api/otel"
export OTEL_SERVICE_NAME="support-agent"3. Observe
app.py
import opentoken
opentoken.init()
from llama_index.core.llms import ChatMessage
from llama_index.llms.bedrock_converse import BedrockConverse
llm = BedrockConverse(model="us.amazon.nova-lite-v1:0", region_name="us-east-1")
with opentoken.context(feature="search"):
reply = llm.chat([ChatMessage(role="user", content="Summarize our refund policy in one sentence.")])
print(reply.message.content)Enforce budgets through the gateway (optional)
llm() returns an OpenAILike model on the gateway with fail-open to Bedrock, usable in any index or query engine:
app.py
import opentoken
from llama_index.core.llms import ChatMessage
from opentoken.adapters.llamaindex import llm
opentoken.init(base_url="https://getopentoken.com/v1")
model = llm("amazon/nova-lite") # OpenAILike on the gateway, with fail-open to Bedrock
with opentoken.context(feature="search"):
reply = model.chat([ChatMessage(role="user", content="Say hello in one sentence.")])
print(reply.message.content)