Securing LLM Applications and Agents
Written with AI. Verified by people. How we make books
This book is open to contributions, so anyone can check it or correct it. Its repository · Report an error · How to fix one
How an assistant with tools and data is turned against its users, and which defences measurably stop it: prompt injection, exfiltration, tool misuse and third-party code, on a property office's assistant.
Who it's for An engineer who has shipped, or is about to ship, an assistant or agent that reads text other people wrote and acts on it, and who writes Python.
Read on Kindle or any e-reader
The EPUB also works in Apple Books, Kobo and most e-readers.
Send to Kindle
Sends this edition's EPUB in English.
-
Approve our address
Amazon delivers to a Kindle only from addresses you've approved, once for each service that emails it. Add ours to your Approved Personal Document E-mail List.
books@rollingedition.comOpen Amazon's Personal Document Settings. On Amazon, it's under Manage Your Content and Devices, Preferences, Personal Document Settings.
Sent. It'll appear in your Kindle library in a few minutes.
If it doesn't arrive, it's almost always the approval in step 1: check that books@rollingedition.com is on your Approved Personal Document E-mail List.
Each new edition of your saved books will go to your Kindle too.
That isn't a Kindle address. It ends in @kindle.com: find it in Amazon's Personal Document Settings.
You've sent {count} books today, the most we send for one account in a day. Try again tomorrow.
We've sent as many books as we can today. Try again tomorrow.
The book didn't send. Try again in a few minutes.
This edition is too large to email.
Send to Kindle comes with Labs.
That isn't a Kindle address. It ends in @kindle.com: find it in Amazon's Personal Document Settings.
The book didn't send. Try again in a few minutes.
Or download the EPUB and drop it on Amazon's Send to Kindle page.
Kindle is a trademark of Amazon.com, Inc.
Labs
Work through this book in the Lab
Run every example exactly as the book did, change the code to see what happens, and try your own ideas on the book's code and data. Nothing to install, and your work is saved. Opens in about 30 seconds.
Run every example exactly as the book did, change the code to see what happens, and try your own ideas on the book's code and data. Nothing to install, and your work is saved. Labs runs it for you: 40 hours a month.
Checking your hours…
Checking for your Lab…
On your own machine, free: download the book's notebooks, with its code and data, from GitHub.
Contents
12 chapters, about 3 hours 43 minutes to read or 4 hours 27 minutes to listen to, with 60 examples.
-
1Attack an assistant, then measure a fix
By the end of this chapter you can attack an assistant with a fixed set of cases, score each run in code, and say with an interval how much one defence changed the result and what it cost in the tasks the assistant still finished.
-
2Draw the assistant's threat model
By the end of this chapter you can draw an assistant's threat model: its assets, the places where text from outside enters it, the flows that carry that text to the model and its tools, and the channels an attacker would use to get data out.
-
3Build an attack set you can score in code
By the end of this chapter you can build a fixed set of attacks on an assistant.
-
4Measure what prompt-level defences buy
By the end of this chapter you can measure what a prompt-level defence does to an assistant's attack success and to its utility, count the tokens it adds to each prompt, and read a held-out result without tuning on it.
-
5Detect injected text, and count the false alarms
By the end of this chapter you can run a classifier on the office's texts, pick its threshold on dev only, count its false alarms on held-out and on two public corpora, and say which of those counts holds for which kind of text.
-
6Enforce least privilege outside the model
By the end of this chapter you can restrict an assistant's tools and arguments in code, so that an injected instruction has less to reach, and you can measure each restriction on a fixed set of attack cases and say what it cost the assistant's ordinary tasks.
-
7Close the exfiltration channels
By the end of this chapter you can keep an assistant's outbound calls and its reply from carrying a tenant's record to a host the office does not run.
-
8Keep untrusted data out of the plan
By the end of this chapter you can build three designs that keep untrusted text away from the model that holds the tools, run each one on the case's attack set and benign tasks, and say what each buys and what each costs, with an interval on both.
-
9Keep secrets and other people's records out of the context
By the end of this chapter you can keep an assistant's secrets out of its prompt, filter the documents it searches by the role of the person it works for, and check its replies for a deposit figure stated without a citation to an office document.
-
10Check what third parties hand the assistant
By the end of this chapter you can check the parts of an assistant that other people wrote: the tools a server offers, the files of the model the assistant loads, and the packages its code installs.
-
11Red-team the assistant with an adaptive attacker
By the end of this chapter you can run an attacker that revises its injection after each try, against a defence, under a fixed budget.
-
12Keep the defences from regressing
By the end of this chapter you can run the defended assistant's attack set as a gate that fails a change which weakens a defence.