cd /news/ai-tools/how-i-protected-my-telegram-bot-s-ai… · home topics ai-tools article
[ARTICLE · art-135226] src=dev.to ↗ pub= topic=ai-tools verified=true sentiment=↑ positive

How I Protected My Telegram Bot's AI Quota From Spam Users

A developer running a Telegram bot on Google's Gemini free tier described building a three-layer quota protection system after a single user sent 1,400 messages in three hours and exhausted the API's 1,500 daily requests, leaving 999+ users with "quota exceeded" errors. The fix combines a per-user rate limit of 20 messages per hour, a global daily cap of 1,400 requests, and an in-memory response cache that the developer says saves 50-70% of quota with no database or extra cost.

by read1 min views1 publishedSep 20, 2026

I run a Telegram bot that uses Google's Gemini API for free-tier AI features. The free tier gives 1,500 requests per day.

Last week, one user sent 1,400 messages in 3 hours.

The result? 999+ users got "quota exceeded" errors. My entire AI feature was dead for the day.

Here's how I fixed it.

Free AI APIs have hard daily limits. One aggressive user can consume the entire quota. There's no native protection unless you build it.

I needed:

def check_rate_limit(user_id):

    now = time.time()

    hour_ago = now - 3600

    uid = str(user_id)

    user_requests.setdefault(uid, [])

    user_requests[uid] = [t for t in user_requests[uid] if t > hour_ago]

    if len(user_requests[uid]) >= 20:

        return False

    user_requests[uid].append(now)

    return True

20 messages per hour per user. 99% users never hit this.

Layer 2: Global Daily Cap

global_counter = {"date": "...", "count": 0}

def check_global_limit():

    today = datetime.utcnow().strftime("%Y-%m-%d")

    if global_counter["date"] != today:

        global_counter["date"] = today

        global_counter["count"] = 0

    if global_counter["count"] >= 1400:

        return False

    global_counter["count"] += 1

    return True

Cap at 1,400 — 100 buffer below the 1,500 limit.

Layer 3: Response Cache

answer_cache = {}

cached = answer_cache.get(question.lower().strip())

if cached:

    return cached

answer_cache[question.lower().strip()] = answer

Common questions like "What is Bitcoin?" get cached. Repeat users hit cache, not API.

The Results

Spam blocked: 20 msg/hour limit

99% users unaffected

Quota saved: 50-70% via caching Zero extra cost: All in-memory, no database

What I Learned

Free tiers are fragile. Build protection before you scale. Rate limiting + caching is the cheapest fix — no infrastructure needed.

Want the full script?

I packaged this as a production-tested template. Includes:

Complete quota_protection.py

Bot integration example

Setup guide

Multi-provider fallback ready

I'm building an automated Telegram empire in public. Follow along for real code, real numbers, no hype.

── more in #ai-tools 4 stories · sorted by recency
── more on @telegram 3 stories trending now
sponsored brought to you by zahid.host 4,200+ EU-deployed projects
reading about agents? ship yours in a single git push.

Run your AI side-project on zahid.host

EU-based hosting, git-push deploys, automatic HTTPS, no cold starts. Free tier with a custom domain — perfect for shipping the agent you just read about.

$git push zahid main
Live at https://your-agent.zahid.host
Get free account → Pricing
from €0/mo · no card required
LIVE [news/how-i-protected-my-t…] indexed:0 read:1min 2026-09-20 ·