Skip to main content

Claude/ChatGPT Prompt to Design Cursor-Based Pagination That Scales

Replace offset pagination with scalable cursor-based pagination for large API collections, with stable ordering, prev/next links, and a migration plan.

Fill in the placeholders

Edit the values, then copy your finished prompt.

Your Prompt
prompt.txt
You are a senior API engineer. Replace offset pagination with cursor-based pagination and specify it tightly enough to build — return concrete request/response shapes, not hand-waving.

Context:
- Endpoint: GET /v1/orders
- Sort order: created_at DESC
- Page size: 50
- Backing store: PostgreSQL 16

Deliverables:
1. Cursor format: opaque base64 of (sort key + tie-break id), and why a composite key is required.
2. Order stability under concurrent inserts/updates/deletes — no skipped or duplicated rows.
3. next/prev link generation and a clear has_more / end-of-list signal.
4. Total-count handling: exact vs estimated vs omitted, and the cost tradeoff.
5. Migration plan so existing offset clients keep working during rollout (dual support, deprecation window).
6. Index requirements on PostgreSQL 16 to keep each page O(log n).

Output: annotated request/response JSON for first page, next page, and last page, plus the index DDL.

What this prompt does

This prompt directs the AI, as a senior API engineer, to replace offset pagination with cursor-based pagination and specify it tightly enough to build - returning concrete request and response shapes, not hand-waving. You supply the [endpoint], the [sort_order], the [page_size], and the [datastore], and it returns annotated request/response JSON for the first, next, and last pages plus the index DDL.

The deliverables are sequenced to cover every place offset pagination fails. It defines an opaque base64 cursor built from the sort key plus a tie-break id and explains why a composite key is required, guarantees order stability under concurrent inserts, updates, and deletes, generates next/prev links with a clear has_more signal, handles total-count tradeoffs, lays out a migration plan so existing offset clients keep working during rollout, and specifies the index on [datastore] to keep each page O(log n). The [sort_order] and tie-break together are what make cursors stable; [datastore] decides the exact index DDL.

When to use it

  • A list endpoint gets slower on every page because offset pagination scans and discards rows.
  • Rows are duplicated or skipped when someone writes to the collection mid-scroll.
  • You need stable ordering on a high-write collection under concurrent inserts and deletes.
  • You are migrating an existing API and must keep offset clients working during the transition.
  • You need the exact index to keep each page fast on your specific database.

Example output

You get annotated request and response JSON for three cases - first page, next page, and last page - showing the opaque cursor, the next/prev links, and the has_more or end-of-list signal. Alongside that, you get the index DDL for [datastore] and a migration plan describing dual support for offset clients and a deprecation window.

Pro tips

  • Nail order stability (deliverable two) first; ordering bugs that skip or duplicate rows are the ones that reach support tickets.
  • Set [sort_order] to a field with a reliable secondary tie-break, because a non-unique sort key alone produces unstable cursors.
  • Match [datastore] to your real database and version so the index DDL is directly usable.
  • Choose [page_size] deliberately; larger pages reduce round-trips but increase per-request cost and memory.
  • Decide total-count handling up front - exact counts are expensive on large collections, so estimated or omitted is often the right call.
  • Keep the offset endpoints alive through the migration window; cutting them over instantly breaks any client mid-scroll.
  • Make the cursor opaque base64 so clients treat it as a token and cannot construct their own, which keeps you free to change its internals later.
  • Watch the prev-direction case as carefully as next; reverse pagination has its own off-by-one and ordering pitfalls that are easy to miss in a quick test.

Frequently Asked Questions

Why is a composite cursor key required instead of just the sort field?
If the sort field is not unique, two rows can share the same value, and a cursor built only on that field cannot tell them apart - leading to skipped or repeated rows. Combining the sort key with a unique tie-break id makes every cursor position unambiguous.
How does cursor pagination stay stable when rows are inserted mid-scroll?
Because the cursor encodes a position in the sort order rather than a numeric offset, new inserts before the current position do not shift the page boundaries. The prompt explicitly requires order stability under concurrent inserts, updates, and deletes so no rows are skipped or duplicated.
Can I migrate without breaking existing offset-based clients?
Yes - the prompt asks for a migration plan with dual support and a deprecation window, so offset clients keep working while new clients adopt cursors. You control how long both run before retiring the offset path.
Does the prompt include the database index I need?
It returns index DDL for your `[datastore]` chosen to keep each page O(log n) rather than a full scan. You should still verify the index against your actual query plan, since real-world indexes depend on your other filters and columns.
Engr Mejba Ahmed

Need this built for real?

Engr Mejba Ahmed

AI Developer · Software Engineer

I'm Mejba — I design and ship production AI systems, automations, and full-stack apps. If you want this turned into a working solution for your team, let's talk.

More in API Development Prompts

Engr Mejba Ahmed

Engr Mejba Ahmed

AI assistant · trained on my work

👋

Hey there!

Quick Actions

WhatsApp Direct line to me

Chat on WhatsApp

+880 1723 741224 · Replies within the hour on working days

Popular Questions

Engr Mejba Ahmed is connected
Engr Mejba Ahmed is typing...
Engr Mejba Ahmed avatar

✉ Want me to follow up? Drop your email

Engr Mejba Ahmed avatar

📞 Connect Directly

Choose how you'd like to reach me

WhatsApp

+880 1723 741224

Email

mejba.13@gmail.com

✓ Details sent! I'll get back to you shortly.

Powered by OpenAI

335+

Blog Posts

25

AI Courses

63

Projects

Services & Expertise

Pricing & Process

Learning & Resources

Connect & Support