Backendcore
API design for AI features
Sync, streaming or async job — pick by how long the work takes and who waits for it.
What it is and why it exists
Short answers (< ~10 s) fit a sync or streamed request. Long work (documents, research, batch) should return 202 Accepted with a job resource the client polls, subscribes to or gets a webhook for. Good AI APIs also have idempotency keys on POSTs, consistent error bodies, versioning, pagination, and usage returned to the caller.
Quick prototype
POST /chat that blocks until the model finishes.
In production, at scale
POST /conversations/{id}/messages streaming over SSE; POST /jobs → 202 + Location; GET /jobs/{id}; webhooks on completion; Idempotency-Key header; RFC 9457 problem+json errors; rate-limit headers; versioned paths or headers.
Vendors and tools
| FastAPI | Python APIs with Pydantic validation and OpenAPI |
|---|---|
| OpenAPI | Contract-first design |
| Stripe / OpenAI API design | Good reference designs |
On AWS: API Gateway (REST/HTTP/WebSocket APIs) in front of Lambda or ECS; ALB for long-lived streaming connections.
How it fails
- 30-second gateway timeouts killing long requests
- Retries creating duplicate jobs
- Error strings the client can’t act on
What the interviewer asks next
Design the API for “summarise this 200-page PDF”.
POST /documents (upload via presigned URL), POST /summaries {document_id} with Idempotency-Key → 202 {job_id}; GET /summaries/{job_id} for status and result; optional webhook; SSE for progress if the UI needs it.
Why return usage in the response?
Callers (and your own frontend) can show cost, enforce budgets and debug — and it makes billing disputes easy.