Docs
OpenAI-compatible Wrapping

OpenAI-compatible Wrapping

How wrapOpenAI preserves your existing client while adding provider identity, policy, estimation, spend reconciliation, and spans.

OpenAI-compatible Wrapping

wrapOpenAI() returns the same client shape you pass in, with Captar runtime control around supported model methods. Your application still calls the provider client directly; Captar is not a hosted LLM proxy.

const openai = captar.wrapOpenAI(client, { session });
 
await openai.responses.create({
  model: 'gpt-4.1-mini',
  input: 'Summarize this support thread.',
  max_output_tokens: 180,
});

What the wrapper adds

Before execution:

  • reads the request model and estimates usage/cost,
  • evaluates call policy such as allowed/blocked models and cost/token ceilings,
  • reserves estimated spend against the active session,
  • creates a request span and emits request lifecycle events.

After execution:

  • extracts usage from the provider response,
  • records input/output/cached-input tokens when available,
  • commits actual cost and releases unused reservation,
  • emits provider-response and spend events,
  • marks the request span completed, blocked, or failed.

Provider identity

The wrapper defaults to provider: 'openai' for compatibility with existing integrations.

For another OpenAI-compatible upstream, set the provider explicitly:

const openrouter = captar.wrapOpenAI(openrouterClient, {
  session,
  provider: 'openrouter',
});

That provider value is used by pricing and telemetry. It also prevents OpenRouter calls from being mislabelled as OpenAI in exported events.

Provider-reported actual cost

When the response contains an authoritative numeric usage.cost, Captar uses that amount as the actual committed USD cost. This is useful for routers such as OpenRouter where the final upstream/provider price may be selected dynamically.

If the provider does not report cost, Captar falls back to its pricing registry for reconciliation. Estimate-time pricing still matters because the budget decision happens before the response exists.

Per-wrapper policy

You can add call policy at the wrapper level. It is merged with the active session policy.

const openai = captar.wrapOpenAI(client, {
  session,
  policy: {
    call: {
      allowedModels: ['gpt-4.1-mini'],
      maxEstimatedCostUsd: 0.35,
      maxOutputTokens: 300,
      retriesCeiling: 1,
      timeoutMs: 15_000,
    },
  },
});