Python SDK, OpenTelemetry and LiteLLM

Integrations & Data Sources
#python#pip#otel#opentelemetry#otlp#litellm#gen_ai#collector

Send telemetry without the JavaScript SDK: a Python package, a LiteLLM callback, or existing OpenTelemetry GenAI spans.

How It Works Under the Hood
  • •All integrations send only model ids and token counts. Energy and carbon are always recomputed on the server with the same EcoLogits methodology, so every source produces comparable figures.
  • •Python: shdwsdk.instrument(client) wraps OpenAI and Anthropic clients (sync, async and streaming). Events are sent from a background thread with a 1.5s timeout and never raise into your code.
  • •OpenTelemetry: POST OTLP/HTTP JSON traces to /api/v1/otlp/v1/traces with an x-shdw-key header. Spans with gen_ai.request.model or gen_ai.response.model and gen_ai.usage.* token counts are ingested; other spans are ignored. Protobuf encoding isn't supported.
  • •LiteLLM: ShdwLiteLLMLogger is a CustomLogger; add it to litellm.callbacks or reference shdwsdk.integrations.litellm.proxy_handler_instance in the proxy config.
  • •The dashboard's Data completeness card shows how many requests came from each integration.
How to Use It Step-by-Step
  1. 1Python: pip install shdwsdk, set SHDW_API_KEY, call shdwsdk.init() and shdwsdk.instrument(OpenAI()).
  2. 2OpenTelemetry: set OTEL_EXPORTER_OTLP_TRACES_ENDPOINT, OTEL_EXPORTER_OTLP_TRACES_PROTOCOL=http/json and the x-shdw-key header (see the install guide).
  3. 3If two instrumentations record the same call, keep only one so usage isn't counted twice.
Python
import shdwsdk
from openai import OpenAI

shdwsdk.init()                       # reads SHDW_API_KEY
client = shdwsdk.instrument(OpenAI())

Related guides