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

Core / Securing LLM applications

Securing LLM applications cheat sheet

Set up (lesson 0)

TaskCommand or code
List the models of the local servercurl -s http://127.0.0.1:11434/api/tags
Prove that the lab workspython3 labcheck.py

Build the bot and break it (lesson 1)

TaskCommand or code
Ask the bot with a seed and samplingbot.ask(prompt, seed=0, temperature=0.7, max_tokens=60)
Run each attack N times and count successesrun(ask, TESTS)
Print an answer with its quotes and line endsrepr(answer)
Measure the baselinepython3 attack.py

Indirect injection (lesson 2)

TaskCommand or code
Summarise a reviewbot.summarise(review, seed=0)
Measure the hidden-line testspython3 attack2.py
Add a sentence to the summary prompt and measurepython3 try_fix2.py

Secrets (lesson 3)

TaskCommand or code
Measure the leak of a code in the promptpython3 leak.py
Hash a code with SHA-256hashlib.sha256(code.encode()).hexdigest()
Compare two hashes without a timing leakhmac.compare_digest(given, stored)
Measure the bot with the code kept in the applicationpython3 fixed.py

Sensitive data (lesson 4)

TaskCommand or code
Put only one customer record in the promptsystem_one(user)
Remove e-mails and card-like numberspii.redact(text)
Keep only letters and digits, in lower casesquash(text): re.sub(r"[^a-z0-9]", "", text.lower())
Measure one set-up of the botpython3 pii.py all
Measure the set-ups that you namepython3 pii.py all-filter one one-filter
Mark a test of an honest request (a fourth item)("my-order", prompt, check, True)

Model output (lesson 5)

TaskCommand or code
Escape an answer for HTMLhtml.escape(text)
List the inline Markdown images of an answer from other hosts (it misses other forms)foreign_images(answer)
Measure the pagepython3 unsafe.py
Sign an answer record with a secret keysign(record): hmac.new(KEY, json.dumps(record, sort_keys=True).encode(), hashlib.sha256).hexdigest()
Check a record before useverified(record, signature)
Count the pairs whose outcome differs when one group word differssum(offered(a) != offered(b) for a, b in pairs)

Database queries (lesson 6)

TaskCommand or code
Run a query with a value that cannot become SQLdb.execute("SELECT id FROM orders WHERE customer = ? AND item LIKE ?", (user, f"%{word}%"))
Accept a word only if the whole text fitsre.fullmatch(r"[A-Za-z0-9]{1,30}", word)
Measure the weak and the safe querypython3 sql_unsafe.py and python3 sql_safe.py

Tools and approval (lesson 7)

TaskCommand or code
Let the bot only ask for a refundrequest_refund(order_id, amount)
Refuse True as an amounttype(amount) in (int, float)
Measure one tool set-uppython3 agent.py approval

Cost and abuse (lesson 8)

TaskCommand or code
Read the tokens and the time of one requestchat(messages, full=True)
Limit requests for each userRateLimit(5, 60).allow(user, now=t)
Stop waiting for a slow answerfuture.result(timeout=120)
Measure the four limitspython3 cost.py
Refuse a wrong key, type, size or number of parts before the modeladmit(key, [("text/plain", data)])

Guardrails (lesson 9)

TaskCommand or code
Refuse a known attack phraseinput_blocked(text)
Refuse an answer with no shop topicon_topic(answer)
Measure the guardrails and the false refusalspython3 guardrail.py

Secure design patterns (lesson 10)

TaskCommand or code
Put outside text in a labelled blockwrap(text)
Parse a JSON answerjson.loads(raw)
Find the words of 4 letters or moreset(re.findall(r"[a-z]{4,}", text.lower()))
Compare the weak and the pattern apppython3 compare.py

Test suite (lesson 11)

TaskCommand or code
Run the suite on a module and judge itpython3 suite.py vulnerable 0.2
Judge a saved results file with no model callspython3 suite.py --from results-patterns.json 0.3
Run a command only when the gate passespython3 suite.py --from results-patterns.json 0.5 && echo "gate passed"
Read the exit code of the last commandecho "exit code $?"

Jailbreak cases (lesson 12)

TaskCommand or code
Judge a reply for the planted code, spaced, backwards or in halvesleaked(text)
Replay the replies written for the lab (not recorded from a model), with no modelpython3 jailbreak.py replay rule-in-prompt
Run the cases on your local modelpython3 jailbreak.py live rule-in-prompt
Read the saved replies of a runjson.load(open('replies-jb-rule-in-prompt-live.json'))
Write a text backwardstext[::-1]

Hardened bot and report (lesson 13)

TaskCommand or code
Run the suite on the hardened botpython3 suite.py safe_bot 0.2
Write the before-and-after reportpython3 report.py results-vulnerable.json results-safe_bot.json

Close the last gap (lesson 14)

TaskCommand or code
Refuse an answer that repeats a code of the userechoed(prompt, answer)
Run the suite with that checkpython3 suite.py safe_bot2 0.2
Compare the before and after filespython3 report.py results-vulnerable.json results-safe_bot2.json

Review a chatbot (lesson 15)

TaskCommand or code
Show what the app sends to the modelpython3 scenario_tests.py audit
Run fixed model answers through the apppython3 scenario_tests.py recorded
Run the real model, 5 seeds for each testpython3 scenario_tests.py live 5

Safety habits