Courses3 levels
Level 1 · Foundations11 courses
Level 2 · Core5 courses
Level 3 · Advanced5 courses
Labs8 topics
Library Practice exams AI Range Pricing Start free

Advanced / Detection and incident response for AI

Detection and incident response for AI cheat sheet

Set up the lab (lesson 0)

TaskCommand or code
Make the lab foldermkdir -p ~/krlab/ai-detection-response
Build the lab from the zip as it stands after a lessonsh kr-lab.sh LESSON_FOLDER ~/krlab/ai-detection-response (run it in the unzipped folder; the folder has the name of the lesson file without .md)
Write the data, or reset the statepython3 makelogs.py
Prove that the lab workspython3 labcheck.py (last line lab ready: 6 of 6)
Look at the first recordhead -n 1 logs/week1.jsonl

Decide what to log (lesson 1)

TaskCommand or code
Fields of a recordid ts user prompt_sha prompt_len excerpt resp_len model tools docs decision tokens_in tokens_out cost confidence
Mask secrets before cuttingsanitise(text)[:60]
Make a record without textmake_record(rid, ts, event)
List why a record must not be loggedviolations(record)
Append a record, or refuse itwrite(path, record)
Apply retentionretain(record, now, held=False) (excerpt 7 days, record 90, hold 365). Run the detectors that read the excerpt within 7 days
Run the testspython3 test_logrec.py
Take away the write right, show the modechmod 440 FILE, stat -c '%a %n' FILE
Seal a closed log, then check itsha256sum FILE > FILE.sha256, sha256sum -c FILE.sha256 (exit code 1 when changed)

Detect injection and abuse (lesson 2)

TaskCommand or code
Count each rule on both weekspython3 run_detect.py
Score of a recordphrase 1, long text 1, tool after an ext- document 2; alert at 2
Users with many refusals in a daydetect.refusal_alerts(records)
Narrow the tool ruleNARROW = {"issue_refund", "send_email_external"}

Watch drift and quality (lesson 3)

TaskCommand or code
Confidence from log-probabilitiesmath.exp(sum(logprobs) / len(logprobs))
Day-by-day drift against a baselinepython3 run_drift.py
Population stability indexdrift.psi(baseline, new); drift above 0.2, at least 100 answers
Answers for people to readdrift.review_queue(records) (below 0.5, plus 1 in 20 by hash of the id)
Share records without naming anyoneDrop id, user, prompt_sha, excerpt, docs, model, cost, tokens and lengths; use the day and int(confidence * 10) / 10; remove groups under 5 records (lesson 3, step 6)
Count equal records, test the smallest groupCounter(rows), min(size.values()) over ALL released columns (k-anonymity, k = 5)
Check that pseudonyms can be undoneHash guessed names with pseudonym(), count the users found again (58 of 62)

Watch rate and cost (lesson 4)

TaskCommand or code
Cost report and alertspython3 run_cost.py
Cost of a requesttokens_in * 3e-6 + tokens_out * 15e-6 (invented prices)
Robust z-score0.6745 * (x - median) / MAD; look above 3.5
Users over the hourly limitcost.user_hour_alerts(records) (more than 30)
Top users by costcost.top_users(records)
Four parts of the costprompts, responses, processing (tool calls, documents), storage (lesson 4, step 6)

Spot extraction (lesson 5)

TaskCommand or code
Features of each userextract.features(records)
Table of precision and recallpython3 run_extract.py
Precision, recallright ÷ found, right ÷ real
Flag with the allow-listextract.flagged(feats, extract.RULES["+ not on allow-list"])

Write playbooks (lesson 6)

TaskCommand or code
Alert idsA-INJ, A-REFUSE, A-DRIFT, A-RATE, A-COST, A-EXTRACT, A-LOG-SECRET, A-LEAK
Five sections of a playbook### Trigger, ### First actions, ### Owner, ### Evidence, ### Communication
Check a folder of playbookspython3 check_playbooks.py playbooks
Read the exit codeecho "exit code $?" (1 for a problem or an empty folder)

Contain, roll back, recover (lesson 7)

TaskCommand or code
Disable, enable a toolpython3 respond.py disable-tool send_email_external, python3 respond.py enable-tool send_email_external
Revoke a keypython3 respond.py revoke-key key-mail-gateway
Safe mode on, offpython3 respond.py safe-mode on, python3 respond.py safe-mode off
Pause, resume ingestionpython3 respond.py pause-ingest, python3 respond.py resume-ingest
Limit a user (requests a day, 0 blocks)python3 respond.py limit-user u_3ca43ca6 30
Roll back to a known-good versionpython3 respond.py rollback model orbi-1.2, python3 respond.py rollback index kb-v6
Verify with the test setpython3 respond.py verify (passes at 90 percent)
Read the change recordstate/changes.jsonl

Audit (lesson 8)

TaskCommand or code
Run all four auditspython3 audit.py
Interval for a shareaudit.wilson(correct, total)
Ratio of outcomes between groupslower rate ÷ higher rate; look below 0.8
Tool uses outside the role, permissions that the role never usedaudit.access_audit(records, access)

Build and run the kit (lessons 9 and 10)

TaskCommand or code
Run everything on week 2python3 kit.py (the first column is the alert id that picks the playbook)
Run on another weekpython3 kit.py logs/week1.jsonl
What makes the kit exit 1A secret in the log, a playbook with a problem, or a folder with no playbook (not alerts)
Make the scenario weekpython3 scenario_week.py
Fingerprint of a filesha256sum logs/week3.jsonl | cut -c1-12

Safety habits