Implementation Guide

    Redacting Sensitive Data in Tool Results Before an Agent Sends Them Back to the LLM

    To redact sensitive data in agent tool results, intercept each result after the tool executes and before it is appended to the model context. Apply layered detection (pattern rules, schema or field-level rules, and ML or NLP for free text), choose masking, tokenization or blocking per data class, and record every decision. Prompt filtering and output filtering do not cover this return path.

    Failure modes to test before production

    • False negatives: measure missed sensitive values separately for JSON, SQL rows, documents and free text.
    • Broken tool calls: replay multi-step tasks with redacted or tokenized values and confirm downstream calls still succeed.
    • Multi-turn re-exposure: check that a value removed in one turn does not return through refetching, summaries, RAG entries or cached state.
    • Aggregation: assess whether combining individually acceptable results produces a context the user is not authorized to see.
    • Bypass paths: confirm no tool, protocol or fallback route reaches the model without passing the control.
    • Adversarial output: send crafted tool results and confirm the control neither fails open nor executes embedded instructions.

    Where tool results enter the LLM context

    NIST describes an LLM agent as a system that iteratively prompts a model, processes its outputs (for example to select and call a function with specified inputs), and provides the results back to the model as part of its next prompt. In NIST's agent guidance, tool return values are appended to the agent's context at the observation stage and become input to the next action, which is either another LLM call or another tool call. MCP is among the tool protocols in scope.

    This makes the observation stage a distinct control point. A raw query result containing names, account numbers or credentials is not a user prompt, so prompt filters never see it. It is not model output either, so output filters see it only after the provider has already received it. Unredacted content can also propagate into later tool calls, not only to the model provider.

    The practical rule is that any redaction control must act between tool return and context assembly, and you need to confirm that no path reaches the LLM or a memory store without passing through it. Agents that fetch tools and resources repeatedly, or write summaries and retrieved entries into long-term memory, create extra paths worth enumerating before you choose an interception point.

    Choosing an interception point

    There are three common places to implement the control. They differ mainly in how reliably every result is forced through the same policy.

    • In-application code: A redaction call wrapped around each tool function. Fast to start and easy to tailor per tool. Coverage depends on every developer and every new tool remembering the call, and policy logic is duplicated across services.
    • Framework middleware: A hook in the agent framework that processes every tool result before context assembly. Gives one place for rules within that framework. Coverage stops at the framework boundary, so other agents, runtimes or direct SDK calls are not covered.
    • Enforcement in the execution path: A policy layer between tools and the model that sees results regardless of which agent or framework produced the call. Policy is enforced at runtime and managed centrally. It adds a network or deployment component to operate and must be sized for latency.

    Detection methods and the payloads they suit

    No single method covers every payload, so layer them. Pattern matching works well for well-formed identifiers such as card numbers or national ID formats. NIST notes that regex is traditionally paired with keywords and validators to improve precision, that results depend on proximity to those keywords, that false positives and noise are a risk, and that processing time scales linearly with data volume. Patterns miss values that do not follow the expected format, and a miss is a leak, so measure false negatives on your own data rather than assuming coverage.

    Schema or field-level rules suit structured payloads where you know the data model. NIST's data classification work uses schemas as rule syntax that triggers on data elements, content and metadata, and labels types such as patient ID, name, address and birthdate. For a JSON or SQL result, a field-level rule that treats a named column as sensitive is more reliable than guessing from its value. The limit is that you must know which fields are identifying, and NIST notes that this takes domain-specific knowledge.

    ML and NLP detection covers free text and unfamiliar patterns. NIST describes ML-based categorization as handling structured and unstructured data and adapting to new patterns without explicit reprogramming. It also notes that statistical tools generally perform less accurately than purpose-built ones and need labeled training data. Semi-structured payloads are the hard case: a structured record often carries a free-text notes field. Apply field-aware rules to the structure, then run text-level detection on string fields that can hold prose.

    Treat tool output as untrusted. A malicious or compromised tool controls the return channel and can return unexpected or adversarial content, so detection logic should not assume well-behaved input.

    Masking, tokenization or blocking

    Masking replaces a value with a fixed placeholder or a partial value. It is simple and irreversible within the agent loop, and it preserves the shape of the data, so a record still parses. The cost is lost information: if the agent needs to pass that value to a later tool, a placeholder will not work.

    Tokenization, or reversible pseudonymization, replaces a value with a consistent token and keeps the mapping outside the model context. The agent can reason about the same entity across turns, and a trusted component can restore the real value when a downstream tool call needs it. This adds a token vault to secure and a re-identification path to control. Whether a token or masked value still works in a given downstream call is not something to assume, so test it per tool.

    Blocking withholds the whole result and returns an error or a neutral message. It is the safest choice for credentials and for results where partial redaction cannot be trusted, but it ends the task step. A workable policy assigns an action per data class: block secrets, tokenize identifiers the agent must carry forward, and mask values that are only incidental.

    Keep the terminology honest. NIST notes that suppression alone does not give formal privacy guarantees and that masking tools may not provide full de-identification, and that information left in the data can allow re-identification. Redaction reduces exposure on this path. It is not anonymization.

    Evidence to retain, and where Trussed fits

    A regulated enterprise should be able to show not just that a policy existed but that it was applied to each result. A reasonable minimum per tool result is the policy version applied, the detection method that fired, the action taken (allow, redact or block), the data classes involved, and a timestamp. Store the decision record rather than the raw sensitive value, and set retention and residency deliberately. Check the specific evidence your auditors expect against your own regulatory obligations, since requirements differ by framework.

    Trussed is one implementation of this pattern. It enforces policy in the execution path across models, agents, tools and MCP servers, so tool results are allowed, redacted or blocked at runtime, not reviewed after the fact. Enforcement produces audit-ready evidence mapped to HIPAA, NAIC, NYDFS, EU AI Act and NIST, and it can be self-managed in the customer cloud with configurable retention and data residency. Its scope is individual interactions and tool calls. The same architectural questions above apply to any option you evaluate: whether every result passes the control, how false negatives are measured, and what record each decision leaves.

    The tool-result return path

    Tool executes

    A database, API, file system or MCP server returns raw data.

    Inspect and decide

    Policy is applied to the result: allow, redact or block.

    Context append

    Only the approved form of the result enters the model context.

    Evidence recorded

    The decision, method and policy version are retained.

    See enforcement on the tool-result path

    Walk through how tool results are allowed, redacted or blocked in the execution path, and what evidence each decision produces.

    See How Evidence Is Produced