SDK Quickstart

Instrument a Python LLM application with the Revefi LLM SDK in a few lines — OpenAI, Anthropic, Gemini, and LangChain calls are tracked automatically.

Overview

The Revefi LLM SDK is a minimal Python package built on OpenLLMetry / Traceloop and OpenTelemetry. A single init_llm_observability() call auto-instruments your LLM client libraries — every request and response is captured as a span and streamed to Revefi.

Supported providers / frameworks: OpenAI, Anthropic, Google Gemini (Generative AI), and LangChain.

Prerequisites

  • Python 3.8+

1. Install

pip install revefi-llm-sdk

2. Initialize

Call init_llm_observability() once at startup, before you create or call any LLM client. It configures the OpenTelemetry exporter and turns on auto-instrumentation.

from revefi_llm_sdk import init_llm_observability

init_llm_observability(
    api_key="<REVEFI_API_TOKEN>",       # your Revefi API access token
    service_name="my-llm-app",           # identifies this application in Revefi
    ingestor_url="https://gateway.revefi.com",
)

# From here on, LLM calls are automatically traced:
from openai import OpenAI

client = OpenAI()
response = client.chat.completions.create(
    model="gpt-4o-mini",
    messages=[{"role": "user", "content": "Hello!"}],
)

Parameters

ParameterRequiredDescription
api_keyYesRevefi API access token. Sent as Authorization: Bearer <api_key>.
service_nameYesLogical name for your application/agent. Appears as service.name on every span.
ingestor_urlNoBase URL of the Revefi ingestion gateway. The SDK appends /api/v1/traces/ingest. Defaults to http://localhost:3000 (local dev). Set this to your Revefi gateway URL in production (see Endpoints).

init_llm_observability() returns True on success and False if initialization failed (e.g. bad configuration) — it never raises, so check the return value if you want to fail fast.

3. Add context and custom tags (optional)

Use set_context() to attach a user identifier, an agent name, and arbitrary custom tags to subsequent spans. Custom tags become first-class dimensions in Revefi — you can filter and group cost/usage charts by any of them (e.g. environment, team, feature, tenant_id).

from revefi_llm_sdk import set_context

set_context(
    user_id="user-123",
    agent_name="support-bot",
    environment="production",   # custom tag
    team="growth",               # custom tag
)

Call set_context() whenever the context changes (for example, per request in a web handler) — it applies to all spans emitted afterward on that execution.

📌

Custom tag limits

A maximum of 50 custom tags may be set. Each key must be a non-empty string of ≤ 64 characters, and each value a non-empty string of ≤ 128 characters. Invalid tags raise a ValueError.

Endpoints

Pass the base URL (no path) as ingestor_url:

Environmentingestor_url
Productionhttps://gateway.revefi.com
AUhttps://au.gateway.revefi.com

Full example

import os
from revefi_llm_sdk import init_llm_observability, set_context
from openai import OpenAI

# 1. Initialize once at startup
ok = init_llm_observability(
    api_key=os.environ["REVEFI_API_TOKEN"],
    service_name="my-llm-app",
    ingestor_url="https://gateway.revefi.com",
)
if not ok:
    raise RuntimeError("Failed to initialize Revefi LLM observability")

# 2. Attach context for this request
set_context(user_id="user-123", agent_name="support-bot", environment="production")

# 3. Make LLM calls as usual — they are traced automatically
client = OpenAI()
resp = client.chat.completions.create(
    model="gpt-4o-mini",
    messages=[{"role": "user", "content": "Summarize today's incidents."}],
)
print(resp.choices[0].message.content)

Within a minute or two your spans should appear in the Revefi AI Observability dashboards.

Troubleshooting

  • init returns False — check application logs; the SDK logs the failure reason. A common cause is an unreachable ingestor_url.
  • Short-lived scripts — the exporter batches spans. Let the process exit cleanly (or flush) so the final batch is sent before shutdown.

Not using Python or these providers?

Send traces directly over OTLP — see Direct Trace Ingestion.


What’s Next

Add custom tags with set_context to break down cost and usage by user, team, or environment. See Span Structure for the full attribute reference.

Did this page help you?