Skip to main content

LLM usage telemetry SDK for Cloptima reporting, attribution, and analytics

Project description

Cloptima LLM Observability Python SDK

Capture LLM usage telemetry from your application and send it to Cloptima for cost reporting, attribution, and analytics.

Use this SDK when you want visibility into LLM usage without replacing your existing provider clients, retry policies, authentication, or application-level security controls.

Install

pip install cloptima-llm-observability

If you want to use the httpx transport helpers, install the optional extra:

pip install "cloptima-llm-observability[httpx]"

Configuration

Common environment variables:

Variable Required Purpose
CLOPTIMA_LLM_OBSERVABILITY_API_KEY Yes Cloptima API key for telemetry writes
CLOPTIMA_LLM_OBSERVABILITY_APP_ID Yes Application or service identifier
CLOPTIMA_LLM_OBSERVABILITY_ENABLED No Explicitly enable or disable the SDK

Recommended optional environment variables:

Variable Purpose
CLOPTIMA_LLM_OBSERVABILITY_ENVIRONMENT Deployment environment such as dev, staging, or prod. Defaults to production, so set this explicitly when testing outside production.
CLOPTIMA_LLM_OBSERVABILITY_TEAM_ID Team or ownership group

The SDK sends bearer-authenticated HTTPS requests to https://api.cloptima.ai/v1/ai/integrations/sdk/events by default.

Quick start

Use observe_call(...) at the point where your application already invokes an LLM provider or an internal AI helper.

from cloptima_llm_observability import (
    extract_openai_usage,
    init_from_env,
)

cloptima = init_from_env()

result = cloptima.observe_call(
    provider="openai",
    model="gpt-4.1-mini",
    call=lambda: summary_service.generate(prompt),
    extract_usage=extract_openai_usage,
    feature_id="summaries",
    workflow_id="support-agent",
    team_id="customer-support",
    fire_and_forget=False,
)

This integration style works well because it:

  • keeps your existing provider integration intact
  • captures the most accurate model and feature context
  • avoids SDK-specific coupling throughout your codebase
  • works well with existing wrappers and shared AI services

Async and streaming calls

If your application already uses async calls or streaming responses, use the matching helpers:

result = await cloptima.observe_async_call(
    provider="anthropic",
    model="claude-3-5-sonnet",
    call=lambda: assistant_client.reply(messages),
    feature_id="chat_reply",
)

async for chunk in cloptima.observe_async_stream_call(
    provider="openai",
    model="gpt-4.1-mini",
    call=lambda: stream_client.stream(prompt),
    feature_id="live_answer",
):
    print(chunk)

Shared transport integration

If your application centralizes outbound LLM calls behind a shared httpx transport, instrument that boundary instead:

import httpx

from cloptima_llm_observability import init_from_env, instrument_httpx_transport

cloptima = init_from_env()
transport = instrument_httpx_transport(
    httpx.HTTPTransport(),
    cloptima=cloptima,
    provider="openai",
    model="gpt-4o-mini",
    fire_and_forget=False,
)
client = httpx.Client(transport=transport)

This is useful for broad coverage, but it has less application context than observe_call(...). Prefer observe_call(...) when you already know the provider, model, and feature at the call site.

OTLP mode

The SDK supports two delivery modes:

  • cloptima_http
  • otlp_http

Use otlp_http when you want the SDK to send OpenTelemetry-compatible payloads to Cloptima's OTLP-compatible receiver instead of the standard SDK telemetry endpoint.

otlp_http is still a Cloptima delivery mode. The SDK keeps the OTLP route fixed and only lets you override the Cloptima API domain or environment.

Advanced configuration:

  • CLOPTIMA_LLM_OBSERVABILITY_DELIVERY_MODE selects cloptima_http or otlp_http
  • CLOPTIMA_LLM_OBSERVABILITY_API_BASE_URL overrides the Cloptima API domain while the SDK keeps the ingest routes fixed
  • CLOPTIMA_LLM_OBSERVABILITY_OTLP_SERVICE_NAME and CLOPTIMA_LLM_OBSERVABILITY_OTLP_SERVICE_VERSION customize OTLP service metadata

Attribution fields

