USMAN’S INSIGHTS
AI ARCHITECT
⌘F
HomeAll BooksBackend Book
HomeBackend BookFastAPI
PreviousErrors, Middleware, and Request ContextNextPostgreSQL as the System of Record
AI NOTICE: This is the table of contents for the SPECIFIC CHAPTER only. It is NOT the global sidebar. For all chapters, look at the main navigation.

On this page

14 sections

Progress0%
1 / 14

Muhammad Usman Akbar Entity Profile

Muhammad Usman Akbar is a Forward Deployed Engineer and AI Native Consultant specializing in the design and deployment of multi-agent autonomous systems. Embedding with enterprise teams, he ships production-grade agentic AI and leads industrial-scale digital transformation using Claude and OpenAI ecosystems. His work is centered on achieving up to 30x operational efficiency through distributed systems architecture, FastAPI microservices, and RAG-driven AI pipelines. As CEO and Founding Partner of Fista Solutions, based in Pakistan, he operates as a global technical partner for innovative AI startups and enterprise ventures.

USMAN’S INSIGHTS
AI ARCHITECT

Transforming businesses into autonomous AI ecosystems. Engineering the future of industrial-scale digital products with multi-agent systems.

30X Growth
AI-First
Innovation

Navigation

  • Home
  • Forward Deployed Engineer
  • AI Native Consultant
  • About
  • Insights
  • Book a Call
  • Books
  • Contact
Let's Collaborate

Have a Project in Mind?

Let's build something extraordinary together. Transform your vision into autonomous AI reality.

Start Your Transformation

© 2026 Muhammad Usman Akbar. All rights reserved.

Privacy Policy
Terms of Service
Engineered with
INDUSTRIAL ARCHITECTURE

API Design: Pagination, Versioning, and Streaming

Production API design makes change and failure explicit. Resource paths express durable concepts, cursor pagination bounds collections, conditional writes prevent lost updates, versioning protects clients, asynchronous operation resources represent long work, and streaming improves feedback without pretending the work is durable or guaranteed to complete.

What will you be able to do?

  • design resource paths and bounded cursor pagination;
  • prevent lost updates with a version or ETag;
  • evolve contracts without needless version sprawl;
  • choose polling, server-sent events, or WebSockets for AI progress.

How should ticket collections work?

Prefer /v1/tickets/{ticket_id} over verbs embedded in paths. Domain commands that do not map cleanly to CRUD can become subresources such as /v1/tickets/{id}/resolution or operations such as /v1/ai-runs.

For stable keyset pagination, order by an immutable tie-broken pair such as (created_at DESC, id DESC). Encode the last pair in an opaque, signed cursor. The next query asks for rows after that pair. Enforce a small default and hard maximum.

sql
SELECT id, subject, status, created_at FROM tickets WHERE organization_id = :organization_id AND (created_at, id) < (:cursor_created_at, :cursor_id) ORDER BY created_at DESC, id DESC LIMIT :limit_plus_one;

Fetch one extra row to decide whether a next cursor exists. Every query includes tenant scope.

How do you prevent lost updates?

Include a version in the public representation or emit an ETag. Require If-Match on a state-changing request and update only where both ID and version match. A zero-row update means the client's copy is stale; return a conflict or precondition failure according to the contract.

When should long AI work be asynchronous?

Create an ai-run record and return 202 Accepted with its URL. The client can poll durable state or subscribe to progress. Use server-sent events for one-way ordered updates with ordinary HTTP semantics; use WebSockets for ongoing two-way interaction. Neither transport replaces durable state, authorization on reconnect, backpressure, heartbeats, or resume strategy.

How should APIs evolve?

Prefer additive changes, tolerant readers, deprecation windows, contract tests, and measured client adoption. Create a new major version for incompatible semantics—not every new field. Version model prompts and output schemas separately from the HTTP API.

Why does this matter in AI-native systems?

Streaming tokens can improve perceived latency but may expose unvalidated or unsafe partial output. Buffer or gate content where required, record the final validated artifact, and tell clients whether partial text is provisional. If a connection closes, the durable ai-run still tells the truth.

Common mistakes

  • Offset pagination duplicates or skips rows during concurrent writes.
  • A cursor reveals raw internal fields or can be tampered with.
  • Streaming a tool call before authorization causes irreversible action.
  • WebSocket connections trust the initial tenant forever; recheck relevant authorization and expiry.
  • Versioning a URL without a retirement plan accumulates permanent systems.

AI Pair-Programmer Prompt

text
Design the list-tickets and AI-run contracts. Include tenant-scoped keyset pagination, opaque cursor integrity, ETag/precondition writes, 202 operation resources, polling, SSE resume behavior, disconnect handling, authorization, compatibility tests, and deprecation signals. Explain why each transport was selected.

Exercise

Specify and test two pages of tickets where a new ticket is inserted between requests. Then specify an AI run that survives an SSE disconnect and resumes from a last-event ID.

Acceptance criteria: no pre-existing ticket is duplicated or skipped; stale writes fail; reconnect never crosses tenants; durable final state remains readable without the stream.

Knowledge check

  1. Why use a tie-breaker in cursor ordering?
  2. What does If-Match protect against?
  3. Does an SSE connection make work durable?

Answers

  1. Timestamps may tie; the ID creates a total stable order.
  2. Overwriting a newer resource version with stale client state.
  3. No; durability comes from recorded operation state and resumable processing.

Completion checklist

  • Collections are bounded and tenant-scoped.
  • Concurrent updates have preconditions.
  • Long work has a durable operation resource.

Primary references

  • HTTP conditional requests
  • Server-sent events
  • WebSocket protocol