OpenAI SDK
OpenAI’s official SDKs work with OpenPike for every model on the chat completions endpoint, and for OpenAI, xAI, Groq and OpenRouter models on the Responses endpoint: set the base URL and the key, and keep the rest of your code.
Python (openai) |
TypeScript (openai) |
|
|---|---|---|
| Base URL | base_url="https://api.openpike.dev/v1" |
baseURL: "https://api.openpike.dev/v1" |
| Key | api_key= a virtual key |
apiKey: a virtual key |
| Tags on every request | default_headers={"x-openpike-tags": ...} |
defaultHeaders: { "x-openpike-tags": ... } |
| Tags on one request | extra_headers={...} |
second argument { headers: {...} } |
Install
Section titled “Install”pip install openai # Pythonnpm install openai # TypeScriptStreaming
Section titled “Streaming”Streaming works as with OpenAI: OpenPike passes the events through as they arrive and adds the token counts to the last chunk.
import os
from openai import OpenAI
client = OpenAI( base_url="https://api.openpike.dev/v1", api_key=os.environ["OPENPIKE_API_KEY"], default_headers={"x-openpike-tags": "agent=docs-example,env=dev"},)
stream = client.chat.completions.create( model="deepseek/deepseek-flash", messages=[{"role": "user", "content": "Count from one to five."}], stream=True,)for chunk in stream: if chunk.choices and chunk.choices[0].delta.content: print(chunk.choices[0].delta.content, end="", flush=True)print()import OpenAI from "openai";
const client = new OpenAI({ baseURL: "https://api.openpike.dev/v1", apiKey: process.env.OPENPIKE_API_KEY, defaultHeaders: { "x-openpike-tags": "agent=docs-example,env=dev" },});
const stream = await client.chat.completions.create({ model: "deepseek/deepseek-flash", messages: [{ role: "user", content: "Count from one to five." }], stream: true,});for await (const chunk of stream) { process.stdout.write(chunk.choices[0]?.delta?.content ?? "");}process.stdout.write("\n");Responses API
Section titled “Responses API”client.responses.create goes to OpenPike’s /v1/responses, which passes the request to the provider unchanged. It works with models from OpenAI, xAI, Groq and OpenRouter: the providers whose entries in client.models.list() list the responses format. OpenPike does not translate Responses into other formats, so Claude, DeepSeek and the other chat completions providers are not served here: use client.chat.completions.create for them.
curl https://api.openpike.dev/v1/responses \ -H "Authorization: Bearer $OPENPIKE_API_KEY" \ -H "Content-Type: application/json" \ -H "x-openpike-tags: agent=docs-example,env=dev" \ -d '{ "model": "openai/gpt-5.4-mini", "input": "Say hello in five words.", "store": false }'import os
from openai import OpenAI
client = OpenAI( base_url="https://api.openpike.dev/v1", api_key=os.environ["OPENPIKE_API_KEY"], default_headers={"x-openpike-tags": "agent=docs-example,env=dev"},)
response = client.responses.create( model="openai/gpt-5.4-mini", input="Say hello in five words.", store=False,)print(response.output_text)print(response.usage.input_tokens, response.usage.output_tokens)import OpenAI from "openai";
const client = new OpenAI({ baseURL: "https://api.openpike.dev/v1", apiKey: process.env.OPENPIKE_API_KEY, defaultHeaders: { "x-openpike-tags": "agent=docs-example,env=dev" },});
const response = await client.responses.create({ model: "openai/gpt-5.4-mini", input: "Say hello in five words.", store: false,});console.log(response.output_text);console.log(response.usage?.input_tokens, response.usage?.output_tokens);Streaming (stream=True), tools, reasoning and structured outputs pass through as with OpenAI, and the token counts come from the final response.completed event. Three things differ from calling OpenAI directly:
background=Trueis rejected with400 unsupported_parameter, because a background response carries no token counts for OpenPike to record.- With an alias, a request that sets
previous_response_idorconversationgoes only to the alias’s first target, with no fallback. - Hosted tools (web search, file search, code interpreter) work, but their per-call fees are not in OpenPike’s cost. See Usage and cost.
A run ID per task
Section titled “A run ID per task”A run budget caps one agent run’s spend, requests and time. Create a client per task with the run in its default headers, or send it per request:
from openai import OpenAI
client = OpenAI( base_url="https://api.openpike.dev/v1", api_key=virtual_key, default_headers={"x-openpike-run": task.id, "x-openpike-run-label": task.title, "x-openpike-run-max-usd": "5"},)const client = new OpenAI({ baseURL: "https://api.openpike.dev/v1", apiKey: virtualKey, defaultHeaders: { "x-openpike-run": task.id, "x-openpike-run-label": task.title },});In Python, client.with_options(default_headers={...}) gives an existing client a run without building a new one. For one request, pass extra_headers={...} in Python or { headers: {...} } as the second argument in TypeScript.
Reading OpenPike’s headers
Section titled “Reading OpenPike’s headers”Use the SDK’s raw response to read x-openpike-request-id, x-openpike-model and x-openpike-attempts. See Aliases and fallbacks for an example.
What works and what does not
Section titled “What works and what does not”- Works:
client.chat.completions.createwith any model on/v1/chat/completions, streaming, tools and function calling, JSON mode and structured outputs where the provider supports them,client.responses.createwith OpenAI, xAI, Groq and OpenRouter models, andclient.models.list(). OpenPike passes request fields through untouched. - Does not work:
client.responses.retrieve,.cancel,.deleteand.input_items, background responses, embeddings, images, audio, files, batches and assistants. OpenPike does not serve those endpoints. - Errors come back in OpenAI’s shape, so the SDK raises its usual exceptions:
AuthenticationErrorfor401,RateLimitErrorfor OpenPike’s429blocks. Check the errorcodeto tell a budget block from a provider rate limit; see Errors. Retries: keep the SDK’s default retries on429. OpenPike refuses a blocked request before any provider call, so a few retries cost nothing, and they still cover provider5xxerrors. Onbudget_exceeded,loop_detected,scope_pausedorrun_limit_exceeded, stop the task and tell the user instead of retrying the same request.
Example
Section titled “Example”Runnable copies of this setup, with a .env.example, are in the OpenPike examples repo: Python and TypeScript.