# Scanning (/docs/typescript/sdk/scanning)



Every scan method hits the same underlying detection pipeline with different
**provenance** — a plain prompt, a retrieved document chunk, a tool call, and
so on — and they all return the same `ScanResult` shape (RAG-family methods
add a couple of extra fields; `scanRetrieval` returns a list plus an
aggregate verdict instead).

## `analyze` [#analyze]

The general-purpose entry point. Scans a plain prompt before it reaches your
LLM.

```ts
const result = await tawa.analyze("Ignore all previous instructions…");
```

### Result [#result]

<TypeTable
  id="type-table-sample-types.ts-ScanResult"
  type="{
  &#x22;id&#x22;: &#x22;sample-types.ts-ScanResult&#x22;,
  &#x22;name&#x22;: &#x22;ScanResult&#x22;,
  &#x22;description&#x22;: &#x22;The result every scan method returns.&#x22;,
  &#x22;entries&#x22;: [
    {
      &#x22;name&#x22;: &#x22;action&#x22;,
      &#x22;description&#x22;: &#x22;The action to enforce: `allow` or `block`. The gateway collapses the\nengine's `review` using your app's per-content-type setting. Against a\ngateway that predates `engineAction`, this is the engine's raw label.&#x22;,
      &#x22;tags&#x22;: [],
      &#x22;type&#x22;: &#x22;ScanAction&#x22;,
      &#x22;simplifiedType&#x22;: &#x22;ScanAction&#x22;,
      &#x22;required&#x22;: true,
      &#x22;deprecated&#x22;: false
    },
    {
      &#x22;name&#x22;: &#x22;engineAction&#x22;,
      &#x22;description&#x22;: &#x22;The engine's own label before collapsing, for example `review` or\n`hard_block`. Falls back to `action` when the gateway does not send it.&#x22;,
      &#x22;tags&#x22;: [],
      &#x22;type&#x22;: &#x22;ScanAction&#x22;,
      &#x22;simplifiedType&#x22;: &#x22;ScanAction&#x22;,
      &#x22;required&#x22;: true,
      &#x22;deprecated&#x22;: false
    },
    {
      &#x22;name&#x22;: &#x22;needsReview&#x22;,
      &#x22;description&#x22;: &#x22;`true` when the engine was uncertain, meaning its action was `review`,\nwhether `action` was collapsed to `allow` or `block`. `ood` does not set\nit. Derived from `action === \&#x22;review\&#x22;` when the gateway does not send it.&#x22;,
      &#x22;tags&#x22;: [],
      &#x22;type&#x22;: &#x22;boolean&#x22;,
      &#x22;simplifiedType&#x22;: &#x22;union&#x22;,
      &#x22;required&#x22;: true,
      &#x22;deprecated&#x22;: false
    },
    {
      &#x22;name&#x22;: &#x22;risk&#x22;,
      &#x22;description&#x22;: &#x22;How likely the input is malicious, from 0 to 1, regardless of\n`action`. Higher means more likely malicious. `null` for responses\nfrom an older service version, or when the input is out of\ndistribution.&#x22;,
      &#x22;tags&#x22;: [],
      &#x22;type&#x22;: &#x22;number | null&#x22;,
      &#x22;simplifiedType&#x22;: &#x22;union&#x22;,
      &#x22;required&#x22;: true,
      &#x22;deprecated&#x22;: false
    },
    {
      &#x22;name&#x22;: &#x22;confidence&#x22;,
      &#x22;description&#x22;: &#x22;Confidence in `action`, from 0 to 1. Equals `risk` for any flagging\naction, and `1 - risk` for `allow`.&#x22;,
      &#x22;tags&#x22;: [],
      &#x22;type&#x22;: &#x22;number&#x22;,
      &#x22;simplifiedType&#x22;: &#x22;number&#x22;,
      &#x22;required&#x22;: true,
      &#x22;deprecated&#x22;: false
    },
    {
      &#x22;name&#x22;: &#x22;suggestion&#x22;,
      &#x22;description&#x22;: &#x22;Coarse allow/block hint, set for the undecided actions.&#x22;,
      &#x22;tags&#x22;: [],
      &#x22;type&#x22;: &#x22;\&#x22;allow\&#x22; | \&#x22;block\&#x22; | null&#x22;,
      &#x22;simplifiedType&#x22;: &#x22;union&#x22;,
      &#x22;required&#x22;: true,
      &#x22;deprecated&#x22;: false
    },
    {
      &#x22;name&#x22;: &#x22;rationale&#x22;,
      &#x22;description&#x22;: &#x22;Human-readable explanation of why this decision was reached.&#x22;,
      &#x22;tags&#x22;: [],
      &#x22;type&#x22;: &#x22;string&#x22;,
      &#x22;simplifiedType&#x22;: &#x22;string&#x22;,
      &#x22;required&#x22;: true,
      &#x22;deprecated&#x22;: false
    },
    {
      &#x22;name&#x22;: &#x22;policy&#x22;,
      &#x22;description&#x22;: &#x22;Directives from the policy engine, if any matched.&#x22;,
      &#x22;tags&#x22;: [],
      &#x22;type&#x22;: &#x22;PolicyVerdict | null&#x22;,
      &#x22;simplifiedType&#x22;: &#x22;union&#x22;,
      &#x22;required&#x22;: true,
      &#x22;deprecated&#x22;: false
    },
    {
      &#x22;name&#x22;: &#x22;language&#x22;,
      &#x22;description&#x22;: &#x22;Language detection detail, `null` while disabled.&#x22;,
      &#x22;tags&#x22;: [],
      &#x22;type&#x22;: &#x22;LanguageInfo | null&#x22;,
      &#x22;simplifiedType&#x22;: &#x22;union&#x22;,
      &#x22;required&#x22;: true,
      &#x22;deprecated&#x22;: false
    },
    {
      &#x22;name&#x22;: &#x22;scoreBreakdown&#x22;,
      &#x22;description&#x22;: &#x22;Diagnostic detail behind the decision. Shape varies by which decision\npath fired (fusion, regex-only, out-of-distribution, ...) — treat it\nas opaque debug detail, not a stable schema.&#x22;,
      &#x22;tags&#x22;: [],
      &#x22;type&#x22;: &#x22;Record<string, unknown> | null&#x22;,
      &#x22;simplifiedType&#x22;: &#x22;union&#x22;,
      &#x22;required&#x22;: true,
      &#x22;deprecated&#x22;: false
    }
  ]
}"
/>

