Claude Agent SDK integration for Pisama detection - real-time failure prevention via hooks
Project description
pisama-agent-sdk
Real-time failure detection hooks for the Claude Agent SDK. Catches infinite loops, hallucinations, and other failure modes before they cause damage.
Part of the Pisama platform for multi-agent failure detection.
Install
pip install pisama-agent-sdk
Quick Start
Passive Monitoring (hooks)
Add two lines to your Claude Agent SDK setup:
from pisama_agent_sdk import pre_tool_use_hook, post_tool_use_hook
agent.hooks.pre_tool_use = pre_tool_use_hook
agent.hooks.post_tool_use = post_tool_use_hook
Every tool call is now checked for failure patterns in real-time (<100ms). If a loop or other issue is detected, the hook returns a blocking signal to stop the agent.
Active Self-Check
Let the agent verify its own output:
from pisama_agent_sdk import check
result = await check(
output="The server is healthy based on the metrics.",
context={"query": "Is auth-service down?", "sources": [...]},
)
if not result["passed"]:
# result["issues"] describes what went wrong
print(result["issues"])
Custom Tool for Claude Agent SDK
Give the agent a tool it can call to self-check:
from pisama_agent_sdk import create_check_tool
from claude_agent_sdk import ClaudeAgentOptions
options = ClaudeAgentOptions(
custom_tools=[create_check_tool()],
)
Configuration
from pisama_agent_sdk import configure_bridge, BridgeConfig
configure_bridge(BridgeConfig(
fail_open=True, # Allow execution if detection errors (default: True)
detection_timeout_ms=80, # Max detection time per hook (default: 80)
))
Tool Matchers
Control which tools get checked:
from pisama_agent_sdk import PreToolUseHook, create_matcher, FILE_TOOLS, SHELL_TOOLS
# Only check file and shell tools
hook = PreToolUseHook(matcher=create_matcher(include=[FILE_TOOLS, SHELL_TOOLS]))
agent.hooks.pre_tool_use = hook
Built-in matchers: ALL_TOOLS, FILE_TOOLS, SHELL_TOOLS, DANGEROUS_COMMANDS, AGENT_TOOLS.
How It Works
- Your agent makes a tool call
pre_tool_use_hookconverts the call into a PisamaSpan- Registered detectors run against the span + recent session context
- If a failure is detected (e.g., 5th consecutive
Readof the same file), the hook returns a blocking result - The agent receives the block signal and adjusts its behavior
Detection runs entirely locally using pisama-core detectors. No network calls unless you configure a remote endpoint.
Evaluator Mode
Use Pisama as an evaluator in multi-agent harnesses:
from pisama_agent_sdk import PisamaEvaluator
evaluator = PisamaEvaluator(endpoint="https://your-pisama-instance/api/v1")
result = await evaluator.evaluate(trace_data)
print(result.passed, result.failures)
Requires pip install pisama-agent-sdk[evaluator].
License
MIT
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file pisama_agent_sdk-0.1.2.tar.gz.
File metadata
- Download URL: pisama_agent_sdk-0.1.2.tar.gz
- Upload date:
- Size: 26.7 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.14.2
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
0c43af875d91b16a838ddfa42c09daab86a8617053604f9f55c3bff00c69db82
|
|
| MD5 |
4dfcc36c3890575a2e24cdb9a8f7eee4
|
|
| BLAKE2b-256 |
89e741be826377c553479bbac5b000de99f68a2fa17afe78adc51921d6998039
|
File details
Details for the file pisama_agent_sdk-0.1.2-py3-none-any.whl.
File metadata
- Download URL: pisama_agent_sdk-0.1.2-py3-none-any.whl
- Upload date:
- Size: 30.3 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.14.2
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
5b960013d509f308b4803ebfe5244c1a3cbb18d9d603d40346c7dd97bd9fad3b
|
|
| MD5 |
dcfcccaa786b9432147329895984f404
|
|
| BLAKE2b-256 |
72404cb5ea396619c0f99c77405b38138acdec39f9e1adb6941a591bbabbe883
|