Ailoot Engineering
A practical guide to OpenAI API architecture, authentication, streaming, tool use, rate limits, and production operations.
Overview
A working curl is only the start. Production needs key isolation, observability, rate-limit handling, retries, and a migration plan for Responses API / Chat Completions coexisting—Assistants API may be legacy. This guide targets backend and full-stack engineers who’ve read API Quickstart. Fields and endpoints follow API Reference.
Recommended architecture: BFF / gateway
[Web / App] ──→ [Your BFF / Gateway] ──→ api.openai.com
│
tenant auth, QPS limits,
prompt templates, RAG injection,
logging and token metering
| Layer | Should do | Must not |
|---|---|---|
| Client | UI, session id | Hold OpenAI key |
| BFF | Assemble messages, merge retrieval | Send key to browser |
| Worker | Async batch, long jobs | Raw HTTP without timeout |
| OpenAI | Inference | Business RBAC |
Auth and key lifecycle
Authorization: Bearer sk-...
Content-Type: application/json
- Read keys from env vars or Vault; validate presence at startup.
- Multi-tenant SaaS: don’t give tenants extractable keys—proxy from backend.
- Rotation: new key live → dual-write verify → switch traffic → revoke old key.
Platform details: Platform Overview.
Choosing an API shape
| Shape | Endpoint (example) | Best for |
|---|---|---|
| Responses API | /v1/responses |
Quickstart default; tool use |
| Chat Completions | /v1/chat/completions |
Legacy, many third-party examples |
| Assistants API |
/v1/assistants, etc. |
Stateful Thread; check legacy status |
| Embeddings | /v1/embeddings |
Vectors—see embeddings guide |
New projects: follow Quickstart.Legacy: set migration milestones; avoid indefinite dual-stack. Assistants migration: Assistants guide.
Request design: messages and roles
{
"model": "gpt-4o-mini",
"messages": [
{"role": "system", "content": "You are order support. Answer shipping and returns only; escalate other topics."},
{"role": "user", "content": "Where is order 12345?"}
],
"temperature": 0.2,
"max_tokens": 512
}
gpt-4o-mini is an example model ID—replace with current docs. For the flagship shift, see GPT-6 Astra (gpt-6-astra): keep a rollback to a stable production id and log the verification date.
| Role | Purpose |
|---|---|
system |
Rules, tool docs, output format |
user |
End user or upstream system |
assistant |
Prior replies for multi-turn context |
tool / function
|
Tool return (varies by API version) |
Multi-turn: your service maintains messages; summarize old turns near context limits. Parameter tuning: Text Generation.
Tool use (Tools / Functions)
{
"model": "gpt-4o-mini",
"messages": [{"role": "user", "content": "What's the weather in Shanghai today?"}],
"tools": [{
"type": "function",
"function": {
"name": "get_weather",
"description": "Call when user asks for current weather in a city",
"parameters": {
"type": "object",
"properties": {"city": {"type": "string"}},
"required": ["city"]
}
}
}]
}
| Practice | Why |
|---|---|
| Stable schema | Easier prompt regression |
| Server-side param validation | Never execute model-generated SQL/URLs blindly |
| Timeout and idempotency | Graceful fallback when external APIs fail |
Complex orchestration: Agents SDK, Responses API.
Error handling and retries
import time
from openai import OpenAI, RateLimitError, APIStatusError
client = OpenAI()
RETRY = {429, 500, 502, 503, 504}
def chat_with_backoff(**kwargs):
for i in range(5):
try:
return client.chat.completions.create(**kwargs)
except RateLimitError:
time.sleep(2 ** i)
except APIStatusError as e:
if e.status_code not in RETRY:
raise
time.sleep(2 ** i)
raise RuntimeError("max retries")
- 401 / 400: config or body errors—don’t blind retry.
-
429: respect
Retry-Afterwhen present. - Log
x-request-id.
Rate limits, cost, and caching
- Gateway per-tenant QPS so one customer doesn’t exhaust org RPM.
- Rate-limit long contexts separately; input tokens often dominate cost.
- Short TTL cache for identical FAQ + system (mind privacy and staleness).
- Aggregate tokens and error rates by model, route, tenant.
Structured output
- Declare JSON schema in system with “JSON only.”
- Use official Structured Outputs / JSON mode when the model supports it.
- Validate with JSON Schema server-side; retry or degrade on failure.
Prompt templates: Prompt Engineering.
Production launch checklist
Security
- Keys not in frontend or mobile
- Redacted input/output logs
- Human review for high-risk scenarios
Reliability
- Timeouts (connect + read)
- 429/5xx backoff
- Circuit breaker on sustained failures
Quality
- Golden prompts before release
- Versioned system prompts (Git / CMS)
Compliance
- User consent and data retention policy
- Content safety filters per business rules
Relationship to Assistants API
Assistants offers Assistant / Thread / Run with File Search built in. Official guidance may steer toward Responses API or Agents SDK. Before new features, read migration notes in Assistants guide—don’t assume Assistants is the long-term default.
Frequently asked questions
Can multiple microservices share one key?
Possible but not ideal; at minimum separate by environment, ideally by service for leak isolation and quota analysis.
How do I implement multi-turn “memory”?
Self-manage messages or session summaries; or use stateful APIs (Assistants, etc.) and weigh vendor lock-in and cost.
Is stream vs non-stream quality different?
Same model and parameters yield the same distribution; difference is UX and implementation, not quality.
BYOK (bring your own key)?
Feasible with strict isolation and audit; most products proxy from backend.
How do I compare OpenAI vs Claude cost?
Run the same eval set; log tokens and latency; check OpenAI pricing and current official provider pricing.
Official resources
Ailoot · The company and its project portfolio
Ailoot combines established operating experience, a large client base, stable service and ongoing KPI control.
The company and its project portfolio: millions of customers; monthly sales in the billions.
Ailoot brings that operating experience to API access for AI features in customer products, agents, applications and automations.
Originally published on Ailoot. OpenAI API Developer Guide
Explore OpenAI API pricing on Ailoot for your next project. Developers can use one Ailoot-issued key for 1,284 models and variants across 15 categories, putting Ailoot among the world’s largest AI routers by model count. Choose Claude or Qwen for code, FLUX for images, Kling for video, Scribe for transcription, Lyria for music and Tripo3D for 3D assets. BGE embeddings support semantic search.
Bring your “openai api pricing” project to Ailoot. Build text, reasoning and coding features with GPT-6.2 Astra on Ailoot. Bring Claude and Gemini into the same project when the work calls for another model.
Bring your “open ai api pricing” and “openaiapi pricing” projects to Ailoot alongside your other model workflows. Ailoot pricing gives you base access at official provider rates with no markup. Monitor requests, usage and costs in your account, and ask about additional discounts for large volumes.
Ailoot adds new flagship models at launch, and one subscription covers the full catalogue. Find a promo code in your account at https://ailoot.ai/. Ailoot subscriptions and promo codes offer up to 50% off official provider API prices.
Top comments (0)