```ts
if (result.action === "allow") {
  console.log(`Safe (${result.confidence.toFixed(2)} confidence)`);
} else {
  console.log(`Blocked: ${result.rationale}`);
}

if (result.needsReview) {
  // The engine was uncertain; engineAction holds its own label, e.g. "review".
  console.log(`Flagged for review (engine said ${result.engineAction})`);
}
```

`action` is `allow` or `block`: the gateway collapses the engine's `review`
using your app's per-content-type setting before the response is sent.
`engineAction` keeps the engine's own label and `needsReview` marks the
uncertain results. See [The review flag](/docs/typescript/guides/review-flag).
Against a gateway that predates these fields, the SDK derives `needsReview`
from `action === "review"` and `action` carries the engine's raw label, so
keep handling `review`, `soft_block`, `sandbox`, and `ambiguous` if you
support such a gateway.

### Options [#options]

Every scan method accepts the same options object:

<TypeTable
  id="type-table-sample-types.ts-ScanOptions"
  type="{
  &#x22;id&#x22;: &#x22;sample-types.ts-ScanOptions&#x22;,
  &#x22;name&#x22;: &#x22;ScanOptions&#x22;,
  &#x22;description&#x22;: &#x22;Options accepted by every scan method.&#x22;,
  &#x22;entries&#x22;: [
    {
      &#x22;name&#x22;: &#x22;cuid&#x22;,
      &#x22;description&#x22;: &#x22;A stable identifier for the user or event this scan belongs to — a\nUUID, or (recommended) a hash of identifying metadata. Never pass raw\nPII.&#x22;,
      &#x22;tags&#x22;: [],
      &#x22;type&#x22;: &#x22;string | undefined&#x22;,
      &#x22;simplifiedType&#x22;: &#x22;string&#x22;,
      &#x22;required&#x22;: false,
      &#x22;deprecated&#x22;: false
    },
    {
      &#x22;name&#x22;: &#x22;tenantId&#x22;,
      &#x22;description&#x22;: &#x22;Your own tenant/customer id, forwarded as-is for multi-tenant setups.&#x22;,
      &#x22;tags&#x22;: [],
      &#x22;type&#x22;: &#x22;string | undefined&#x22;,
      &#x22;simplifiedType&#x22;: &#x22;string&#x22;,
      &#x22;required&#x22;: false,
      &#x22;deprecated&#x22;: false
    },
    {
      &#x22;name&#x22;: &#x22;policyOverrides&#x22;,
      &#x22;description&#x22;: &#x22;Additive policy rules unioned with your app's saved policy.&#x22;,
      &#x22;tags&#x22;: [],
      &#x22;type&#x22;: &#x22;PolicyRuleOverride[] | undefined&#x22;,
      &#x22;simplifiedType&#x22;: &#x22;array&#x22;,
      &#x22;required&#x22;: false,
      &#x22;deprecated&#x22;: false
    },
    {
      &#x22;name&#x22;: &#x22;language&#x22;,
      &#x22;description&#x22;: &#x22;ISO language code hint (trust-but-verify): used for routing when the\nworker's language ID is enabled, but still cross-checked — a mismatch\nsurfaces as `language.hintAgrees === false` on the result.&#x22;,
      &#x22;tags&#x22;: [],
      &#x22;type&#x22;: &#x22;string | undefined&#x22;,
      &#x22;simplifiedType&#x22;: &#x22;string&#x22;,
      &#x22;required&#x22;: false,
      &#x22;deprecated&#x22;: false
    },
    {
      &#x22;name&#x22;: &#x22;sessionId&#x22;,
      &#x22;description&#x22;: &#x22;Opaque id correlating this call with every other scan in the same\nagent turn, for a live event stream subscriber to receive as one\nconnected timeline. See the Live Event Stream guide.&#x22;,
      &#x22;tags&#x22;: [],
      &#x22;type&#x22;: &#x22;string | undefined&#x22;,
      &#x22;simplifiedType&#x22;: &#x22;string&#x22;,
      &#x22;required&#x22;: false,
      &#x22;deprecated&#x22;: false
    }
  ]
}"
/>

