Skip to main content

Constructor

Options

If omitted, the client reads:
  • MARGOVIA_API_KEY
  • MARGOVIA_BASE_URL
  • MARGOVIA_DEBUG
Installing the SDK alone sends nothing. Without MARGOVIA_API_KEY, the client skips Margovia tracking. With MARGOVIA_API_KEY, it sends to Margovia Cloud unless baseUrl or MARGOVIA_BASE_URL points at a self-hosted or compatible receiver.

openai

Creates a tracked OpenAI client. The tracked client starts a run, calls client.chat.completions.create(request), records cost from the response usage, and completes or fails the run. Use this for the simplest one-call OpenAI integration.

anthropic

Creates a tracked Anthropic client. The tracked client starts a run, calls client.messages.create(request), records cost from the response usage, and completes or fails the run. Use this for the simplest one-call Anthropic integration.

wrapOpenAI

Wraps client.chat.completions.create. With autoTrack: true, the wrapper starts a run, records the provider cost event, and completes the run after the provider call succeeds.

wrapAnthropic

Wraps client.messages.create. With autoTrack: true, the wrapper starts a run, records the provider cost event, and completes the run after the provider call succeeds.

trackOpenAI

Starts a run, calls your function, reads OpenAI response usage, records a cost event, then completes or fails the run. Use this when you prefer an explicit helper around one provider call.

trackAnthropic

Starts a run, calls your function, reads Anthropic response usage, records a cost event, then completes or fails the run. Use this when you prefer an explicit helper around one provider call.

track

Wraps a function in a run. The run is completed on success and failed on error. track does not extract provider token usage by itself. Use tracked or wrapped provider clients inside fn, call trackOpenAI(...) or trackAnthropic(...), or manually report cost.

canRun

Checks active Margovia budgets before running expensive work. This is advisory until hard/soft stop enforcement is enabled in your product.

flush

Waits for in-flight SDK requests to settle. Most SDK calls are awaited already, but flush is useful before a short-lived process exits.

startRun

Starts a run and returns a MargoviaRun. Manual runs stay running until run.complete(...) or run.fail(...) is called.

trackCost

Records a cost event for an existing run. Cost events do not close manual runs unless completeRun: true is explicitly sent. In normal manual workflows, call run.complete(...) after all work is finished.

trackOutcome

Records an outcome for an existing run.

Helpers

customer(...) and user(...) help create stable namespaced IDs such as workspace_123 and user_456.