Providers

OpenAI API gateway: proxy GPT calls with per-feature cost tracking and budget limits

Route OpenAI SDK calls through an OpenAI-compatible gateway by changing base_url. Track GPT spend per feature, team and user, and cap it with budgets enforced before the call — Python, Node.js and curl examples.

Cognocient AI is an OpenAI-compatible gateway: the OpenAI SDK you already use talks to it unchanged. You swap base_url and the API key, and every GPT call gets per-feature cost attribution, budgets enforced before the call, and a kill switch.

Setup

  1. Add your OpenAI API key in Settings → AI Providers. It is encrypted at rest and never shown again.
  2. Create a proxy key (sk-cog-...) in Settings → Proxy Keys.
  3. Change two lines in your client:

Setting OPENAI_BASE_URL=https://api.cognocient.com/v1 and OPENAI_API_KEY=sk-cog-... in the environment works too: the official SDKs read both, so tools that wrap the OpenAI SDK pick the gateway up with no code change.

What works

EndpointThrough the gatewayNotes
/v1/chat/completions✅Streaming, function/tool calling, structured output, vision
/v1/responses✅Stateless: send the full conversation; hosted tools are not supported. See Responses API
/v1/embeddings✅Costed per input token
/v1/images/generations✅dall-e-3, dall-e-2, gpt-image-1, priced per image
/v1/audio/*✅Transcription, translation, speech. See Audio & rerank
/v1/files, /v1/batches✅ Growth planBatch discount attributed to your tags. See Batch API

Model names are forwarded exactly as you send them, so a model OpenAI releases today works today. Pricing comes from a daily catalogue sync; until a brand-new model is priced, its calls are costed at a conservative fallback rate rather than $0 (see New models).

Streaming and token usage

On streamed calls the gateway asks OpenAI for a final usage chunk (stream_options.include_usage), so cost is computed from OpenAI's own token counts, not an estimate. You don't need to set it yourself, and your client will see one extra chunk with an empty choices array at the end of the stream, which the official SDKs handle.

Tag calls by feature, team and user

Headers set on the client tag every call it makes. Per-request tags go in extra_headers:

client.chat.completions.create(
    model="gpt-4o",
    messages=messages,
    extra_headers={
        "X-Cost-User": f"user_{user.id}",       # your internal ID, not an email
        "X-Cost-Run-ID": run_id,                # groups an agent run's calls
    },
)

The full list is in Attribution headers. Untagged calls still appear, under untagged.

Cap OpenAI spend

Create a budget in Budgets → New Budget on a feature, department, user, run or the whole account. It is checked before the call is forwarded, using a reservation for the call's worst-case cost, so concurrent calls can't jointly overshoot it. When it runs out you choose what happens:

  • Block — the call returns 429 with a JSON error your code can catch.
  • Degrade — the call runs on a cheaper model you pick (for example gpt-4o → gpt-4o-mini) and the response carries x-cog-degraded: true.
  • Alert — the call goes through and you get an email or Slack alert.

See Budget enforcement for how reservations, streaming and timeouts are counted.

Azure OpenAI takes precedence for GPT models

If you also connect an Azure OpenAI key, model names containing gpt-, o1 or o3 are sent to your Azure deployment instead of OpenAI. See Azure OpenAI.

Frequently asked questions

On this page