Integrations
OpenAI Agents SDK
Authorize tool calls in the OpenAI Agents SDK using run context for identity.
The OpenAI Agents SDK runs your tool functions when the model calls them. Put the ReQlo check inside the tool, and take identity from the run context your server passes in, which the model can't see or change.
Python
import os
from dataclasses import dataclass
from agents import Agent, Runner, RunContextWrapper, function_tool
from reqlo import AsyncReqlo, ReqloDeniedError
reqlo = AsyncReqlo(api_key=os.environ["REQLO_API_KEY"])
@dataclass
class AppContext:
user_id: str # from your verified session
agent_id: str = "agt_assistant_42"
@function_tool
async def read_document(ctx: RunContextWrapper[AppContext], document_id: str) -> str:
"""Read a workspace document by ID."""
doc = await documents.get(document_id) # authoritative lookup
try:
await reqlo.enforce(
agent={"id": ctx.context.agent_id},
subject={"id": ctx.context.user_id, "kind": "user"},
action="documents.read",
resource={"type": "document", "id": doc.id, "attributes": {"owner_id": doc.owner_id}},
context={"document_classification": doc.classification},
)
except ReqloDeniedError:
return "You don't have access to that document."
return doc.content
assistant = Agent[AppContext](
name="Document assistant",
instructions="Answer questions using workspace documents.",
tools=[read_document],
)
async def handle(request, session):
result = await Runner.run(assistant, input=request.message, context=AppContext(user_id=session.user_id))
return result.final_outputNotes
- Model-provided arguments (
document_id) are only used to look up the resource; ownership comes from your database. - Errors other than denial propagate, so the tool fails instead of returning data.
- Handoffs to other agents should pass their own
agent_id; permissions aren't inherited.
Something wrong or unclear? Report an issue with this page.