LLM application security

Lessons on assistant logs: what a conversation id, a role field, a max token cap, or a cache flag does not prove.

  • 27 lessons
  • 5 h of reading

An LLM feature mixes instructions and untrusted content in one stream of text, so the model will sometimes follow text it should only have read. These lessons design the application so that when it does, nothing important happens.

Test your own application and its model providers within their terms of use.

  1. 01Prompt injection defense, part 1: map where untrusted text enters the modelDraw the trust boundaries of an LLM feature, mark every place untrusted text reaches the prompt, and list what the model can do with tools, so defenses go where an injection would cause harm.11 min
  2. 02Prompt injection defense, part 2: controls that limit what injected text can doApply least privilege to tools, require confirmation for side effects, enforce limits in code rather than in the prompt, and restrict output rendering so an injected instruction cannot move money or leak data.13 min
  3. 03Prompt injection defense, part 3: regression tests with promptfooTurn the injected test documents from your threat map into a promptfoo suite that checks tool calls and output on every prompt or model change, and read a failing result.11 min
  4. 04Where untrusted text enters an assistantSeparate the user message, a retrieved document, and a tool result. Each channel can steer the model, and each one needs its own control.11 min
  5. 05What a system instruction is forUse a system instruction for role and format. It is not a security boundary, because nothing in the window checks later text against it.11 min
  6. 06Log a suspected injection without the promptWhen a turn looks like an injection, store the channel, the source, a hash, and a short redacted excerpt. Leave the full prompt out if it may hold secrets.12 min
  7. 07Review a tool before an assistant may call itBefore an internal assistant may call a tool, check who the caller is, what the arguments may name, what the tool changes, and what text it sends back.12 min
  8. 08A model reply is not the record of what happenedText the model emitted is a generated sentence. Write the model id, the response id, and the time. Cite the application’s own log for any claim that a tool ran or a record changed.11 min
  9. 09A tool trace has a request and a resultA trace can show that the model asked for a tool and, separately, whether the application ran it. Write both lines. Do not treat the model’s retelling of the tool output as the output.11 min
  10. 10A classifier label is not an incidentA block or allow flag is a classifier’s decision at a version and a score. Write the name, version, and decision. A block is not proof of an attack, and an allow is not permission for a tool.12 min
  11. 11An eval count is one fixture setA passing eval count means those fixtures passed on that model and prompt version. Write the set id and the model id. Do not read the count as “the assistant is safe,” and do not compare runs that used different sets.11 min
  12. 12A citation in the reply is not a retrieved sourceA document title in the model’s reply is text the model produced. The retrieval log’s document id is the source. Write the id from the log, and say when the title in the reply is not there.11 min
  13. 13The finish reason is why the reply stoppedThe finish reason says whether the model ended the reply or the reply hit a length limit. Write it next to the text. A cut-off sentence is not a complete instruction.11 min
  14. 14The model id on the response is what answeredThe configuration names the model you asked for. The response names the model that produced the text. Write the response’s id when they differ, and do not file the reply under the configured name alone.11 min
  15. 15A retrieval score is a rank, not a permissionA score on a retrieved chunk is the ranker’s number. Write the document id and the score as a rank. The score does not mean the person may see the chunk, and it does not mean the chunk is true.12 min
  16. 16Temperature on the request is not a confidence scoreTemperature is a sampling setting on the request, if the log recorded it. It is not a measure of how sure the model was, and a zero does not make the reply a record.11 min
  17. 17Token counts are usage, not qualityPrompt and completion token counts say how much text was billed or metered. A longer completion is not a better answer, and a short one is not incomplete unless the finish reason says so.11 min
  18. 18The schema version labels the tool argumentsA tool trace’s arguments mean what the schema version on that trace says they mean. Write the version. Do not interpret a field with today’s schema if the log names an older one.12 min
  19. 19Empty content is not an approvalAn empty reply is an empty string. It is not a yes. Write the empty content and any refusal or finish field beside it, and do not treat silence as permission for a tool.11 min
  20. 20The request id is not the response idThe request id names the call the application sent. The response id names the reply the provider returned. A retry can reuse one and mint the other. Write both, and do not file two replies as one answer.11 min
  21. 21The user id is who sent the turnThe user id on an application log is the account that submitted the turn. The model id is what produced the text. Write them as different fields, and do not treat the user id as the author of the reply.11 min
  22. 22Latency is the round trip, not a decisionA latency figure is how long that call took to return. It is not a classifier score, a block, or a judgment that the answer is short because it was refused. Write the milliseconds beside the response id.11 min
  23. 23A redaction marker is not the original textA marker such as [redacted] is text your logger wrote in place of something it withheld. It is not the user’s sentence and not the model’s reply. Write the marker as a marker, and do not reconstruct the withheld span.12 min
  24. 24The conversation id is not the request idA conversation id groups turns. A request id is one call. Write both, and do not merge calls that only share the thread.11 min
  25. 25The role field is not the accountRole names the message slot. The account is a different field. Do not treat the word user as who sent it.10 min
  26. 26The max token cap is not the usagemax_tokens is the limit on the request. The completion count is what was metered. Do not copy the cap into the usage field.10 min
  27. 27A cache hit is not a new generationA cache flag means a stored result was served. The time on this call is the serve time, not the first generation.11 min