Skip to main content
The Python SDK installs from PyPI as pylva-sdk and imports as pylva. Python 3.10 or newer is required.

Initialize

The SDK does not read PYLVA_API_KEY on its own - pass it explicitly as shown. You can pass endpoint for self-hosted deployments (omit it for Pylva Cloud), and flush_interval in seconds (default 5.0; the TypeScript SDK’s flushInterval is in milliseconds). See Open Source And Hosted when choosing between Cloud and self-hosting. If init raises InvalidApiKeyError or telemetry logs a key rejection, see Troubleshooting.

Auto-instrumentation

Importing pylva patches supported provider classes, and init re-applies the patches defensively - so clients constructed before init are still covered, but calls made before init runs are not recorded. Call init at process startup, before serving traffic. Current coverage:
  • OpenAI.
  • Anthropic.
Captured fields include provider, model, tokens in, tokens out, latency, status, customer_id, and step_name when a tracking context is active. For the full automatic, callback, and manual coverage matrix, see Supported Integrations.

Tracking context

track_context uses Python context variables so metadata follows async work without manual argument threading. For short scripts, flush before exit: await pylva.flush() inside async code, or asyncio.run(pylva.flush()) in sync scripts (it raises inside an already-running event loop). If a tracked call succeeds but does not appear in the dashboard, see Troubleshooting.

LangGraph callback

Use the callback path when LangGraph is the orchestration layer. It preserves LangGraph run ids and uses langgraph_node as the default Pylva step. See LangGraph Cost Tracking.

Non-LLM usage

Report raw usage, not dollars. Pylva applies pricing on the server.

Budget hard stops

SDK errors fail open unless an active, cached rule intentionally blocks the call.

Webhook verification

The helper accepts raw hex signatures and sha256= prefixed signatures. For the full delivery workflow, see Alert Delivery And Webhooks.