Skip to main content
The OpenAI Agents SDK supports lifecycle hooks. Benchspan provides on_tool_end hooks that scan every tool return value before it’s fed back into the model.

Python

1

Install

2

Attach hooks to your agent

agent.py
The Agents SDK wraps exceptions raised from hooks in a UserError. Check the string or inspect e.__cause__ for the original InjectionDetectedError.

TypeScript

1

Install

2

Attach hooks

agent.ts

What gets scanned

Every string return value from a tool, via the on_tool_end / onToolEnd hook. The tool’s name is passed through as source so you can see which tools produce the most injections in your dashboard. User inputs (from Runner.run(agent, input=...)) are not scanned by this hook. To also scan user input, call guard.scan(user_input, role="user") before Runner.run, or use the raw decorator approach from the OpenAI raw SDK integration.

Full example with blocking