Recommended: tracked OpenAI client
Use this when one OpenAI call should become one Margovia run.client.chat.completions.create(request), reads response.usage, records the cost event, and completes or fails the run.
Use a stable, namespaced customerId. Margovia keeps this ID exactly as sent so it can join back to your app or billing data.
The tracked client records:
provider: "openai"- model
- prompt tokens
- completion tokens
- cached prompt tokens
- reasoning tokens
- latency
Alternative: patch an existing client
Use this when you want to keep normal OpenAI call sites and pass Margovia fields through providermetadata.
Custom run input
UsegetRunInput when your customer metadata lives outside the provider request.
getRunInput when metadata is not available on the provider request or when you want to derive attribution from your authenticated app context.
Explicit helper
UsetrackOpenAI(...) when you already have your own helper function around OpenAI calls.
response.usage, records cost, and completes or fails the run.
Use a raw OpenAI client inside trackOpenAI(...). Do not pass an already-wrapped client into this helper or you may double-report the same call.
Manual run context
Userun.step(...) when a provider call belongs to a larger manual workflow.
run.complete(...) or run.fail(...) after the workflow finishes.
Common mistake
Do not wrap a raw OpenAI call with.track(...) and expect cost to appear:
margovia.openai(client), wrapOpenAI(...), trackOpenAI(...), or run.trackCost(...).