Agent instructions
You are integrating Pylva, cost infrastructure for AI agent businesses. The SDK meters the host application’s LLM and non-LLM usage per customer and per agent step, and reports cost-shaped telemetry. Rules to follow throughout:- Never print, log, or commit the Pylva API key. Store it in the project’s secret store or
.env, and make sure.envis gitignored. - Never send prompt text, completions, tool arguments, message bodies, emails, or phone numbers in telemetry. Use opaque
customer_idvalues and stablesteplabels. - Pylva instrumentation must fail open: do not let it break the host application.
- The full documentation index is at
https://docs.pylva.com/llms.txt. You can also connect the docs MCP server:claude mcp add --transport http pylva-docs https://docs.pylva.com/mcp.
Step 1 - Detect the stack
Inspect the project and choose one integration mode:| Project uses | Mode |
|---|---|
TypeScript with openai, @anthropic-ai/sdk, or Vercel ai | Auto-instrumentation via init |
Python with openai or anthropic | Auto-instrumentation via init |
| LangChain or LangGraph (either language) | PylvaCallbackHandler - do NOT also auto-instrument the same calls, or they will be double counted. See LangGraph Cost Tracking. |
| Anything else (other providers, non-LLM costs) | Manual reporting - see Non-LLM Usage |
Step 2 - HUMAN ACTION: account and API key
Tell your human exactly this, then wait:I need a Pylva API key.Shortcut: on a brand-new account, the empty dashboard shows a “Get started” checklist with a one-click Create API key button.
- Sign in at https://pylva.com/login (GitHub, Google, or email magic link). Your first sign-in creates a free workspace automatically - no credit card needed. The Free plan includes 100k events/month.
- Open https://pylva.com/settings/keys (after sign-in it lands on Settings -> API keys). You must be the workspace owner.
- Click Create key and create it.
- Copy the key immediately - it is shown only once. Put it in
.envasPYLVA_API_KEY=pv_live_...(or paste it to me and I will store it there).
Step 3 - Validate the key
Before writing any code:- Check the format:
^pv_(live|cli)_[a-f0-9]{8}_[a-f0-9]{32}$. New keys usepv_live_*; legacypv_cli_*keys remain valid after the universal-key migration. - Check it live (this endpoint is a safe read):
200 means the key is valid. 401 means it is not - re-ask the human (a typo or a revoked key; the body is {"error":{"code":"INVALID_API_KEY",...}}). 403 WRONG_SCOPE only appears on older deployments that still enforce legacy key scopes.
For a richer check, GET https://api.pylva.com/api/v1/whoami with the same header returns the workspace, plan tier, and current usage in one call - see Who am I.
Step 4 - Install and wire the SDK
The SDK does not read environment variables automatically - always pass the key explicitly from the environment.TypeScript
Python
- Find where the application already knows which customer a request belongs to, and wrap at that boundary. Use the application’s stable internal user/account ID as
customer_id(opaque,[A-Za-z0-9_-], max 255 chars) - never an email. - Use a small set of stable
steplabels named after workflow stages (retrieve_context,draft,evaluate), not per-request values. - Self-hosted Pylva: pass
endpoint: "https://<your-host>"/endpoint="https://<your-host>"toinit.
Step 5 - Verify end to end
- Optionally prove ingest works before running the app - send one zero-cost test event:
{"accepted":1,"rejected":0}. Every field above is required - the API rejects partial events. The full schema is at https://api.pylva.com/openapi.json.
-
Run the instrumented code path once for a real (test) customer. The SDK flushes about every 5 seconds; for a short script, flush before exit:
await flush()(TypeScript) /await pylva.flush()(Python, orasyncio.run(pylva.flush())in sync scripts). - Tell the human:
Open your Pylva dashboard - once the first event arrives, the “Get started” checklist is replaced by live cost data. You should see customer setup-test (and your real test customer) with the steps we instrumented.
If no event appears, work through Troubleshooting.
Step 6 - Production checklist
- Confirm no prompts, completions, or PII are sent in any
metadatayou added (Privacy and PII). - If the project has non-LLM costs (search, speech, vector DBs), see Non-LLM Usage and tell the human which pricing rules to configure in the dashboard.
- Before enabling budget hard stops, read Budget Enforcement and Controls Workflow.
Step 7 - HUMAN ACTION (optional): upgrade the plan
The Free plan (100k events/month) is enough to complete this setup. See Plans and limits for tier details and what happens at the cap. When usage approaches the cap, tell the human:Your Pylva workspace is on the Free plan (100k events/month). To upgrade: https://pylva.com/pricing - Pro (199/mo, 10M events) at https://pylva.com/login?next=/subscribe/scale.Payment happens in the browser; do not ask the human for card details.