Chat completions
OpenAI-compatible POST /v1/chat/completions — auth, validation, provider forward, and optional Idempotency-Key replay.
The proxy exposes an OpenAI-shaped chat endpoint for SDK drop-in compatibility. After auth, agent verification, rate limits, and request normalization, the proxy forwards to the configured LLM provider (mock or live).
Endpoint
/v1/chat/completionsOpenAI-compatible chat completions. Gateway alias: POST /proxy/v1/chat/completions (same handler).
Decision records: ADR-0012 (normalization), ADR-0035 (Idempotency-Key). Path org_id on org-scoped probes must match the token org.
Required headers
| Parameter | Type | Description |
|---|---|---|
AuthorizationRequired | string | Bearer PAT issued by auth service. |
X-IBEX-Agent-IDRequired | string (uuid) | Calling agent; must belong to the token organization. |
Content-TypeRequired | string | Must be application/json (charset suffix allowed). |
Optional headers
| Parameter | Type | Description |
|---|---|---|
Idempotency-Key | string | Opaque client key (max 256 chars). Within TTL (default 24h), the same key + same request fingerprint replays the first completion without a second upstream call. Redis key: idempotency:{org_id}:{key} (org from token). See ADR-0035. |
X-IBEX-Session-ID | string | Sticky session external id (max 64 chars). Missing/invalid values are minted and echoed on the response. Durable sessions require POSTGRES_DSN — see Sessions. |
Idempotency behavior
- Missing header: unchanged — every request calls the provider.
- Hit (same key + fingerprint): replay stored HTTP status and body; no provider call; no second session checkpoint / trace.
- Conflict (same key, different body):
409withIDEMPOTENCY_KEY_REUSE. - In progress:
409withIDEMPOTENCY_IN_PROGRESS— retry after a short backoff. - Transient upstream errors (429 / 5xx): pending claim is released so the same key can retry.
- Streaming:
stream=truewithIdempotency-Key→400 VALIDATION_ERROR(non-streaming only in this milestone). - Redis down / timeout: fail-open — request proceeds without dedupe (billing risk under Redis outage).
- The key is not forwarded to OpenAI.
Request body (parsed fields)
Per ADR-0012, the proxy decodes OpenAI-shaped JSON and ignores unknown top-level keys:
| Parameter | Type | Description |
|---|---|---|
modelRequired | string | Model identifier (validated in ADR-0013; max 256 chars). |
messagesRequired | array | Objects with role and content strings; max 1000 messages. |
stream | boolean | Optional OpenAI fields (temperature, max_tokens) also parsed. Idempotency-Key requires stream=false. |
After successful parse, the proxy logs metadata only (org_id, request_id, model, message_count, stream) — never message content.
Response
Successful non-streaming completions return the upstream OpenAI-shaped JSON with status from the provider. Response headers include X-Request-ID, X-Trace-ID, and X-Response-Time per ADR-0013. Session sticky header X-IBEX-Session-ID may be set when session lifecycle is enabled.
Mapped provider failures use the stable error envelope (PROVIDER_TIMEOUT, RATE_LIMITED, etc.).
Try it locally
IDEM_KEY="$(uuidgen)"
BODY='{"model":"gpt-4o","messages":[{"role":"user","content":"hello"}]}'
curl -s -w "\nHTTP %{http_code}\n" \
-X POST http://localhost:8080/v1/chat/completions \
-H "Authorization: Bearer ${IBEX_DEV_TOKEN}" \
-H "X-IBEX-Agent-ID: ${IBEX_DEV_AGENT_ID}" \
-H "Content-Type: application/json" \
-H "Idempotency-Key: ${IDEM_KEY}" \
-d "${BODY}"
# Retry with the same key + body to verify replay (no second upstream completion):
curl -s -w "\nHTTP %{http_code}\n" \
-X POST http://localhost:8080/v1/chat/completions \
-H "Authorization: Bearer ${IBEX_DEV_TOKEN}" \
-H "X-IBEX-Agent-ID: ${IBEX_DEV_AGENT_ID}" \
-H "Content-Type: application/json" \
-H "Idempotency-Key: ${IDEM_KEY}" \
-d "${BODY}"With IBEX_LLM_MODE=mock, expect HTTP 200 and an OpenAI-shaped JSON body. The second request reuses the same Idempotency-Key and body so replay can be verified without a second upstream completion.
Auth + agent verify
ValidateToken and ValidateAgent over gRPC before body handling completes.
Parse + validate JSON
OpenAI-shaped body normalized per ADR-0012/0013.
Idempotency claim (optional)
When Idempotency-Key is set, claim or replay via Redis (ADR-0035).
Provider forward
Mock or live Complete; commit idempotency record on terminal response.
Related
- HTTP route inventory — all implemented proxy paths
- Sessions — sticky
X-IBEX-Session-ID - Quickstart — five-minute local path
- Authentication — header requirements
- ADR-0035 — Idempotency-Key design
Was this page helpful?
Last updated on