The most useful fields for reporting and ownership are:

  • app_id
  • environment
  • team_id
  • feature_id
  • workflow_id
  • cost_center
  • business_unit
  • tenant_id
  • customer_segment
  • cloud_account_id
  • cluster_id
  • repository_id

You can pass them through default_attribution, or directly on observe_call(...) / observe_stream_call(...).

Metadata controls

Use metadata_policy to control what custom metadata is retained:

  • metadata_only
  • allowlisted_metadata
  • strict_finops
  • debug_observability

Sensitive-looking keys such as prompts, messages, credentials, and secrets are treated conservatively by default.

Validation helpers

These helpers are useful when you want to inspect payloads locally before sending traffic to Cloptima:

  • preview_event_payload(...)
  • preview_batch_payload(...)
  • preview_otlp_request(...)
  • validate_payload(...)

They return payload previews in memory and do not send network traffic.

Examples

See the examples/ directory for:

  • OpenAI call-site instrumentation
  • OpenTelemetry-compatible delivery to Cloptima
  • Anthropic call-site instrumentation
  • Gemini call-site instrumentation
  • custom wrapper integration
  • httpx transport integration

Public API

Stable core surface:

  • CloptimaLLMObservability
  • init_from_env
  • disabled_client
  • observe
  • observe_call
  • observe_async_call
  • observe_stream
  • observe_stream_call
  • record
  • record_batch
  • record_async
  • provider usage extractors

Additional helper surface:

  • instrument_httpx_client
  • instrument_httpx_transport
  • instrument_openai_compatible_response
  • instrument_openai_compatible_stream
  • ainstrument_openai_compatible_response
  • ainstrument_openai_compatible_stream
  • instrument_fastapi_request_context
  • instrument_flask_request_context

Troubleshooting

No telemetry arrives:

  • verify the API key is valid for Cloptima telemetry ingestion
  • check client.is_enabled()
  • if you use advanced routing overrides, verify CLOPTIMA_LLM_OBSERVABILITY_API_BASE_URL points at the intended Cloptima environment
  • inspect a sample event with validate_payload(preview_event_payload(...))

Configuration behavior:

  • init_from_env() returns a disabled pass-through client when configuration is absent
  • if you explicitly enable the SDK with incomplete config, initialization stays non-blocking by default unless strict=True

Payload contracts

  • single event schema: cloptima.llm.event.v1
  • batch schema: cloptima.llm.batch.v1

SDK envelopes also include sdk_delivery_stats for delivery monitoring.

Support

  • Issues: https://github.com/cloptima/cloptima-llm-observability-python/issues
  • Security: see SECURITY.md
  • Product support: hello@cloptima.ai

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

cloptima_llm_observability-0.1.1.tar.gz (38.6 kB view details)

Uploaded Source

Built Distribution

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

cloptima_llm_observability-0.1.1-py3-none-any.whl (24.6 kB view details)

Uploaded Python 3

File details

Details for the file cloptima_llm_observability-0.1.1.tar.gz.

File metadata

File hashes

Hashes for cloptima_llm_observability-0.1.1.tar.gz
Algorithm Hash digest
SHA256 ed681a3d5bbf2ba0605ae3a4dfaef03e93fd8318b070995a366ffd5de78b8d6a
MD5 70911e8c4c93ed2750c4f031335c0f1c
BLAKE2b-256 adf49db69774586784fee118af02251579b6c7d7c7d2dbb9a3b7174bd4ddd4e4

See more details on using hashes here.

Provenance

The following attestation bundles were made for cloptima_llm_observability-0.1.1.tar.gz:

Publisher: release.yml on cloptima/cloptima-llm-observability-python

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file cloptima_llm_observability-0.1.1-py3-none-any.whl.

File metadata

File hashes

Hashes for cloptima_llm_observability-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 a0cf5ff07df311b44949b8e178fc1090a0b2df5de5bff1094f14f95268695bb2
MD5 cf5c92e3530c0b103660dc0a48a54011
BLAKE2b-256 ada4b72a5d933c3718d2203f892ecb3158ce0aca79b0011837ab4d05b00ec3c5

See more details on using hashes here.

Provenance

The following attestation bundles were made for cloptima_llm_observability-0.1.1-py3-none-any.whl:

Publisher: release.yml on cloptima/cloptima-llm-observability-python

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

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