Skip to main content

OpenAI pass-through

The /openai_passthrough route lets a team keep using OpenAI's own API and SDK while the gateway holds the OpenAI key. The caller sends a Spanway virtual key, the gateway swaps in its own OpenAI credential, forwards the request to OpenAI unchanged and records the call in the spend logs. Use it for OpenAI endpoints the unified API does not cover, or when a product must behave exactly as it does against OpenAI directly. /v1/responses on the gateway is the unified Responses API that works with every provider, while /openai_passthrough/v1/responses always reaches OpenAI.

The gateway also serves /openai/{path}, which forwards straight to OpenAI like /openai_passthrough. The Azure style paths /openai/deployments/{model}/chat/completions, /openai/deployments/{model}/completions and /openai/deployments/{model}/embeddings are different: they are unified routes that run the named model_list model on any provider, with the same routing, fallbacks and guardrails as /v1/chat/completions.

A real-world scenario​

A support team built an internal tool on the OpenAI Responses API and stores conversation state with OpenAI. The security team wants the OpenAI key out of the tool and every call attributed to a team budget. The tool keeps the OpenAI SDK, points its base URL at https://gateway.example.com/openai_passthrough/v1 and uses a virtual key. Nothing else changes, and the Logs page now shows every call with the team, the model and the cost.

Before you start​

The gateway needs an OpenAI credential. It looks first for a model_list deployment with custom_llm_provider OpenAI and use_in_pass_through: true, and otherwise for the environment variable below.

VariablePurpose
OPENAI_API_KEYThe OpenAI key the gateway sends upstream.
OPENAI_API_BASEOptional. Changes the upstream address from https://api.openai.com.

When no OpenAI key is found, the gateway can sign in to OpenAI with workload identity instead: with OPENAI_IDENTITY_PROVIDER_ID, OPENAI_SERVICE_ACCOUNT_ID and OPENAI_IDENTITY_TOKEN_FILE set, it exchanges the platform token for a short lived OpenAI token on each call, so no long lived OpenAI key is stored on the gateway at all.

Steps​

  1. Give the gateway the credential. In the Admin UI open /ui/models-and-endpoints, stay on the Add Model tab, pick OpenAI under Provider, pick a model, open Advanced Settings, turn on Use in pass through routes and click Add Model. Or set OPENAI_API_KEY in the gateway environment and restart.

  2. Issue a virtual key. Open /ui/api-keys, click + Create New Key, give it a name and a budget, and click Create Key. Copy the key into SPANWAY_API_KEY.

  3. Call OpenAI through the gateway.

    curl -X POST http://localhost:4000/openai_passthrough/v1/responses \
    -H "Authorization: Bearer $SPANWAY_API_KEY" \
    -H "Content-Type: application/json" \
    -d '{"model": "gpt-6.1-sol", "input": "Say hello to the support team."}'
  4. With the OpenAI SDK, set the base URL to http://localhost:4000/openai_passthrough/v1 and the API key to the virtual key.

  5. Open /ui/logs and click the newest row to see the request, the key that made it and its cost.

Expected result​

The response is OpenAI's own response, with OpenAI's status code and headers. Streaming responses are streamed through as they arrive.

{
"id": "resp_7a2b",
"object": "response",
"model": "gpt-6.1-sol",
"output": [{"type": "message", "role": "assistant", "content": [{"type": "output_text", "text": "Hello, support team."}]}],
"usage": {"input_tokens": 9, "output_tokens": 5, "total_tokens": 14}
}

The spend log row carries the calling key, team and user, and the cost worked out from the usage in the response. Budgets, rate limits, blocked keys and denied_passthrough_routes apply exactly as on the unified endpoints.

Set general_settings.enable_openai_websocket_passthrough: true to also serve the OpenAI WebSocket route, which relays frames under the gateway's credential without reading them.

Errors​

StatusMessageWhat it means and how to fix it
401Authentication Error, Invalid proxy server token passedThe virtual key is missing, wrong, expired or blocked. Create a key on /ui/api-keys or check its status there.
403Key/team denied access to passthrough route /openai_passthrough/...The key or team lists the route in denied_passthrough_routes. Remove it or use another key.
500Required 'OPENAI_API_KEY' in environment to make pass-through calls to OpenAI.The gateway has no OpenAI credential. Add a deployment with Use in pass through routes on, or set OPENAI_API_KEY.
Any otherOpenAI's own error bodyThe request reached OpenAI and OpenAI refused it. Read the message, it is returned unchanged.

Logs​

The route writes the shared pass-through lines, see the pass-through Logs section. An error from OpenAI is logged at WARNING as pass_through_endpoint: upstream <method> <url> returned <status>: <body>, with the body cut to 4096 characters and never the request body. At DEBUG the credential lookup says whether a model_list deployment, OPENAI_API_KEY or workload identity was used.

Pass-through endpoints lists every built-in route and explains custom routes. Pass-through guardrails covers guardrails on custom routes. The Responses API is the unified alternative that works with every provider, and the OpenAI provider page covers OpenAI through the unified endpoints.