How to Set Hard Spend Limits on the OpenAI API
Enforce atomic daily, monthly, and per-request dollar caps on OpenAI SDK calls before upstream provider billing runs.
Verified Source Facts
Provider-level billing dashboards evaluate usage asynchronously and issue invoices after requests finish. When an agent worker spawns parallel retries or loops over large tool payloads, a soft monthly email alert arrives minutes after your balance is spent.
Noethekx enforces hard spend limits synchronously at the gateway before any request is forwarded to OpenAI. Point your OpenAI client baseURL to https://nothekx.com/api/v1 and pass a scoped nk_live_your_key_here proxy key with daily, monthly, and single-request caps.
Drop-In OpenAI SDK Configuration
Replace only the base URL and API key in your existing OpenAI client. Existing chat.completions.create calls, tool definitions, and streaming responses continue working without SDK wrappers.
import OpenAI from 'openai';
const client = new OpenAI({
baseURL: 'https://nothekx.com/api/v1',
apiKey: process.env.NOETHEKX_KEY ?? 'nk_live_your_key_here',
});
const completion = await client.chat.completions.create({
model: 'gpt-4o',
max_tokens: 512,
messages: [
{ role: 'user', content: 'Return a JSON checklist for API key rotation.' },
],
});
console.log(completion.choices[0]?.message.content);When the gateway responds, inspect X-Noethekx-Cost-Usd and X-Noethekx-Remaining-Daily-Usd headers to track exact cost in real time, or X-Noethekx-Block-Reason when a request is rejected with HTTP 402 CAP_EXCEEDED.
How Pre-Flight Reservation Prevents Concurrent Overspend
Checking spent < cap in application memory fails when multiple workers execute concurrently. Two or more workers read the same remaining balance, pass the check, and dispatch upstream OpenAI requests simultaneously.
Noethekx calculates a worst-case estimate from input byte length and max_tokens (or max_completion_tokens) using estimateWorstCaseMicroCents before contacting OpenAI. A Firestore transaction reserves that amount atomically on both the proxy key and the organization spend policy, then records a lease in active_reservations that expires after 5 minutes if a node terminates mid-flight.
Once OpenAI returns usage.prompt_tokens and usage.completion_tokens, reconcileBudgetAtomically swaps the worst-case reservation for the actual micro-cent charge. For streaming responses, Noethekx injects stream_options: { include_usage: true } automatically and still reconciles conservatively from streamed character length if the client disconnects early.
Measured Local Gateway Overhead and Concurrency Results
Running node scripts/bench_proxy.mjs against the local HTTP gateway handler over 100 sequential HTTP requests measured 7.004ms median overhead and 16.001ms p95 overhead. Under a 25 parallel requests race configured with a $0.05 daily cap, optimistic concurrency retries enforced the boundary strictly with zero overspend and zero leaked reservations.
Curl Verification Check
Test cap enforcement from your terminal against the proxy endpoint:
curl -X POST https://nothekx.com/api/v1/chat/completions \
-H "Authorization: Bearer nk_live_your_key_here" \
-H "Content-Type: application/json" \
-d '{
"model": "gpt-4o-mini",
"max_tokens": 128,
"messages": [{"role": "user", "content": "Confirm gateway status."}]
}'When a request exceeds max_request_cents, daily_cap_cents, or monthly_cap_cents, the gateway returns an OpenAI-formatted error JSON body with HTTP 402 without ever hitting your upstream OpenAI balance.
Test hard spend caps in the Zero-Key Sandbox
Route requests through https://nothekx.com/api/v1 or inspect pass-through header mode.