Prompt Injection as a Cost Attack: How Legitimate Requests Drain an AI App's Budget
Prompt injection in an AI app is a cost attack, not just a security flaw. Legitimate keys can drain budgets through long outputs, context padding and agent tool loops. Four burn paths, token-based limits, per-key budgets, and where Cloudflare AI Gateway fits.
Short answer: This is not an intrusion — it is allowed calls spending your money. The dangerous version of prompt injection in an AI app is not making the model say the wrong thing; it is inducing legitimate accounts and legitimate keys to repeatedly take the most expensive inference path: long outputs, full context windows, agent tool loops, and cache-bypassing routes to the priciest model. Every request looks valid to the WAF, so rule matching may not fire, while the bill explodes in hours. This “cost-based DoS” (economic denial of service) is not defeated by recognizing attack signatures but by metering tokens, budgeting per user/key, and capping output and tool calls — authoritative controls that live in your application. Cloudflare provides observability, rate limiting, caching and bot governance; it does not understand prompt semantics for you.
Frame it correctly: this is a cost-based DoS, not an intrusion
Classic API abuse has a “bad actor + bad credential” signature — stolen keys, credential stuffing, endpoint scraping. WAF and risk controls can find anomalies in the request. Cost-oriented prompt injection flips that: the account is real, the key is real, and the payload matches the contract. The attacker merely induces the model to make “legal but expensive” decisions.
The consequences:
- Each individual request passes validation, and WAF rules may not match, because no part of the payload looks like an attack.
- Loss does not show up as a data breach; it shows up as an inference bill — unauthorised spend, quota gone, tokens burned.
- It does not need to take the server down. It only needs the service to keep obliging on the most expensive path, which is denial of service in economic terms (LLM denial of service).
The measurement bar shifts from “requests per minute” to “tokens and dollars burned per minute”. Two calls can differ in cost by tens of times (exact multiples depend on the model and pricing — check the vendor’s current price list).
Four burn paths and the signals to watch
| Burn path | Why traditional rate limiting misses it | Signal to watch |
|---|---|---|
Inducing very long max_tokens output |
Counting by “request”, one long generation and one short Q&A both count as 1 | Long tail of output tokens per request; average output length suddenly rising |
| Crafting oversized prompts to fill context | Request frequency is normal, but each input fills the window | Input token distribution shifting right; context window occupancy persistently high |
| Agent loops calling tools repeatedly | Every tool call is a separate, compliant request | Tool calls per session; repeating call patterns and loops |
| Bypassing cache to force the priciest model | Randomised parameters or route switching make headers look perfectly normal | Cache-hit rate collapsing; model mix shifting toward expensive models |
Do not stare only at QPS. All four paths share one trait: “request count unchanged, per-request cost explodes.” If your dashboard only shows requests/min, you are blind to this class of attack. What belongs on the dashboard is input tokens, output tokens, cache hit rate, model mix, and cost per user.
A layered defence checklist
Split the six layers by “who owns them”:
- Provided by the vendor / model API: the
max_tokensparameter, theusagetoken field on every call, and the model price list — the raw material for metering. - Your application must own: the token-counting ledger, per-user/per-key cost budgets, the agent loop breaker, and semantic-layer prompt filtering. Following the reference article’s口径, the authoritative rate-limit counter belongs in the application layer, because it must accumulate across requests and data centres and cannot be left to a mitigation delay of a few seconds.
- Borrowable from Cloudflare: AI Gateway request-level observability and governance (logs, per-key association, caching), Rate limiting rules, Turnstile to block automated sign-ups, and bot signals. These “see” and “block coarsely”; they do not interpret prompts.
Cloudflare capabilities vs. what your application must own
| Item | What Cloudflare provides | What your application must do |
|---|---|---|
| Request-level observability and governance | AI Gateway unified gateway: request logs, per-key/consumer association, caching and routing (exact quotas per official current docs) | Fold token usage into your own ledger; define “what counts as abnormal cost” |
| Rate limiting | Rate limiting rules: rules Free 1 / Pro 2 / Business 5 / Enterprise 100; IP-only characteristics on Free/Pro, richer keys from Business; counters accumulate per data centre with a few seconds of mitigation delay | Authoritative token/cost counting and a hard budget that accumulates across data centres |
| Caching | AI Gateway caching; semantic-cache capability per official current docs; hit rate observable | Cache key design, semantic-match logic, boundaries of what may be cached |
| Sign-ups and bots | Turnstile blocks automated sign-ups; bot signals from Bot Fight Mode on Free to Bot Management on Enterprise | Account lifecycle and quota issuance policy |
| Prompt semantics | Not provided — Cloudflare does not understand prompt semantics or filter injection payloads | Semantic-layer prompt filtering, injection detection, agent loop breaker |
Boundaries and non-applicable cases
- Trusted internal callers are lower risk, but still need a token ledger; insiders can write infinite loops through bugs too.
- Do not treat AI Gateway as a cost ceiling. It provides observability and governance, but the authoritative budget counter stays in your application — otherwise a few seconds of mitigation delay is enough to burn a night’s budget.
- Request-based rate limiting is no substitute for token metering. They complement each other: the first stops floods, the second stops “few but very expensive” requests.
- Semantic caching is not bulletproof. Approximate matching can mis-hit; keep privacy-sensitive or strongly consistent outputs out of shared caches.
FAQ
Can a WAF stop prompt injection? Do not count on it. A WAF matches attack signatures in requests, while every request of a cost-based injection is syntactically legal. Keep the WAF for ordinary web attacks, but token metering and cost budgeting must be solved in the application layer.
Is request-based rate limiting enough? No. One long output and one short Q&A are both “1 request” but can differ in cost by tens of times. Extend rate limiting with token and cost dimensions, or implement an authoritative budget in your application.
Can AI Gateway cap costs? It provides request-level observability, caching and governance, so you can “see” anomalies, but it does not enforce a hard cost ceiling for you; the exact capability range is per official current docs. The actual capping action (reject when over limit) belongs in your application.
How do I detect tokens being burned? Watch whether the cost curve diverges from the business curve: tokens up while business is flat, a single user’s cost jumping into the long tail, cache-hit rate collapsing, and model mix shifting toward the priciest models. Turn these four into alerts rather than reconciling the bill after the fact.
Related reading
Run the same check on your domain
Enter a domain and see its DNS hosting, mail authentication and Cloudflare status in 30 seconds. Free, public records only.
Public DNS records only. No intrusive scanning. · Used only to send this diagnosis — no marketing blasts.
Sources
Official Cloudflare documentation. Where this article mentions availability, quotas or limits, these pages are the source of truth. Cloudflare features change over time — confirm what is visible in your dashboard before committing to a design.