Skip to main content
The TypeScript SDK ships as @pylva/sdk. It auto-instruments supported provider clients and emits server-priced telemetry to Pylva.

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 flushInterval in milliseconds (default 5000; the Python SDK’s flush_interval is in seconds). See Open Source And Hosted when choosing between Cloud and self-hosting. If init throws InvalidApiKeyError or telemetry logs a key rejection, see Troubleshooting.

Auto-instrumentation

Importing the SDK patches supported provider classes at the prototype level, 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.
  • Vercel AI.
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 uses async context so metadata follows nested awaits without manual argument threading. If a tracked call succeeds but does not appear in the dashboard, see Troubleshooting.

LangGraph and LangChain callbacks

Use the callback entrypoint when LangGraph.js or LangChain.js owns the model calls. This path is observer-only and does not import the root SDK entrypoint, so it does not auto-patch providers.
@pylva/sdk/langchain re-exports the same handler. Pylva records run ids, parent run ids, graph node attribution, provider, model, token usage, latency, status, and customer id. It does not send prompts, completions, tool inputs, or tool outputs. Customer id resolution order is:
  • Constructor customerId.
  • metadata.pylva_customer_id.
  • metadata.customer_id.
  • Active track() context.
  • anonymous.
Enable tool-call usage only when the tool execution itself is billable:
Tool events report metric="calls" and metric_value=1.

Non-LLM usage

Note the snake_case keys: reportUsage takes wire-format field names (customer_id), unlike the camelCase options elsewhere in the TypeScript API. 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.