Stop AI Agents Acting on Bluffs: Building a Deterministic Tool-Decider Gate A developer published a tutorial on building a deterministic "tool-decider gate" that sits between an AI agent and every side-effecting tool, parsing structured tool calls with TypeScript and Zod and evaluating them against explicit policy rules to return an execution plan, a structured repair request, or a hard denial. The gate is designed to catch semantically wrong but structurally valid calls, such as a refund issued for 49 cents instead of $49.00, and to produce an auditable trail with machine-readable reason codes for each decision. Originally published on tamiz.pro https://tamiz.pro/insights/deterministic-tool-decider-gate-ai-agents . An AI agent can produce a perfectly plausible tool call that your runtime happily executes: a SQL statement against the wrong table, a shell command with an unexpected flag, a payment request for the wrong amount. The model was not "lying" in a moral sense; it was optimizing for a plausible next step, and plausibility is not correctness. The fix is to move authorization and policy decisions out of the model's discretion and into a deterministic layer that sits between the agent and every side-effecting tool. This layer is often called a tool-decider gate . It parses structured tool calls, evaluates them against explicit, testable rules, and either returns an execution plan, a correction request, or a hard denial. This tutorial builds that gate for a real agent workflow using TypeScript, Zod, and a small policy engine. The result is not merely a safer agent; it is an auditable one, where every decision has a machine-readable reason code and a replayable evidence trail. Most agent frameworks assume the model emits a tool call and the runtime executes it. That assumption is fine for read-only tools with no sensitive parameters. It breaks down when a tool can mutate state, spend money, delete data, or reach an internal network. Consider a customer-support agent asked to "refund order A-1042 for $49.00." A model might emit: { "tool": "issue refund", "args": { "orderId": "A-1042", "amount": 4900, "currency": "USD", "reason": "customer request" } } That looks correct. Now consider the same agent under a slightly different prompt, where the model confuses cents and dollars: { "tool": "issue refund", "args": { "orderId": "A-1042", "amount": 49, "currency": "USD", "reason": "customer request" } } The second call is structurally valid, semantically wrong, and will happily refund $0.49 while reporting $49.00 to the customer. No schema validator catches this. No prompt makes it reliably safe. The general failure mode is that language models are probabilistic generators of text-shaped actions , while production systems require deterministic acceptance criteria over structured actions . The gap between the two is where bluffs become incidents. A tool-decider gate narrows that gap by enforcing four properties: The gate must be ordinary code. It must never ask a model whether it thinks the call is safe. The gate is a chokepoint. Every tool call, whether generated by a model, a planner, a sub-agent, or a human-facing UI, passes through it. User Request | v Agent / Planner LLM | | proposed tool call structured v Tool-Decider Gate <-- schemas, policy rules, context, allowlists | | | | | | | +-- DENY: hard block, no execution | | +----- REPAIR: return structured correction request | +-------- ALLOW: emit execution plan v Executor / Sandbox | v Tool DB, API, shell, browser, payments | v Audit Log <-- decision, rule IDs, evidence hashes, outcome Two design rules follow immediately. The gate returns decisions, not just booleans. A boolean false tells the agent nothing useful. A structured denial tells it exactly which field failed, what constraint applies, and what it must do next. That distinction is what turns a gate from a wall into a feedback loop. The executor trusts only the gate's output. The executor should never re-derive policy, and the model should never be able to construct an execution plan directly. If the agent can bypass the gate, the gate is decoration. A useful mental model: the LLM is a proposal engine , and the gate is the compiler front end . The model's output is untrusted source code. The gate performs type checking, semantic analysis, and admission control before anything runs. You will need: You do not need an LLM API key to build and test the gate. That is deliberate: policy logic that depends on a network call is not deterministic, and deterministic policy logic should be testable offline. Create the project and install dependencies: mkdir tool-decider-gate && cd tool-decider-gate npm init -y npm install zod npm install -D typescript tsx @types/node vitest npx tsc --init Configure tsconfig.json : { "compilerOptions": { "target": "ES2022", "module": "NodeNext", "moduleResolution": "NodeNext", "strict": true, "noUncheckedIndexedAccess": true,\n "exactOptionalPropertyTypes": true,\n "outDir": "dist", "rootDir": "src", "declaration": true, "sourceMap": true, "esModuleInterop": true, "skipLibCheck": true }, "include": "src/ / .ts", "tests/ / .ts" } Set the package to ESM and add scripts: npm pkg set type=module npm pkg set scripts.build="tsc -p tsconfig.json" npm pkg set scripts.test="vitest run" npm pkg set scripts.dev="tsx src/demo.ts" Create the source layout: mkdir -p src/tools src/policy src/gate src/audit tests Tools are declared once, and that declaration drives three things: validation, the policy gate's type narrowing, and the agent-facing schema. One source of truth prevents the classic failure where the prompt describes a tool differently from the runtime. Create src/tools/registry.ts : js import { z } from "zod"; / Amounts are always integer minor units cents . / export const refundArgsSchema = z.object { orderId: z.string .regex /^ A-Z {1,3}-\d{4,10}$/, "Invalid order ID format" , amount: z.number .int .positive .max 100 000 , currency: z.enum "USD", "EUR", "GBP" , reason: z.enum "customer request", "damaged item", "duplicate charge" , idempotencyKey: z.string .min 8 .max 128 , } ; export type RefundArgs = z.infer