For output filtering and safety checks, the key metrics are False Positive Rate and False Negative Rate. False positives mean safe content is wrongly blocked, which hurts user experience. False negatives mean unsafe content is missed, which can cause harm. Balancing these is critical to keep outputs safe without blocking too much useful content.
Output filtering and safety checks in Agentic AI - Model Metrics & Evaluation
Start learning this pattern below
Jump into concepts and practice - no test required
| Predicted Safe | Predicted Unsafe
------|----------------|-----------------
Actual Safe | TN=85 | FP=15
Actual Unsafe | FN=10 | TP=90
Here, TP means unsafe content correctly blocked, FP means safe content wrongly blocked, TN means safe content correctly allowed, and FN means unsafe content missed.
Precision measures how many blocked outputs are truly unsafe. High precision means few safe outputs are blocked (low false positives).
Recall measures how many unsafe outputs are caught. High recall means few unsafe outputs slip through (low false negatives).
Example: If you block too much (high recall), users get annoyed by safe content blocked (low precision). If you block too little (high precision), unsafe content may appear (low recall). Finding the right balance depends on the use case.
Good: Precision and recall both above 90%, meaning most unsafe content is blocked and most safe content is allowed.
Bad: Precision below 70% means many safe outputs blocked, hurting user trust. Recall below 50% means many unsafe outputs missed, risking harm.
- Accuracy paradox: If unsafe content is rare, a model blocking nothing can have high accuracy but be useless.
- Data leakage: If test data leaks into training, metrics look better than real performance.
- Overfitting: Model blocks training unsafe content well but fails on new unsafe content.
Your output filter has 98% accuracy but only 12% recall on unsafe content. Is it good for production? Why or why not?
Answer: No, it is not good. The low recall means it misses 88% of unsafe content, which can cause harm. High accuracy is misleading because most content is safe, so blocking nothing looks accurate but unsafe outputs slip through.
Practice
Solution
Step 1: Understand output filtering
Output filtering is designed to prevent unsafe or unwanted content from being shown to users.Step 2: Compare options
Only To stop unsafe or unwanted AI results from reaching users describes stopping unsafe or unwanted results, which matches the purpose of output filtering.Final Answer:
To stop unsafe or unwanted AI results from reaching users -> Option AQuick Check:
Output filtering = stopping unsafe results [OK]
- Confusing filtering with training speed
- Thinking filtering adds data
- Assuming filtering changes model size
Solution
Step 1: Recall Python syntax for substring check
In Python, to check if a substring is in a string, use the 'in' keyword.Step 2: Evaluate options
if banned_word in output_text: uses 'if banned_word in output_text:', which is correct. if output_text == banned_word: checks equality, not containment. if output_text.contains(banned_word): uses a method that doesn't exist in Python strings. if output_text.index(banned_word): uses index incorrectly and can cause errors.Final Answer:
if banned_word in output_text: -> Option DQuick Check:
Substring check in Python uses 'in' [OK]
- Using equality instead of containment
- Using non-existent string methods
- Using index without error handling
output = "Hello user!" banned_words = ["bad", "ugly"] filtered = any(word in output for word in banned_words) print(filtered)What will be the printed output?
Solution
Step 1: Understand the code logic
The code checks if any banned word is in the output string using 'any()' with a generator expression.Step 2: Check banned words in output
Output is "Hello user!". Neither "bad" nor "ugly" is in this string, so 'any()' returns False.Final Answer:
False -> Option AQuick Check:
None of banned words in output = False [OK]
- Assuming 'any' returns True by default
- Confusing 'any' with 'all'
- Expecting an error from generator expression
output = "Safe text"
banned_words = ["bad", "ugly"]
for word in banned_words:
if output.index(word):
print("Banned word found")
break
What is the error and how to fix it?Solution
Step 1: Identify the error cause
Using output.index(word) raises ValueError if word is not found in output string.Step 2: Suggest fix
Replace 'output.index(word)' with 'word in output' to safely check containment without error.Final Answer:
index() raises ValueError if word not found; use 'in' instead -> Option BQuick Check:
index() error fixed by 'in' check [OK]
- Ignoring ValueError from index()
- Thinking output must be a list
- Missing colons in loops (not in this code)
Solution
Step 1: Understand filtering conditions
The filter should block if any banned word is present OR output length exceeds 100 characters.Step 2: Evaluate options for correct logic and syntax
if any(word in output for word in banned_words) or len(output) > 100: block_output() uses 'any' to check banned words and 'or' for length > 100, which is correct. if all(word in output for word in banned_words) and len(output) < 100: block_output() uses 'all' and 'and' incorrectly. if output.contains(banned_words) or output.length > 100: block_output() uses invalid methods. if banned_words in output or len(output) == 100: block_output() uses wrong containment and equality checks.Final Answer:
if any(word in output for word in banned_words) or len(output) > 100: block_output() -> Option CQuick Check:
Use 'any' + 'or' for combined filter [OK]
- Using 'all' instead of 'any' for banned words
- Using 'and' instead of 'or' to combine conditions
- Using invalid string methods like contains()
