Skip to main content
  • Python 3.9+
  • One runtime dependency: httpx
  • Optional extra: benchspan[langchain] (only if you’re using LangChain / CrewAI)

BenchGuard

Constructor arguments

api_key
str
required
Bearer API key from Dashboard → API Keys. Format: ag_live_....
agent
str | None
Optional label to tag every scan from this instance. Useful for filtering per-agent traffic in the dashboard.
mode
"block" | "warn"
default:"\"block\""
In block mode, callbacks/hooks raise InjectionDetectedError on detection (synchronous scan; adds typical sub-100ms scan latency). In warn mode, the scan runs in a daemon thread and the LLM call proceeds with zero added latency; detections land in your dashboard asynchronously. See Modes.
api_url
str
default:"https://api.benchspan.com"
Override the API host. Used for self-hosted deployments.

Methods

guard.scan(input, role="user", source=None) → ScanResult

Scan a single string synchronously. Returns the verdict; does not raise on injection. Use framework integrations or wrap for auto-raise behavior.

guard.scan_async(input, role="user", source=None) → ScanResult

Async variant of scan. Use inside async def functions.

@guard.wrap

Decorator for functions that call the LLM directly (raw OpenAI / Anthropic SDK, etc.). Scans the messages argument before the function runs.

@guard.wrap_async

Async variant of @guard.wrap. For async def functions.

LangChain / CrewAI callback

BenchGuard implements BaseCallbackHandler directly. Pass it where LangChain accepts callbacks:
See LangChain integration and CrewAI integration.

guard.as_agent_hooks()

Returns an AgentHooksBase subclass (OpenAI Agents SDK) with on_tool_end wired up. See OpenAI Agents integration.

guard.as_adk_callback()

Returns a before_model_callback for Google ADK. See Google ADK integration.

Types

ScanResult

InjectionDetectedError

Raised by @guard.wrap, @guard.wrap_async, and all framework integrations when verdict == "block".

Logging

The SDK logs to the benchspan logger. Attach a handler to see warn detections and debug output:

Thread / async safety

Each BenchGuard instance maintains a dedup cache of scanned message hashes, so don’t share an instance across unrelated agent runs if you need each run to get a fresh cache. A new instance is cheap; it’s just a config bag around httpx.