RiskboundDocs

Examples

OpenAI Agents SDK example

A support agent built with the OpenAI Agents SDK (openai-agents 0.26.2). Its one tool, refund_order, calls guard() from the Python SDK before it touches the fake payments API (payments.py). A blocked refund comes back to the model as the tool's answer, so the agent can tell the customer.

Run it#

Start the local stack and get an agent key first: see ../README.md. Then:

bash
cd examples/openai-agents
uv venv .venv
uv pip install -p .venv -r requirements.txt

Scripted mode, no model key#

The SDK's own agents.testing.ScriptedModel stands in for the model. It asks for the $40 and $900 refunds, then summarises the tool results. The Runner, the tool and the Riskbound checks are all real.

bash
RISKBOUND_API_KEY=$RISKBOUND_AGENT_KEY .venv/bin/python agent.py --scripted

You see (the two tool calls run at the same time, so their lines can come in either order):

Customer: Please refund ord_1001 ($40) and ord_1002 ($900).
  [payments] refunded 40.00 USD on ord_1001
  [riskbound] 40.00 USD on ord_1001: allow
  [riskbound] 900.00 USD on ord_1002: blocked (refund-hard-cap)
Agent: Refunded 40.00 USD on ord_1001, refund id re_0001. Riskbound blocked this refund (refund-hard-cap). Nothing was refunded.
Refunds issued: 1

Add ASK_FOR_APPROVAL=1 to also send the $480 refund, then approve it as ../README.md shows.

With a real model#

This calls OpenAI and needs your own key. It was not run for this example; only scripted mode was tested.

bash
OPENAI_API_KEY=sk-... RISKBOUND_API_KEY=$RISKBOUND_AGENT_KEY .venv/bin/python agent.py "Please refund ord_1003, it was 480 dollars"

The model is the SDK's default. Set OPENAI_DEFAULT_MODEL to change it.

What to copy into your own agent#

python
rb = AsyncRiskbound(api_key=api_key, base_url=base_url)
guarded_refund = rb.guard("refund", agent="support-bot")

@function_tool
async def refund_order(order_id: str, amount_minor: int) -> str:
    try:
        result = await guarded_refund(
            {"order_id": order_id, "amount_minor": amount_minor, "currency": "USD"},
            lambda: payments.refund(order_id, amount_minor, "USD"),
        )
    except RiskboundBlockedError as err:
        return f"Riskbound blocked this refund ({', '.join(err.reasons)})."
    return f"Refunded, refund id {result.value.id}."

Let RiskboundUnavailableError raise. The SDK turns a tool error into a message for the model, and the refund does not run.