Install
Configure
You need two kinds of keys:- A Margovia API key from your Margovia project settings. This lets the SDK send usage to Margovia.
- Your model provider key, such as
OPENAI_API_KEYorANTHROPIC_API_KEY. Margovia does not call OpenAI or Anthropic for you.
MARGOVIA_API_KEY is configured, the SDK sends events to Margovia Cloud by default.
If you self-host Margovia, run a compatible receiver, or test against a local API, set MARGOVIA_BASE_URL:
Track your first OpenAI call
The easiest OpenAI setup ismargovia.openai(client).
Think of it as a tracked OpenAI client. You still call OpenAI, but Margovia records the workflow name, customer, token usage, and cost.
Track your first Anthropic call
The Anthropic setup is the same idea. Wrap the Anthropic client once, then callanthropic.messages.create(...) with your Margovia fields plus the real Anthropic request.
If you already have a helper
UsetrackAnthropic(...) or trackOpenAI(...) only when you already have your own provider helper function and want to keep that shape.
startRun, run.trackCost, and run.complete.
Use a raw provider client inside trackAnthropic(...) or trackOpenAI(...). Do not pass an already-wrapped client into these helpers or you may double-report cost.
What .track(...) is for
margovia.track(...) tracks a workflow boundary. It does not read OpenAI or Anthropic token usage by itself.
Good use:
anthropic is not wrapped then Margovia receives no token usage and no cost.
If you use another provider
OpenAI and Anthropic have first-class helpers because the SDK knows how to read their usage fields. For Gemini, Cohere, custom models, search APIs, or internal tools, use manual tracking and send either tokens orcostUsd.
Verify
Run the code once, then open Margovia and look for a run named:- The code ran on your backend, not in the browser
MARGOVIA_API_KEYis set in the same process that calls OpenAI or Anthropic- The API key belongs to the Margovia project you are viewing
MARGOVIA_BASE_URLis not pointing at a local API by accident
Attribution checklist
Send attribution from your app’s source of truth:customerId: stable account, workspace, org, tenant, or billing customer IDcustomerName: readable display namecustomerPlan: current plan name and monthly revenueuserId: optional actor inside that customer account
workspace_123, org_abc, or stripe_cus_123. Margovia stores the ID exactly as sent so it can join back to your app and billing data.