Skip to main content

Context0 SDK — one import, all calls logged

Project description

Context0 — Python SDK

Drop-in replacements for OpenAI, Azure OpenAI, and Anthropic clients. One import change, all LLM calls logged automatically.

Quick Start

from llm_observatory import configure, OpenAI

configure(api_key="sk-proj-xxx")

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

Supported Clients

from llm_observatory import (
    OpenAI,            # drop-in for openai.OpenAI
    AsyncOpenAI,       # drop-in for openai.AsyncOpenAI
    AzureOpenAI,       # drop-in for openai.AzureOpenAI
    AsyncAzureOpenAI,  # drop-in for openai.AsyncAzureOpenAI
    Anthropic,         # drop-in for anthropic.Anthropic
    AsyncAnthropic,    # drop-in for anthropic.AsyncAnthropic
)

All clients support chat.completions.create() (OpenAI/Azure) or messages.create() (Anthropic), including streaming. OpenAI clients also support responses.create().

Streaming

for chunk in client.chat.completions.create(
    model="gpt-4o",
    messages=[{"role": "user", "content": "Hello"}],
    stream=True,
):
    print(chunk.choices[0].delta.content, end="")

Streaming captures time_to_first_token_ms and tokens_per_second automatically.

Metadata

Attach metadata at the client level or per-request:

# Client-level — applies to all subsequent calls
client.set_metadata(user_id="u-123", session_id="s-456", tags=["prod"], custom={"team": "ml"})

# Per-request — merged with client-level metadata
response = client.chat.completions.create(
    model="gpt-4o",
    messages=[...],
    metadata={"user_id": "u-789"},  # overrides client-level user_id
)

Configuration

configure(
    api_key="sk-proj-xxx",          # or set LLM_OBSERVATORY_API_KEY
    base_url="http://localhost:8080", # or set LLM_OBSERVATORY_BASE_URL
    enabled=True,                    # or set LLM_OBSERVATORY_ENABLED=false to disable
    redact_messages=False,           # redact all message content from logged events
    flush_interval=5.0,             # seconds between batch flushes
    flush_batch_size=50,            # max events per batch
    max_queue_size=1000,            # max queued events (oldest dropped on overflow)
)

Environment Variables

Variable Description
LLM_OBSERVATORY_API_KEY API key (used if not passed to configure())
LLM_OBSERVATORY_BASE_URL Ingestion endpoint URL
LLM_OBSERVATORY_ENABLED Set to false to disable all logging

Flushing

Events are batched and sent asynchronously in a background thread. To force-flush:

client.flush()  # blocks until all buffered events are sent

Design Principles

  • Not in the critical path. LLM calls go directly to the provider. Logging is async in a background thread.
  • Never crashes user code. Event capture is try/catch wrapped. Exceptions from the LLM provider are re-raised unchanged.
  • Transparent. Same interface as the original clients. Returns original responses unchanged.

Structure

sdk-python/
├── pyproject.toml
├── src/llm_observatory/
│   ├── __init__.py          # Public API: configure, OpenAI, Anthropic, etc.
│   ├── client.py            # Client wrappers (OpenAI, Azure, Anthropic)
│   ├── streaming.py         # Stream iterator wrappers
│   ├── transport.py         # Async batch flusher (daemon thread)
│   ├── event.py             # Event schema + redaction
│   └── config.py            # Configuration
└── tests/

Install (dev)

cd sdk-python
pip install -e ".[dev]"
pytest tests/ -v

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_observatory-0.1.0.tar.gz (29.0 kB view details)

Uploaded Source

Built Distribution

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

llm_observatory-0.1.0-py3-none-any.whl (15.9 kB view details)

Uploaded Python 3

File details

Details for the file llm_observatory-0.1.0.tar.gz.

File metadata

  • Download URL: llm_observatory-0.1.0.tar.gz
  • Upload date:
  • Size: 29.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.11.10

File hashes

Hashes for llm_observatory-0.1.0.tar.gz
Algorithm Hash digest
SHA256 4e38ad7f6ea322d4f882aed928fc330789ce32b30b5adbfa0584a7ac75285fc4
MD5 bc0df6843f96ae31c37157d804c90a8b
BLAKE2b-256 8f37a50e858f4cf3b3c6733c02f4aa8163f98f080be7eaed3eea6d0e87fbf43b

See more details on using hashes here.

File details

Details for the file llm_observatory-0.1.0-py3-none-any.whl.

File metadata

File hashes

Hashes for llm_observatory-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 ad83a5bf3dfeaf326027f69c2aed1ddda009dcdd5854760a47875844835e12b6
MD5 f7c60c65a02bc13cba690603e8653164
BLAKE2b-256 1047923f2d50c818fee78f5d116a28cb8d70fd999ffd15dee26dd4a10c3ada11

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