Back to GuidesLLM Setup

Connect OpenAI Usage to Cloptima

6 min readUpdated June 2026

Choose your adoption path

Two paths, and they coexist: route selected apps through Cloptima's governed gateway for real-time policy enforcement, or keep direct OpenAI calls and add the Cloptima SDK for telemetry and attribution. Start wherever the friction is lowest.

Path A — route through the gateway

Create a Cloptima virtual key, bind it to a policy (approved models, budget ceilings, environment limits), then change only the base URL and key in your OpenAI client. Attribution travels on x-cloptima-* headers.

python
from openai import OpenAI

client = OpenAI(
    base_url="https://api.cloptima.ai/v1/ai",
    api_key="clop_vk_dPXO67p…",
)

resp = client.chat.completions.create(
    model="gpt-4o-mini",
    messages=[{"role": "user", "content": "..."}],
    extra_headers={"x-cloptima-team": "search", "x-cloptima-app": "ranking", "x-cloptima-environment": "production"},
)

Path B — keep direct calls, add the SDK

If you can't change the request path yet, wrap the call with the Cloptima observability SDK. You keep your own provider client, retries, and auth; Cloptima gets usage and attribution. SDKs are available for JavaScript/TypeScript, Python, and Go.

ts
import { initFromEnv, extractOpenAIUsage } from "@cloptima/llm-observability";

const cloptima = initFromEnv(); // CLOPTIMA_LLM_OBSERVABILITY_API_KEY / _APP_ID

const result = await cloptima.observeCall({
  provider: "openai",
  model: "gpt-4o-mini",
  call: () => openai.chat.completions.create(req),
  extractUsage: extractOpenAIUsage,
  featureId: "ranking",
  workflowId: "search_pipeline",
});

Add attribution

Whichever path you choose, attach team, app, feature, environment, user, session, and run context — plus custom dimensions — so spend maps to owners and product workflows, not just a provider total.

Validate reporting

Compare the first day of traffic by provider, model, app, and team in the spend explorer. Treat missing team/app/environment as an onboarding task before moving more production traffic.

Put This Guide Into Practice

Cloptima automates the strategies described in this guide.

No credit card required
5-minute setup
Free trial