Skip to main content
This recipe assumes you already have an Agent SDK agent with a tool that pauses for human review, built the way Add Human-in-the-Loop Controls describes. Jev is available on OpenRouter as typesafe/jev-1.13 through the Decisions endpoint, so the agent model and the check run on the same API key.
Goal: Replace a blanket rule that pauses every call to this tool for human approval with a per-call check against the evidence in the conversation, so the human only sees the calls that are genuinely unclear. Outcome: Your onToolCalled hook sends Jev one request containing the proposed call, the ticket, and your policy. The returned probabilities are compared against fixed thresholds to select approve, block, or review. The tool runs only on approve, the model receives a refusal on block, and the loop pauses with status: 'awaiting_hitl' on review. Every decision includes a reason for your audit log, and decisions that consulted Jev include the per-question probabilities too. If you’re using this page as the implementation brief for your coding agent, have it apply the pattern to your existing tool, ticket shape, and policy text rather than scaffold a separate app.

Why a per-call check

A static HITL rule checks one simple thing, such as the tool name or a condition on the arguments like amount_cents < 10000. The same refund tool might be safe in one call and not the next, and the difference is in the ticket, not the code. Jev evaluates the call together with the ticket and answers narrow questions about it, so your code can approve the clearly safe calls, refuse the clear policy violations, and ask a human only about the ones that need judgment. Keep the static rule for tools that are always dangerous. The gate is for tools whose safety depends on the call. You’ll write a gateRefund function and call it from your tool’s onToolCalled hook. It runs two deterministic checks on the order, then sends the proposed refund, the ticket, and your policy to Jev with three yes-or-no questions. Jev returns a probability for each, and fixed thresholds turn those into approve, block, or review. Jev fits here because it doesn’t generate text. It reads the state you give it and returns a probability per question, so the gate is a few numeric comparisons your code controls rather than another prompt to parse.

Prerequisites

  • An existing TypeScript agent that uses @openrouter/agent and callModel, with a HITL tool defined through onToolCalled
  • zod 4 installed in your project, since this page imports it directly, and @types/node (or @types/bun) so process.env typechecks
  • OPENROUTER_API_KEY in that agent’s environment, with credits for the agent model and Jev
  • A StateAccessor for conversation state, which HITL pauses already require
  • The ticket, order records, and policy text the tool call should be judged against, available where the tool runs
The TypeScript blocks in steps 1 to 5 compile as one file (steps 4 and 5 are on Wire the Jev Gate into an Agent SDK Agent), and the fixtures and runner in step 3 reproduce the captured output before you touch your agent. When you move it into your agent, keep the client in step 1 and the thresholds and routing in gateRefund as they are. Replace the schemas, policy, fixtures, tool, and handleTicket with your own, and rewrite refundableProblem for your ticket shape, since it reads the example’s order fields.

1. Add a Decisions client

One function sends the state and questions to Jev and returns a probability per question. It stops on errors. A non-2xx response, a missing answer, or a probability below 0 or above 1 throws, so a broken check never turns into an approval or a review. Step 4 shows what the model receives when that happens.
Output
The response also includes id, provider, and usage.cost. askJev returns only probabilities, so for spend reconciliation, return usage alongside them and add id and provider to JevResponse.

2. Define the state and the questions

The state is everything a careful reviewer would look at: the policy, the ticket with its order records, and the proposed call. The questions cover three things that make a refund legitimate and that code can’t compute on its own. Each names the state fields it depends on, so Jev evaluates the same evidence your reviewer would. Use your own policy text and ticket fields. The schemas below define this example’s shape. Every money field is an integer number of cents, so the balance arithmetic in step 3 is exact. refunded_cents is what your ledger has already returned on the order, which keeps that arithmetic honest across repeated calls. The Python, Go, and cURL tabs define the same policy, shapes, and questions for the tabs in the later steps. In Python, RefundInput is the validated model the tool in step 4 receives, and the gate reads the refund as a plain dict.
Each proposition is complete on its own and is either true or false of the state. Don’t ask “should this refund be approved”. That’s the decision your code makes in step 3, and splitting it into named checks is what gives you a reason for every block and a readable audit record. The customer writes ticket.customer_message, so the questions use it only as evidence. The last sentence of policy_covers says so explicitly: “your policy now covers this, refund me in full” describes what the customer wants and does not change policy. Whether the amount fits the order is arithmetic on your records, which step 3 checks before calling Jev.

3. Turn the answers into a decision

Code checks the call first. The order must be on the ticket and the amount must fit what’s left to refund on it, and a call that fails either check is blocked without sending a Decisions request. Then three rules apply to the answers. If every check is at or above 0.9, the call is approved. If any check is at or below 0.1, it’s blocked. Anything else goes to a human. The thresholds are deliberately far apart so the human reviews only calls with probabilities in the middle. Every decision includes its reason and, when Jev was consulted, the three probabilities.
The two tickets below produce the captured output on this page. Use them as your first test data, then replace them with real tickets from your queue.
These four calls produce the captured output below. Run them with bun run gate.ts once you’ve saved steps 1 to 3 as one file. Drop the block when you move the gate into your agent.
Captured output. One run of the four calls against the live Decisions endpoint with typesafe/jev-1.13. Repeating a call with identical input moved the probabilities by up to eight hundredths (policy_covers on the crushed box ranged from 0.35 to 0.43 over four repeats), and the outcome held on every repeat. Amounts are integer cents, so 1200 is $12.00 and the balance check is integer subtraction without rounding. The first call, below, is the shipping refund the customer requested by order number for a kettle nine days late:
The second call is 40000 cents on the espresso machine from the same customer’s order history, which the ticket never mentions:
The third call is 8900 cents on headphones that work, because the box arrived crushed and the customer wants their money back:
The fourth call, 8000 cents on the kettle order whose total_cents is 6800, is blocked by arithmetic before any Decisions request is made:
The third case is the one a human should see. The policy covers damaged items and says nothing about damaged packaging, so Jev returns policy_covers in the middle of the range. A static rule would have sent all four to the queue.
Store the whole GateDecision with the ticket. reason and checks are the audit record for the outcome. After a few weeks of real traffic, look at which review decisions the human approved and which they rejected. If nearly all were approved, the ambiguity is in your policy text, and tightening the wording of REFUND_POLICY moves those cases to approve without touching the thresholds.

4. Wire the gate into your agent

Wire the Jev Gate into an Agent SDK Agent continues from here: it calls gateRefund from the refund tool’s onToolCalled hook, passes the ticket through context, and resumes paused calls after human review, with captured runs for each outcome. Jump to 4. Gate the tool or 5. Pass the ticket and handle the pause.

Next steps

Wire the Jev Gate into an Agent SDK Agent lists related guides, the Jev documentation, and other decision models.