Any loop that calls tools can use three methods. They work the same in all three languages.
| Method | Use |
|---|---|
| run.check(tool, args, options) | Run the before-guards; returns a verdict |
| run.after(tool, args, result, options) | Run the after-guards; returns what the model should read |
| run.wrap(tool, fn) | Both around a function: returns the result, or the message for the model |
check and after#
const verdict = await run.check("refund_invoice", args, { id: toolCallId, messages });let output: unknown;if (verdict.kind === "proceed" || verdict.kind === "transform") { const result = await refundInvoice(verdict.args); output = (await run.after("refund_invoice", verdict.args, result, { id: toolCallId })).result;} else { output = verdict.message; // for the model if (verdict.say) tellThePerson(verdict.say);}refundInvoice and tellThePerson stand for your own functions.
check options#
Return values#
- check returns a verdict: kind, args (with transforms applied), message for the model when the call does not run, say for the person, decision (the winning guard's decision), results (every guard's answer).
- after returns { result, results } in TypeScript; in Python and Ruby it returns the result itself.
Python verdicts have verdict.runs; Ruby verdicts have verdict.runs?. In TypeScript, compare verdict.kind with proceed and transform.
A call checked and allowed by check has already taken its share of the budgets. Run it, or the count is off by one.
wrap#
const refund = run.wrap("refund_invoice", async (args: { invoice_id: string; amount: number }) => billing.refund(args));const output = await refund({ invoice_id: "INV-1", amount: 90 }); // the result, or the message for the model- TypeScript: the wrapper takes the arguments object and returns a promise. When the call does not run, it returns the verdict's message (or Not allowed.).
- Python: works as a function (run.wrap(tool, fn)) or a decorator. The function is called with the arguments as keywords. messages takes a list or a function that returns one, read on each call, so grounding sees the latest conversation.
- Ruby: takes a block and returns a lambda that takes the arguments hash (symbol keys).
TypeScript and Ruby wrap pass no conversation, so grounding passes those calls (unless requireTranscript is set). Use check with messages when grounding matters.
Parallel calls#
Checks in one run go one at a time, even when your loop fires several at once. Each check sees the budgets earlier checks reserved.
Hide tools the task can never use#
Before each model step, offer only the tools the task could use:
| TypeScript | Python | Ruby |
|---|---|---|
| await run.allowedTools(names) | run.allowed_tools(names) | run.allowed_tools(names) |
It keeps names whose permission is in the task's ceiling, names with no permission, and the human tools. Without names, it uses the tools the run has seen through its adapter or wrap (Python) calls.