Skip to main content

Lightweight LLM observability SDK

Project description

LLM Watch SDK

Lightweight SDK to send LLM usage telemetry to the LLM Watch backend.

Install

pip install llm-watch-sdk

Quickstart

LLM Watch reads config from env by default:

LLMWATCH_BACKEND_URL=http://127.0.0.1:8000
LLMWATCH_API_KEY=YOUR_PROJECT_KEY
from llm_watch import LLMWatch

watch = LLMWatch()  # reads env

You can still pass backend_url and project_api_key explicitly.

from openai import OpenAI

client = OpenAI(api_key="OPENAI_KEY")
llm = watch.instrument_openai(client, model="gpt-4o-mini")

# trace() is optional; it groups related calls
with watch.trace(actor_id="user-42"):
    resp = llm.invoke({
        "messages": [{"role": "user", "content": "hello"}],
    })

Providers

Instrument a provider client, then use the same .invoke(...) pattern across providers.

Pattern When to use Example
Instrument + invoke Recommended for new integrations llm = watch.instrument_openai(client, model="gpt-4o-mini"); llm.invoke({...})
Drop-in wrapper Keep existing call sites client = watch.wrap_openai(client, model="gpt-4o-mini")
# OpenAI (recommended unified invoke)
from openai import OpenAI

client = OpenAI(api_key="...")
llm = watch.instrument_openai(client, model="gpt-4o-mini")

with watch.trace(actor_id="user-42"):
    resp = llm.invoke({
        "messages": [{"role": "user", "content": "hello"}],
    })
# Gemini (google.genai)
from google import genai

client = genai.Client(api_key="...")
llm = watch.instrument_gemini(client, model="models/gemini-pro-latest")

with watch.trace(actor_id="user-42"):
    resp = llm.invoke({"contents": "hello"})
# Bedrock (create or wrap a client)
llm = watch.instrument_bedrock(region="eu-north-1", model_id="arn:aws:bedrock:...")

with watch.trace(actor_id="user-42"):
    resp = llm.invoke({
        "messages": [{"role": "user", "content": [{"text": "hello"}]}],
        "inferenceConfig": {"maxTokens": 64},
    })

Optional drop-in wrappers

If you prefer to keep your existing call sites, you can use drop-in wrappers:

# OpenAI drop-in (keeps .chat.completions.create)
client = OpenAI(api_key="...")
client = watch.wrap_openai(client, model="gpt-4o-mini")
client.chat.completions.create(
    model="gpt-4o-mini",
    messages=[{"role": "user", "content": "hello"}],
)
# Bedrock drop-in (wrap existing boto3 client)
import boto3
br = boto3.client("bedrock-runtime", region_name="eu-north-1")
br = watch.instrument_bedrock(client=br, model_id="arn:aws:bedrock:...")
br.invoke_model(
    modelId="arn:aws:bedrock:...",
    body=b'{"messages":[{"role":"user","content":[{"text":"hello"}]}]}',
    contentType="application/json",
    accept="application/json",
)

Manual logging (if you prefer)

watch.log_llm_call(
    provider="openai",
    model="gpt-4o-mini",
    input_tokens=120,
    output_tokens=320,
    total_tokens=440,
    latency_ms=780,
    actor_id="user-42",
)

watch.trace() is optional. If you don’t create a trace, LLM Watch auto-creates one per call. Use trace() to group calls under a single workflow.

Production checklist

  • Store LLMWATCH_API_KEY in your secrets manager (do not hardcode).
  • Set LLMWATCH_BACKEND_URL via environment (per‑environment endpoint).
  • If you skip trace(), each call still logs with an auto trace/span.
  • Add request timeouts and retry policies to your LLM client (SDK never blocks your app).
  • Capture actor context (actor_id, actor_type, actor_name) for accountability.
  • Use one key per project/workspace for clean separation of data.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

llm_watch_sdk-0.1.8.tar.gz (21.4 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

llm_watch_sdk-0.1.8-py3-none-any.whl (30.8 kB view details)

Uploaded Python 3

File details

Details for the file llm_watch_sdk-0.1.8.tar.gz.

File metadata

  • Download URL: llm_watch_sdk-0.1.8.tar.gz
  • Upload date:
  • Size: 21.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.6

File hashes

Hashes for llm_watch_sdk-0.1.8.tar.gz
Algorithm Hash digest
SHA256 6ddf9eb51a03582869537da2bd72770c1e0434152bc74ab6263d2ea128f2e43a
MD5 fabd45ad9b392fb0df805d2710829268
BLAKE2b-256 c09e51999a7aafe7979d376c01885ced64317040da17468a88e6fabb86887142

See more details on using hashes here.

File details

Details for the file llm_watch_sdk-0.1.8-py3-none-any.whl.

File metadata

  • Download URL: llm_watch_sdk-0.1.8-py3-none-any.whl
  • Upload date:
  • Size: 30.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.6

File hashes

Hashes for llm_watch_sdk-0.1.8-py3-none-any.whl
Algorithm Hash digest
SHA256 ad314ac736de762d62f8b5a7196276e7e98e9db667936548bedc23f85174d269
MD5 b4621e5ae793bd4d35c427cdd8d241c3
BLAKE2b-256 f76accd5f04ce5a1aa6fa4cf489911a5f156f3a323139b9bd59ad48def91226c

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page