Engineering · AI · Performance
Making Cereby AI Faster: 1 to 2 Seconds Instead of 5 to 8
Every question rebuilt the same context, one query at a time. Three caches, a trimmer and parallel queries stopped it.
A student asks Cereby AI, "What are my weakest topics in calculus?" and waits. Then, "Where am I struggling in calculus?" and waits just as long. (Both questions are synthetic.) In February every request took 5 to 8 seconds, and follow-ups hit the wall hardest: nothing had changed between the two questions, and Cereby rebuilt everything anyway.
Two to three seconds of that went to the context aggregator, the step that gathers a student's quiz results, flashcards, calendar, notes and weak points before every answer. Its queries ran one after another, uncached. The intent was classified again every time, and about 15,000 tokens of history went to the model, most of it irrelevant:
We made that layer remember what it had built, send the model less, and fetch the rest in parallel. (A later post, Making File Chat Faster by Cutting Seven Wasted Queries, covers the same layer on file chat.) The path we landed on:
The same context, every time
The first tier was an in-memory LRU cache (least recently used: when full, it drops the entry untouched longest), holding context for 2 minutes and intents for 5. The second was a database cache with a 5-minute TTL (time to live), shared by every server, surviving restarts and cleaning up its own expired entries. A completed quiz, an updated note or a new calendar event invalidated both. The third tier cached intent classification itself, since our two calculus questions are structurally the same request; it cut redundant AI calls for intent by 30 to 40%.
Most of the payload was about other subjects
A question about calculus does not need quiz history in every subject. The compressor read the classified intent and kept quiz history for that subject, high-severity weak points in that area, and the top entries in each category, so the tail could not grow back:
Its downstream effect surprised us most. A smaller payload meant faster, cheaper inference, and because the model worked from more focused context, answer quality held or improved.
One query after another
In sequence, the aggregator took the sum of its queries, about 2.9 seconds at the median. In parallel, it took as long as the slowest, about 800 ms, before any cache came into it:
Three missing indexes (user and completion date, user and event date, user and subject) took another 40 to 60% off each query.
Before and after
| Metric | Before | After (cache hit) |
|---|---|---|
| Average response time | 5 to 8 seconds | 1 to 2 seconds |
| Context loading time | 2 to 3 seconds | 200 to 400 ms |
| Database queries per request | 5 to 7 | 0 to 1 |
| Tokens per request | About 15,000 | About 5,000 to 8,000 |
| Cache hit rate | 0% | 60 to 70% |
| Cost per request | $0.15 to $0.25 | $0.05 to $0.10 |
Across all requests, database load dropped 60% and API costs 40%.
What this taught us
Caching pays fastest at the layer with the most redundancy. Ours was context aggregation, identical context rebuilt on every request from an active student, and the in-memory tier alone solved most of it.
Token reduction compounds. A smaller context lowered cost and latency on every AI call, cached or not. Filtering a payload to what the question needs is probably the cheapest optimization there is.