Cache & Rate-Limit Upstreams
Protect backends from runaway model calls.
Cache & Rate-Limit Upstreams is a free MCP Academy lesson on CoddyKit — lesson 4 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the MCP Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
Models Can Hammer Backends
An eager model may call a tool again and again. Without guards, it can overload the database or API behind it. ⚠️
Cache Repeated Reads
A cache stores recent results so an identical request returns instantly instead of hitting the upstream a second time.
Key the Cache by Inputs
Build the cache key from the tool arguments, so the same city or id reuses its stored answer and anything new fetches fresh.
key = ("weather", city)
if key in cache:
return cache[key]Give Entries a TTL
Set a time-to-live so cached data expires. Stale answers get refreshed, keeping results both fast and reasonably current.
Cache Only Safe Reads
Cache idempotent reads, never actions with side effects. You would not want a cached write to silently skip a real change.
Rate-Limit the Calls
A rate limit caps how often a tool may run, so a runaway model cannot fire thousands of requests in a burst.
The Token Bucket Idea
A common scheme is the token bucket: each call spends a token, tokens refill over time, and an empty bucket means wait.
Respect Upstream Limits
Match your limit to the API quota. Staying under its quota avoids 429 responses and keeps your access from being throttled.
Back Off on 429
If an upstream replies 429 Too Many Requests, pause and retry later with backoff instead of pounding it harder.
Bound Concurrency
A semaphore limits how many calls hit the backend at once, smoothing spikes so the upstream is never swamped.
sem = asyncio.Semaphore(5)
async with sem:
...Return a Clear Limit Message
When you do throttle, tell the model plainly that it is rate-limited so it can wait or try a different approach.
Quick Check
What kind of result is safe to cache?
Recap: Cache & Limit
You shielded upstreams with a cache plus rate limits and backoff, keeping backends safe from runaway model calls. 🎯
Frequently asked questions
Is the “Cache & Rate-Limit Upstreams” lesson free?
Yes — the full text of “Cache & Rate-Limit Upstreams” is free to read here on the web, and the MCP Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the MCP Academy course, upgrade to CoddyKit PRO.
What will I learn in “Cache & Rate-Limit Upstreams”?
Protect backends from runaway model calls. You practise MCP Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start MCP Academy?
No prior experience is required. MCP Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 4 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Cache & Rate-Limit Upstreams” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this MCP Academy lesson?
Yes. Every MCP Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- Expose SQL Queries Safely
- Wrap a REST API as Tools
- Pool Connections in Lifespan
- Cache & Rate-Limit Upstreams