## RAG protection [#rag-protection]

Scan retrieved content before it's assembled into a prompt.

```ts
// A single retrieved chunk, before it enters the assembled context.
const chunkResult = await tawa.scanChunk(chunkText, { chunkId: "doc-42#3" });

// The fully assembled prompt (system + retrieved chunks + user turn),
// immediately before the LLM call.
const contextResult = await tawa.scanContext(assembledPrompt);

// An entire retrieved set in one call — a per-chunk verdict plus an
// aggregate.
const { perChunk, aggregate } = await tawa.scanRetrieval(query, [
  { chunkId: "doc-42#1", text: "…" },
  { chunkId: "doc-42#2", text: "…" },
]);

aggregate.flaggedChunkIds; // chunks enforced as block
aggregate.reviewChunkIds;  // chunks the engine flagged, blocked or not
```

See the [RAG protection cookbook](/docs/typescript/cookbook/rag-protection) for a full
walkthrough combining all three.

## Agent protection [#agent-protection]

Inspect a proposed tool call before it executes, and its output before it
re-enters the agent loop.

```ts
const callResult = await tawa.scanToolCall("run_query", { sql: "SELECT …" });
if (callResult.action !== "allow") {
  // withhold the tool call
}

const outputResult = await tawa.scanToolOutput("run_query", queryResult);
```

See the [Agent protection cookbook](/docs/typescript/cookbook/agent-protection) for a
full walkthrough with both gates and policy directive handling. In a Vercel AI
SDK app, the [middleware](/docs/typescript/integrations/vercel-ai-sdk#tool-scanning)
runs both scans for you from 0.3.0.

## Scanning the model's reply [#scanning-the-models-reply]

`scanResponse` scans the model's own reply immediately before it reaches the
end user — the same threat family as a tool's output (untrusted-ish content
that can carry an injection aimed at the *next* turn).

```ts
const replyResult = await tawa.scanResponse(modelReply);
```

<Callout type="info">
  The [Vercel AI SDK integration](/docs/typescript/integrations/vercel-ai-sdk) calls
  `analyze` (or `scanContext`), `scanToolOutput`, `scanToolCall` and, with
  `scanResponses`, `scanResponse` for you automatically, so you rarely need to
  call these directly in a Vercel AI SDK app.
</Callout>
