Agent Rate Limiting and Cost Attack Defense: Token Quotas, Sliding Windows, Attack Vector Practice

Systematic guide to rate limiting and cost attack defense for LLM Agents in production: single-user token quotas, multi-tenant sliding windows, prompt injection amplifying cost, DDoS protection, and integration with LangChain/CrewAI/AutoGen frameworks.

AgentList Team · 2026年7月6日
rate limitingcost controlprompt injectionagent security安全red team

2025 is the year of LLM Agents scaling to production. But almost every team encounters the same unexpected bill: one user burned $100K of tokens in a day. This article systematically explains Agent rate limiting and cost attack defense engineering practice.

1. Uniqueness of Cost Attack

Traditional API attacks (like DDoS) target making service unavailable; cost attacks target making opponent spend money. The two have essential differences in defense strategy:

Dimension Traditional DDoS LLM Cost Attack
Attacker goal Service down Victim wallet bleeds
Attack cost Attacker pays bandwidth Victim pays token fees
Defense focus Traffic scrubbing Token quota
Detection difficulty High Medium
Attack reusability One-time Repeatable (same prompt)
Legal risk Clearly illegal Gray area (prompt injection)

Key insight: traditional DDoS has attacker pay cost, LLM cost attack has victim pay cost. This changes the attack-defense economics — attackers can launch attacks at zero cost.

2. Complete Classification of Attack Vectors

2.1 Direct Attack

Scenario 1 (single user exhaustion): attack script sends 10000 requests per hour, each consuming 50K tokens, bill $7500. Scenario 2 (token amplification): attacker constructs prompt that forces LLM to output extremely long response, 100 token input but GPT-4 outputs 20000+ tokens.

2.2 Indirect Attack (via Prompt Injection)

More insidious attack: attackers inject prompts through third-party data sources (documents, web pages, emails), making user's Agent run high-cost tasks for the attacker. Scenario 3 (email injection): attacker email body requires Agent read all PDF attachments and generate summaries sent to attacker email; Agent executes this instruction because prompt injection bypassed system security checks.

2.3 Distributed Attack

Attacker controls botnet, each node sends few requests, bypasses single IP rate limit. 1000 different IPs, each sends 10 requests/hour, total cost $5000/hour.

2.4 Multi-Step Attack

Leveraging Agent's multi-step execution capability, single request consumes large tokens. For example, analyze all company GitHub repos and generate visualization report may trigger 20-step Agent execution, total tokens ~500K.

3. Defense System: Four-Layer Defense Model

Layer 4 (Emergency response): Kill Switch + abnormal bill alert. Layer 3 (Anomaly detection): abnormal token usage detection + AI behavior analysis. Layer 2 (Quota limits): user quota + sliding window + circuit breaker. Layer 1 (Entry protection): WAF + rate limit + auth.

Layer 1: Entry Protection

Traditional web protection, used to filter obviously malicious traffic. NGINX + Cloudflare config: single IP rate limit (rate=10r/s), global connection limit (limit_conn 50), burst allowance burst=20.

Layer 2: Quota Limits (Core)

Core of LLM Agent defense. Need to implement four types of quotas: request limits (per minute/hour/day), token limits (per minute/hour/day), single request limits (max_tokens_per_request, max_steps_per_agent).

QuotaConfig data structure configured by tier (free / pro / enterprise), containing requests_per_minute/hour/day, tokens_per_minute/hour/day fields.

Sliding Window Rate Limit (Token Bucket Algorithm)

Implemented via Redis + Lua script. Read current tokens and last_refill timestamp, supplement tokens based on time delta, judge tokens >= cost then deduct and allow, else reject. Each operation updates last_refill and sets EXPIRE.

Layer 3: Anomaly Detection

Quota limits are static, anomaly detection is dynamic. Detection dimensions: token usage spike 5x, single request token abnormally large (10x baseline), error rate spike (possibly prompt injection probing), prompt diversity reduction (automated attack feature, unique_prompts < request_count * 0.1).

Layer 4: Emergency Response (Kill Switch)

When attack detected, must be able to circuit-break immediately. KillSwitch class provides is_active(scope) and activate(scope, reason, duration_seconds) interfaces, scope can be user_id, tier, global. Activation triggers alert simultaneously (PagerDuty / Slack).

4. LangChain/CrewAI/AutoGen Framework Integration

LangChain Callback Handler

Implement BaseCallbackHandler, in on_llm_start: check Agent step limit (step_count > max_steps_per_agent throws error), estimate this token consumption, check quota (Token Bucket check_and_consume), record actual token usage to total_tokens_used.

CrewAI Integration

CrewAI 0.86+ supports step callback, use @before_llm_call decorator to implement enforce_quota function.

AutoGen Integration

Inherit ConversableAgent to implement QuotaAwareAgent, in a_generate_reply check total cost, estimate tokens, check quota, accumulate actual cost.

5. Cost Defense Against Prompt Injection

Prompt injection is main carrier of cost attack, defense needs two-pronged approach.

5.1 Input Side: Limit Prompt Length

