RiskboundDocs

Examples

LangGraph example

A support agent built as a LangGraph graph (langgraph 1.2.25): a model node, a ToolNode, and tools_condition between them. 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.

Run it#

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

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

Scripted mode, no model key#

FakeMessagesListChatModel from langchain-core stands in for the model. It asks for the $40 and $900 refunds, then replies. The graph, the ToolNode and the Riskbound checks are all real.

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

You see:

Customer: Please refund ord_1001 ($40) and ord_1002 ($900).
  [payments] refunded 40.00 USD on ord_1001
  [tool] Refunded 40.00 USD on ord_1001 (allow), refund id re_0001.
  [tool] Riskbound blocked this refund (refund-hard-cap). Nothing was refunded.
Agent: I have tried each refund. The results are above.
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 through langchain-openai 1.8.5 and needs your own key. It was not run for this example; only scripted mode was tested.

bash
uv pip install -p .venv -r requirements-model.txt
OPENAI_API_KEY=sk-... RISKBOUND_API_KEY=$RISKBOUND_AGENT_KEY .venv/bin/python agent.py "Please refund ord_1003, it was 480 dollars"

Set OPENAI_MODEL to pick the model.

What to copy into your own agent#

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

@tool
def refund_order(order_id: str, amount_minor: int) -> str:
    """Refund a customer's order. amount_minor is in cents."""
    try:
        result = 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}."