Skip to main content
This page is the second half of Gate Agent Tool Calls with Jev. Steps 4 and 5 below use the Decisions client, REFUND_POLICY, REFUND_CHECKS, and gateRefund from steps 1 to 3 there. The accordion below repeats that TypeScript, without the step 3 runner, so the blocks on this page compile as one file with it.

4. Gate the tool

Replace the body of your tool’s onToolCalled with the gate. approve runs the real side effect and returns the result. block returns a refusal with the gate’s reason in the note. The note goes to the model, which writes the reply to the customer, so if you don’t want check names like right_order=0.01 in the model’s context, send a fixed message and keep reason in your log. review returns null, which pauses the loop exactly as it did under the static rule. The ticket reaches both hooks through the tool’s contextSchema, which the tools reference documents. A human resolves a paused call by returning a decision, not a payment result. The review surface returns approve or deny. resolveReview in step 5 copies the refund from the pending call in your conversation state into the function_call_output, and onResponseReceived runs the same arithmetic and the same issueRefund as an automatic approval. The SDK passes onResponseReceived only the output it receives and doesn’t expose the original call’s arguments, so resolveReview must be the only code that writes a function_call_output for this tool. A review surface that wrote its own could submit a refund Jev never evaluated, and only the arithmetic check would limit it. The Python tabs in steps 4 and 5 use the Python port of the Agent SDK. Install it with pip install openrouter-agent-sdk and import it as openrouter_agent. Keep every Python import and helper from steps 1 to 3, including os, RefundInput, GateDecision, gate_refund, and refundable_problem, but leave out the step 1 __main__ smoke test and the step 3 loop over cases. The Go tabs call the Responses API directly and run the same hooks in their own loop. If you add the Go code to the first page’s program, replace its main, which only runs the Decisions smoke test, with one that calls handleTicket and then resolveReview in the same process, so resolveReview finds the pending call in the in-memory store.
Both hooks start by parsing the ticket out of the context. When that parse throws, or askJev throws because Decisions is down or returned something the schema rejects, the SDK records the error as that call’s output in the form { error: ... }. The model receives the error instead of a refund result, nothing is issued, and the model may call refund again, which runs the gate again. If you’d rather a human reviewed the calls Jev couldn’t evaluate, catch the error from gateRefund alone and return null. Don’t do the same for the ticket parse, since onResponseReceived parses the same context and a reviewer’s approval would fail on it too. The gate never returns approve without the required evidence.
issueRefund is a placeholder. Wire it to your payments provider only after the step 3 runner and the step 5 ticket runs reproduce the captured outcomes with the placeholder in place. Your ledger, not this file, is the record of what’s been refunded. Read refunded_cents from it when you build the ticket. Have the provider reject any amount above the remaining balance at execution time, so concurrent turns can’t overdraw it, and give the provider an idempotency key so a retried turn can’t pay the same refund twice.

5. Pass the ticket and handle the pause

Pass the ticket to the tool through context, keyed by tool name, alongside the tools and state you already pass to callModel. After the run, check the state. awaiting_hitl means the gate returned review, and the pending call goes to your review surface as before. The other status values are listed in the Tool Approval & State reference. Resuming follows the HITL recipe, step 4 with two differences. The function_call_output includes a ReviewDecision whose refund is copied from the pending call in the saved state, so the reviewer only provides a decision and a note. The resumed call passes the same context, because onResponseReceived reads the ticket from it.
Captured output. The kettle ticket, run through handleTicket with the placeholder issueRefund. The model proposed 1200 cents, the gate approved it, and the model told the customer:
The crushed-box ticket through the same function. The model proposed the full 8900 cents with its own reason text, which scored policy_covers lower than the step 3 fixture did (0.26 against 0.41) and still landed in the review band. The gate returned review and the run paused:
Call resolveReview with the pending call’s ID and 'approve'. onResponseReceived issues the refund and the model closes the ticket:
The probabilities differ from step 3 because refund.reason is part of the state and the model wrote its own. The agent model didn’t propose the espresso refund in any of our runs, so we exercise the block path by calling gateRefund directly, as in step 3. The gate guarantees the outcome. The model’s judgment doesn’t.
Each handleTicket call spends credits on the agent model and on one Decisions request per proposed refund. In our runs each Decisions request cost under $0.0001 (usage.cost between 0.000030 and 0.000036) and returned in under 600 ms.

Next steps