Skip to content

Unsafe and custom topic detection

AI Security for Apps can detect when an LLM prompt touches on unsafe or unwanted subjects. There are two layers of topic detection:

  • Default unsafe topics: A built-in set of safety categories that detect harmful content such as violent crimes, hate speech, and sexual content.
  • Custom topics: Topics you define to match your organization's specific policies, such as "competitors" or "financial-advice".

Default unsafe topics

When AI Security for Apps is enabled, it automatically evaluates prompts against a set of default unsafe topic categories and populates two fields:

Default unsafe topic categories

CategoryDescription
S1Violent crimes
S2Non-violent crimes
S3Sex-related crimes
S4Child sexual exploitation
S5Defamation
S6Specialized advice
S7Privacy
S8Intellectual property
S9Indiscriminate weapons
S10Hate
S11Suicide and self-harm
S12Sexual content
S13Elections
S14Code interpreter abuse

Custom topics

Custom topic detection lets you define your own topics and AI Security for Apps will score each prompt against them. You can then use these scores in custom rules or rate limiting rules to block, challenge, or log requests based on a relevance score that you define.

This capability uses a zero-shot classification model that evaluates prompts at runtime. No model training is required.

How custom topics work

  1. You define a list of up to 20 custom topics. Each topic consists of:
    • Label: A short, hyphenated identifier used in rule expressions and analytics (for example, financial-advice).
    • Topic description: The descriptive text the model uses to classify prompts (for example, seeking financial advice).
  2. When a request arrives at a cf-llm labeled endpoint, the model evaluates the prompt against all defined topic descriptions and returns a relevance score for each.
  3. Scores are written to the cf.llm.prompt.custom_topic_categories map field, keyed by label. You use labels (not topic descriptions) in rule expressions and analytics.

Define custom topics

You can manage custom topics from two places in the dashboard:

  1. Security Settings page: Go to Security > Settings and search for the AI Security for Apps section. Under Custom Topics, select Manage topics to add, edit, or remove topics.
  2. Expression builder sidebar: When creating or editing a custom rule, select the LLM Custom topic field. Then, select Manage custom topics to open a sidebar where you can manage topics without leaving the rule creation page.

Both methods will update the same underlying topic list. Changes made in one are immediately reflected in the other.

  1. In the Cloudflare dashboard, go to the Security Settings page.

    Go to Settings

    Alternatively, go to the custom rules creation page, select the LLM Custom topic field, and select Manage custom topics to open the sidebar.

  2. Add a topic by providing:

    • Label: A short, hyphenated identifier (for example, competitors).
    • Topic Description: A descriptive English phrase the model uses for classification (for example, seeking info on competitors).
  3. Select Save.

Constraints

ParameterLimit
Maximum number of topics20
Topic string length2–50 printable ASCII characters
Label length2–20 characters
Label formatLowercase letters, numbers, and hyphens (-) only

Best practices for defining custom topics

The quality of custom topic detection depends on how you write your topic descriptions. The underlying model is a zero-shot classifier that compares the semantic meaning of the prompt against your topic description.

The most important thing to do is to describe the user's intent, not just the subject.

Lead with intent

The model performs semantic classification, not keyword matching. Topic descriptions that capture what the user is trying to do are significantly more accurate than descriptions that simply name a subject area. A short verb phrase (3–6 words) is usually the best trade-off between precision and coverage.

Compare how the same two topic descriptions perform against two prompts that both mention finance but with very different intent:

Topic descriptionPrompt: "Should I invest my savings in index funds?"Prompt: "Our finance team just finished the Q3 report."
financial advice (noun only)MatchesAlso matches. The word "finance" appears, even though no advice is being sought
seeking financial advice (intent phrase)MatchesCorrectly ignored. Mentions finance but has no advice-seeking intent

Example: competitors

QualityTopic descriptionWhy
Bestseeking info on competitorsCaptures intent. Only fires when users are actively asking about competitors
OkayAcme Corp, Banana Co, Candy & SonsWorks for known names but misses unnamed competitors and catches casual mentions
Avoidother companiesFar too vague. Matches nearly any prompt that mentions a business

Example: financial-advice

QualityTopic descriptionWhy
Bestseeking financial adviceIntent-driven. Matches users asking for guidance, ignores passive mentions of finance
Okaysecurities and investmentsReasonable subject scope but fires on news articles and factual mentions, not just advice-seeking
AvoidfinanceExtremely broad. Matches almost everything from expense reports to pricing questions

More best practices

  • Be specific. Overly broad topics cause false positives; overly narrow topics cause false negatives.
  • Avoid semantic overlap. If two topics mean nearly the same thing (for example, seeking financial advice and asking for investment guidance), they will score similarly on the same prompts and waste your 20-topic budget.
  • Test and iterate. Send test prompts and review scores in Security Analytics. You can tune by adjusting the topic description (more or less specific) or the score threshold in your rule (lt 20 is strict, lt 50 is permissive).
  • Do not list multiple values in one topic description. The model only evaluates against the first item in a comma-separated list. For example, Toyota, Ford, Audi, BMW will only match prompts about Toyota — the remaining items are ignored. Removing the commas does not improve results. Use a single intent-driven phrase such as seeking info on competitors, or create separate topics for each value.

Example custom topics

LabelTopic description
competitorsseeking info on competitors
financial-adviceseeking financial advice
legal-adviceasking for legal or regulatory advice
sensitive-datarequesting passwords or API keys
job-seekingasking about job openings or careers
biascomparing demographic groups as better or worse