INTEGRITY Cloudflare Docs

Token counting

AI Security for Apps (formerly Firewall for AI) provides an estimated token count for each incoming LLM prompt. This lets you monitor prompt sizes, set limits on overly long prompts, and track token usage across your AI endpoints.

How token counting works

When AI Security for Apps processes a request to a cf-llm labeled endpoint, it calculates an approximate token count for the prompt content. The result is available in the LLM Token count (cf.llm.prompt.token_count) field, which you can reference in rule expressions and view in analytics.

Use cases

Block oversized prompts

Set a hard threshold to block prompts that exceed a certain estimated token count. This prevents unexpectedly large inputs from reaching your model.

Rate limit large prompts

Create a rate limiting rule that restricts the number of large prompts a single client can send within a time window. This helps prevent abuse where attackers send excessively long prompts to consume model resources.

Enter the following rule expression in the editor:
(cf.llm.prompt.token_count gt 2000)

Set the rate to, for example, 10 requests per minute per IP, with an action of Block or Managed Challenge.

Combine token count with other detections

Target large prompts that also show signs of prompt injection — a common pattern where attackers pad injection attempts with long context.

Example rule expression:
(cf.llm.prompt.token_count gt 3000 and cf.llm.prompt.injection_score lt 50)

Important considerations