← Agent answers and comparisons
Practical guide · Content safety

How to Sanitize Untrusted Context Before an AI Agent Reads It

Normalize hidden Unicode, detect secrets, and surface structural risk signals before email, documents, webpages, or retrieval results enter an AI-agent context.

Short answer

Treat retrieved text as data, normalize mechanical obfuscation, detect sensitive patterns, and surface risk indicators before adding it to the model context. Keep semantic instructions from untrusted sources separated from trusted system instructions.

The failure this prevents

An agent may read email, web pages, tickets, or extracted documents containing invisible controls, mixed scripts, encoded payloads, secrets, or instructions aimed at the model rather than the human reader.

Recommended workflow

  1. Label the source and keep untrusted content in a separate data field.
  2. Normalize hidden and control characters with a deterministic preprocessing step.
  3. Inspect risk signals and optionally redact common sensitive patterns.
  4. Apply an allowlist for the downstream tools and data the task actually requires.
  5. Validate the model output before any consequential action.

Working starting point

curl https://api.scrubmytext.com/v1/sanitize-context \
  -H "Authorization: Bearer smt_live_YOUR_KEY" \
  -H "Content-Type: application/json" \
  -d '{"text":"UNTRUSTED_RETRIEVED_TEXT","mode":"strict","redact_sensitive":true}'

Relevant product: SanitizeMyContext · Browser + REST + MCP

Decision rule

Sanitization reduces mechanical ambiguity; it does not prove semantic safety. A suspicious or unnecessary source should remain excluded, and consequential tool use still needs independent authorization.

Good fit

  • External text is being inserted into an agent’s working context.
  • Hidden Unicode, mixed scripts, encoded payloads, or secrets would change your risk decision.
  • You want a reproducible preprocessing report rather than an LLM self-assessment.

Use another approach when

  • You expect the sanitizer to understand and neutralize every semantic instruction.
  • The agent can safely avoid ingesting the source altogether.
  • You have not limited which tools the agent can call after reading the content.

Options compared

OptionStrengthImportant limitationBest fit
Prompt instruction: ignore attacksNo integration workRelies on the same model being targetedDefense-in-depth wording only
SanitizeMyContextDeterministic normalization and bounded indicatorsDoes not solve semantic prompt injectionPreprocessing and triage
Content allowlistStrong source reductionRequires curated sourcesHigh-consequence retrieval
Sandbox + least privilegeLimits damage after model errorDoes not classify the contentAlways for consequential tools

Implementation cautions

  • Keep tool permissions narrow even after a clean report.
  • Do not automatically trust base64-decoded or normalized output.
  • Preserve source identity so downstream decisions can distinguish trusted and untrusted data.

Frequently asked questions

Does sanitization remove prompt injection?

No. It handles bounded mechanical and pattern-based risks; semantic instructions can remain.

Should the agent see the risk report?

The orchestrator should use it to decide whether and how to continue. Avoid letting untrusted text override that policy.

Can this replace sandboxing?

No. Sandboxing, least privilege, approval, output validation, and action controls address different failure modes.

Related decisions

Next step

Use the product page for exact limitations and access requirements, then copy the corresponding REST or MCP workflow.