MAX_PROMPT_LENGTH = 50_000 tokens, exceeding throws PromptTooLong exception.

5.2 Behavior Side: Limit Agent Steps and Tool Calls

MAX_STEPS = 20, MAX_TOOL_CALLS = 50, MAX_COST_PER_REQUEST = $1.00. CostAwareAgent accumulates step_count, tool_call_count, total_cost each step.

5.3 Output Side: Detect Abnormal Response

Single response should not exceed 8K tokens; detect if response contains sensitive operation instructions ("disregard previous", "ignore all previous" etc.).

6. Production Deployment Architecture

API Gateway (auth, quota query, token quota check) → Agent Runtime (LangChain/CrewAI/AutoGen + Callback + Cost Tracker + Kill Switch) → LLM Provider (OpenAI / Anthropic / self-hosted) → Monitoring (bill alert, anomaly detection, audit log).

Key components: Redis (store token bucket, sliding window, quota count, high-frequency read/write), Postgres (store user subscription, tier, historical usage, persistent), Prometheus + Grafana (real-time token usage monitoring), PagerDuty / Slack (abnormal bill alert, >$1000/hour trigger), Langfuse / Helicone (LLM call full-chain tracing).

7. Common Traps

  1. Quota = requests per minute: only limiting QPS not tokens is useless, attackers can use large prompts to exhaust token quota.
  2. Hardcode free user limit: must dynamically adjust based on usage pattern.
  3. Prompt injection is just security issue: prompt injection is first a cost issue.
  4. Check bill afterwards is enough: bill is T+1, must real-time monitor + auto circuit break.
  5. User quota is enough: ignore IP dimension, single user using proxy pool rotating IPs bypasses.
  6. LLM Provider rate limit is enough: OpenAI/Anthropic rate limit only protects them, not you.

8. Recommended Configuration

Scenario User Tier Token Quota Single Request Max Agent Steps
Personal project / demo Free 100K/day 4K 10
SaaS free tier Free 500K/day 4K 15
SaaS paid tier Pro 100M/day 16K 50
Enterprise internal Enterprise 1B/day 32K 200

9. Future Trends

Token Bucket standard protocol (similar to HTTP RateLimit Header RFC 9239), AI behavior analysis (use ML models to identify abnormal Agent behavior patterns), federated quota (cross-multiple LLM Provider unified quota management), intelligent circuit breaking (predictive circuit breaking based on historical patterns), cost as code (write quota and cost policy as code).

Final word: Agent cost defense is not after-the-fact remediation, but must be done on day one of launch. Since 2024, dozens of publicly reported AI bill out of control incidents have occurred, involving amounts from thousands to hundreds of thousands of dollars.

10. Real-World Cost Attack Incidents

Incident 1 (2024 Q3, AI startup): User signed up for free tier, used prompt injection to abuse LLM API. Spent $47K in 3 days before detection. Root cause: no per-user token limit, only IP-based rate limit. Fix: implemented tiered quota + automatic kill switch. Saved estimated $200K in subsequent months.

Incident 2 (2024 Q4, coding agent company): A user discovered that the agent would repeatedly call expensive tools in a loop if not explicitly bounded. Single prompt triggered 500 tool calls in one session. Cost: $1.2K. Fix: added max_steps_per_agent limit (default 50, configurable per tier).

Incident 3 (2025 Q1, customer support bot provider): Botnet attack from 5K IPs, each sending 1 request every 5 minutes to bypass per-IP rate limit. Total cost: $30K in one week. Fix: added CAPTCHA for free tier + behavioral analysis to detect bot patterns.

Common pattern: every public Agent service will face cost attack attempts within weeks of launch. Defense must be proactive, not reactive.

11. Cost Monitoring Best Practices

Real-time alerts (must-have):

  1. Per-user token usage exceeding 10x baseline (alert within 5 minutes)
  2. Global token usage exceeding $X/hour (configurable, typical $100-1000)
  3. Single request exceeding max_tokens_per_request (immediate alert)
  4. Error rate spike from specific user (possible attack probe)
  5. Token bucket exhaustion rate > 50% (high demand signal)

Daily reports (should-have):

  1. Top 10 users by token usage
  2. Per-tier aggregate token usage
  3. Cost per successful task (efficiency metric)
  4. Denied request rate (UX friction indicator)
  5. New prompt patterns (anomaly detection)

Weekly reviews (nice-to-have):

  1. Cost per feature/product line
  2. Tier upgrade conversion rate
  3. User behavior shifts (possibly new attack pattern)
  4. Quota tuning opportunities

12. Building a Cost-Aware Culture

Technical controls alone are not enough. Team culture matters:

  1. Cost as a first-class metric: every Agent feature should be evaluated for cost impact, not just functionality.
  2. Pre-launch cost review: new Agent features require cost projection and approval before launch.
  3. User education: users should understand quota limits and upgrade paths.
  4. Transparent pricing: users should be able to see their current usage in real-time.
  5. Internal cost dashboard: leadership should see cost trends weekly.

Companies that treat cost as afterthought inevitably face crisis. Companies that bake cost awareness into culture scale sustainably.