Skip to content
mentoor

Topic 2 of 5 · 5 min read

Data minimization in prompts and context

Goal: Change a prompt builder or tool output so it passes only an allowlist of fields to the model.

Send the model only the fields the task needs, chosen by an allowlist in code, so a field you forgot about can never leak by default. Redaction cleans text after the fact. Minimization avoids putting the data there in the first place.

Check yourself · 1 of 3

Why is an allowlist of fields safer than a denylist for building a prompt?

Choose one answer

Why it matters

The usual leak is not a user typing a secret. It is a developer writing JSON.stringify(user) into a prompt, or a tool that returns a whole database row. The model gets the home address, birth date and internal notes, and your logs, traces and provider records get them too. GDPR Article 5 states the principle: personal data must be adequate, relevant and limited to what is necessary. NIST SP 800-122 gives the same advice: collect and keep the least PII that works.

How to do it

  1. Write down what the task needs. "Draft a delivery-delay email" needs first name, order id, new date. It does not need the street address or phone.
  2. Build prompts from named fields, not whole objects. Use a small function that picks keys. An allowlist fails safe: new columns added to the table stay out until someone chooses them. A denylist fails open.
  3. Apply the same rule to tools. A get_customer tool should return a trimmed view, not the row. Models read everything a tool returns.
  4. Apply it to retrieval. Strip personal fields from chunks before they enter the context, or filter by metadata so one user's documents never appear in another user's query.
  5. Prefer coarse values. Send age_band: "30-39" or city instead of a birth date and street.

Before and after

- const prompt = `Write a delay notice for this customer:\n${JSON.stringify(customer)}`;
+ const view = {
+   firstName: customer.firstName,
+   orderId: order.id,
+   newDate: order.newDeliveryDate,
+ };
+ const prompt = `Write a delay notice using only these fields:\n${JSON.stringify(view)}`;

The first version also sends email, phone, address and any note field the table gains later. The second sends three values, and a reviewer can read the full list in one place.

What goes wrong

  • Allowlist drift. Someone adds address "just for this case" and it stays. Review the list in code review like an API contract.
  • Hidden fields in tool output. The tool was trimmed, but its error message prints the SQL row.
  • Whole-conversation history. Resending all earlier turns carries every earlier secret into each new call. Trim or summarize history.
  • Retrieval across users. A shared index without per-user filters can return someone else's chunk.

Examples

A trimmed tool result

The get_order tool returned the full row, including shipping_address and payment_last4. The fix is a toModelView(order) function returning { id, status, eta }. The full row stays available to the app code, not to the model.

Conversation history trimmed

A chat app resent every earlier message on each call, including a pasted passport number from turn two. The change keeps the last six turns and replaces older ones with a short summary that contains no identifiers.

Key points

  • Allowlist the fields a prompt builder may send; new data stays out until someone adds it deliberately.
  • Tool outputs and retrieved chunks go to the model too, so trim them with the same rule.
  • Coarse values such as an age band or a city often serve the task as well as exact ones.
  • Review the allowlist in code review like a public API, and keep it small.
  • Filter retrieval by user or tenant so one person's data cannot appear in another's context.

Common mistakes

  • Using a denylist of 'sensitive' columns, so every new column is sent by default.
  • Serializing a whole user or order object into the prompt for convenience.
  • Trimming the prompt but leaving the tool output and retrieval chunks untouched.
  • Resending the full chat history forever.

Check yourself · 2 of 3

A get_customer tool returns the whole database row to the model for a task that only needs the first name. What is the best fix?

Choose one answer

Check yourself · 3 of 3

A chat app resends the full history on every call, and a user pasted a passport number early on. Which change helps most?

Choose one answer

Practice items, not counted towards your mastery.

Start here

More for this topic