# Gate OpenAI agent tools

Add approvals to an OpenAI Agents SDK agent in TypeScript or Python, so each gated tool call is authorized and verified on its exact arguments, with a person's approval when your policy asks, before it runs.

Use `@provenanceauth/openai-agents` (TypeScript) or `provenanceauth-openai-agents` (Python) to add approvals to an OpenAI Agents SDK agent. A gated tool interrupts the run before it executes, and `resolveApprovals` decides each interruption with Provenance on the call's exact arguments, then resumes the run.

**Before you begin:** install the package for your language, and export the sandbox account key.

```bash
export PROVENANCE_API=https://sandbox-api.provenanceauth.com
export PROVENANCE_KEY=prv_test_acct_P9xN2vQ7bT4mW8sYd3cFjLh6gAe1kR5
```

**This package isn't on npm yet. This package isn't on PyPI yet.** Until then, every request in these docs runs over HTTP against the [sandbox](https://provenanceauth.com/docs/get-started/sandbox.md).

With access to the private Provenance repository, which your Provenance contact grants, install from it:

```bash
git clone https://github.com/michaelnbane-jpg/provenance.git provenance
cd provenance && pnpm install && pnpm build
node scripts/check-publishable.mjs --out ../provenance-packs
cd ../your-project
npm install ../provenance-packs/provenanceauth-sdk-1.0.0.tgz ../provenance-packs/provenanceauth-agent-adapters-1.0.0.tgz ../provenance-packs/provenanceauth-openai-agents-1.0.0.tgz
```

```bash
git clone https://github.com/michaelnbane-jpg/provenance.git provenance
pip install ./provenance/python/sdk ./provenance/python/openai-agents-provenance
```

**Availability:** the packages are available to beta customers. They support `@openai/agents` 0.8 to 0.21 and `openai-agents` 0.3 to 0.24.

## 1. Gate a tool

`withAuthorize` returns a copy of a function tool that asks before it runs. Pass the copy to your agent:

**Node**

```ts
import { Agent, run, tool } from "@openai/agents";
import { z } from "zod";
import { Provenance } from "@provenanceauth/sdk";
import { withAuthorize, resolveApprovals } from "@provenanceauth/openai-agents";

const prov = new Provenance({ apiKey: process.env.PROVENANCE_KEY!, baseUrl: process.env.PROVENANCE_API });

const payInvoice = withAuthorize(
  tool({
    name: "pay_invoice",
    description: "Pay a supplier invoice",
    parameters: z.object({ invoice: z.string(), amount: z.number(), payee: z.string() }),
    execute: async (a) => `paid ${a.invoice}`,
  }),
  {
    client: prov,
    deviceId: "dev_00000000000000000000000000000001",
    describe: (a) => `Pay invoice ${a.invoice}: $${a.amount} to ${a.payee}`,
  },
);

const agent = new Agent({ name: "Billing", instructions: "Pay supplier invoices.", tools: [payInvoice] });
```

**Python**

```python
import os
from agents import Agent, Runner, function_tool
from provenanceauth_openai_agents import with_authorize, resolve_approvals
from provenanceauth import Provenance

prov = Provenance(os.environ["PROVENANCE_KEY"], base_url=os.environ["PROVENANCE_API"])

@function_tool
def pay_invoice(invoice: str, amount: float, payee: str) -> str:
    """Pay a supplier invoice."""
    return f"paid {invoice}"

gated = with_authorize(
    pay_invoice,
    client=prov,
    device_id="dev_00000000000000000000000000000001",
    describe=lambda a: f"Pay invoice {a['invoice']}: ${a['amount']} to {a['payee']}",
)

agent = Agent(name="Billing", instructions="Pay supplier invoices.", tools=[gated])
```

Without `describe`, the action description is the tool name and the canonical JSON of its arguments. The payload is `{ "tool", "input" }` by default, so the whole input is hash-bound to the token.

## 2. Resolve the interruptions

Run the agent as usual, then hand the result to `resolveApprovals`. It decides each interruption for a gated tool and resumes the run until none are left:

**Node**

```ts
let result = await run(agent, "Pay invoice INV-2041 from Brindlecote Supplies");
result = await resolveApprovals(result, { agent, run, agentSessionId: "sess_oai_1" });
console.log(result.finalOutput);
```

**Python**

```python
result = await Runner.run(agent, "Pay invoice INV-2041 from Brindlecote Supplies")
result = await resolve_approvals(result, agent=agent, agent_session_id="sess_oai_1")
print(result.final_output)
```

| Outcome | What happens |
| --- | --- |
| Verified | The call is approved and the tool runs with the arguments that were verified |
| Denied | The call is rejected, and the model reads a sentence such as "The person declined this action." |
| Waiting for approval | It waits for the person, up to `timeoutMs` (default 2 minutes), then approves or rejects |

If the person hasn't answered when the wait ends, the model reads "Waiting for approval (appr_…)", and the next call with the same arguments reuses the token that opened the approval. Interruptions for tools you didn't gate stay on the result for your own handler. In Python, extra keyword arguments such as `session` or `context` go to `Runner.run` when the run resumes, and the agent session defaults to the session's id.

## 3. Pass the rest of the options

- `context` (function): Facts for your [policy](https://provenanceauth.com/docs/concepts/policies.md), read at verify.
- `payload` (function): The payload bound into the token. Default: the tool name and its whole input.
- `runId` (string): An [agent-team run](https://provenanceauth.com/docs/mandate/guides/halt-a-run.md) to bind each call to (`run_id` in Python).
- `mandateToken` (string): A grant to authorize under (`mandate_token`).
- `agentId` (string): The registered agent making the calls (`agent_id`).
- `timeoutMs` (number): How long to wait for the person (`timeout_ms`). `0` answers at once.

On an `openai-agents` release without tool approvals, such as 0.3, `with_authorize` makes the same decision inside the tool instead, and a denial is the tool's result. `HAS_TOOL_APPROVAL` tells you which applies.

## Errors you might see

| Symptom | Cause | Fix |
| --- | --- | --- |
| The run ends with interruptions left | The tool wasn't wrapped with `withAuthorize`, or the agent passed to `resolveApprovals` isn't the one that ran | Pass the agent the run started with. |
| The model reads "Waiting for approval" | The person hadn't answered within `timeoutMs` | Raise `timeoutMs`, or call the agent again after they approve. |
| `TypeError: pass a function tool` | A hosted tool, such as web search, was passed | Gate function tools only. |

## See also

- [Framework integrations](https://provenanceauth.com/docs/mandate/guides/framework-integrations.md)
- [Step-up approval](https://provenanceauth.com/docs/mandate/guides/step-up-approval.md)
- [Execution binding](https://provenanceauth.com/docs/mandate/guides/execution-binding.md)
