Skip to main content

Core detection, scoring, and healing engine for Pisama agent forensics

Project description

pisama-core

Detection, scoring, and healing engine for AI agent systems. Detect failure modes like infinite loops, hallucinations, cost overruns, and coordination breakdowns in your LLM agents -- entirely offline, no API keys required.

Part of the Pisama platform for multi-agent failure detection.

Install

pip install pisama-core

Quick Start

import asyncio
from pisama_core import Trace, SpanKind, DetectionOrchestrator

# Build a trace from your agent's execution
trace = Trace()
for i in range(8):
    trace.create_span(name="Read", kind=SpanKind.TOOL)

# Run all built-in detectors
orchestrator = DetectionOrchestrator()
result = asyncio.run(orchestrator.analyze(trace))

for detection in result.detections:
    print(f"[{detection.detector_name}] {detection.summary}")
    print(f"  Severity: {detection.severity}/100")
    print(f"  Fix: {detection.fix_recommendation.instruction}")

Output:

[loop] Tool 'Read' repeated 8x consecutively
  Severity: 45/100
  Fix: Stop the current loop. Try a different approach or ask the user for guidance.

No API key. No network calls. Runs completely locally.

Built-in Detectors

Detector What it catches
Loop Consecutive repetitions, cyclic patterns (A->B->A->B), low tool diversity
Repetition Similar actions with slight variations, tool dominance
Cost Token budget overruns, excessive LLM/tool calls
Hallucination Failed file operations, error rate spikes
Coordination Message storms, agent imbalance, handoff loops

All detectors support both batch analysis (full trace) and real-time hooks (per-span).

Use Individual Detectors

import asyncio
from pisama_core import Trace, SpanKind
from pisama_core.detection.detectors.loop import LoopDetector
from pisama_core.detection.detectors.cost import CostDetector

trace = Trace()
# ... add spans representing your agent's execution

loop = LoopDetector()
cost = CostDetector()

loop_result = asyncio.run(loop.detect(trace))
cost_result = asyncio.run(cost.detect(trace))

if loop_result.detected:
    print(f"Loop detected: {loop_result.summary}")

Write Your Own Detector

from pisama_core import BaseDetector, DetectionResult, Trace
from pisama_core.detection.result import FixType

class MyDetector(BaseDetector):
    name = "my_detector"
    description = "Detects my custom failure pattern"
    version = "0.1.0"

    async def detect(self, trace: Trace) -> DetectionResult:
        # Your detection logic here
        tool_names = trace.get_tool_sequence()
        if len(set(tool_names)) == 1 and len(tool_names) > 5:
            return DetectionResult.issue_found(
                detector_name=self.name,
                severity=50,
                summary="Agent is stuck using a single tool",
                fix_type=FixType.SWITCH_STRATEGY,
                fix_instruction="Try a different approach",
            )
        return DetectionResult.no_issue(self.name)

Register it so the orchestrator picks it up:

from pisama_core import registry
registry.register(MyDetector())

Core Concepts

  • Trace -- A complete agent execution session containing multiple spans
  • Span -- A single unit of work (tool call, LLM inference, agent turn) with kind, timing, and optional I/O data
  • DetectionResult -- Detector output: issue found (yes/no), severity (0-100), evidence, fix recommendation
  • DetectorRegistry -- Plugin system for registering detectors (built-ins auto-register on import)
  • DetectionOrchestrator -- Runs all registered detectors and aggregates results

Platform Support

Traces are framework-agnostic. Set platform for platform-aware threshold tuning:

from pisama_core import Trace, TraceMetadata, Platform

trace = Trace(metadata=TraceMetadata(platform=Platform.LANGGRAPH))

Works with Claude Agent SDK, LangGraph, AutoGen, CrewAI, n8n, Dify, and custom agents.

Pisama Platform

For production monitoring with 25+ calibrated detectors, ML-based detection, LLM-as-judge verification, and a dashboard, see pisama.ai.

License

MIT

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

pisama_core-1.6.2.tar.gz (221.5 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

pisama_core-1.6.2-py3-none-any.whl (234.7 kB view details)

Uploaded Python 3

File details

Details for the file pisama_core-1.6.2.tar.gz.

File metadata

  • Download URL: pisama_core-1.6.2.tar.gz
  • Upload date:
  • Size: 221.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for pisama_core-1.6.2.tar.gz
Algorithm Hash digest
SHA256 a8a5ca0758b082294caef3933acccca4d01f2fedb85e8abbbe15fb0b43d96f73
MD5 86361e831580d44bdb66a35776e7769d
BLAKE2b-256 64005ffd9d03fc78eb952e85b33c6df6b7cb119c5a4b492f66c08ebea651d3e3

See more details on using hashes here.

Provenance

The following attestation bundles were made for pisama_core-1.6.2.tar.gz:

Publisher: publish.yml on tn-pisama/pisama

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file pisama_core-1.6.2-py3-none-any.whl.

File metadata

  • Download URL: pisama_core-1.6.2-py3-none-any.whl
  • Upload date:
  • Size: 234.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for pisama_core-1.6.2-py3-none-any.whl
Algorithm Hash digest
SHA256 c9703a0c1949ecd36e3e30caf3b6bfc652eb013c18db11cad1e74a905a57049e
MD5 e36888f7dfaca7547a1128198650ebe8
BLAKE2b-256 a573ff9cae16617b34ab58d12ec355eaa6a7aef7eace039a8ae7f6ecf8e11514

See more details on using hashes here.

Provenance

The following attestation bundles were made for pisama_core-1.6.2-py3-none-any.whl:

Publisher: publish.yml on tn-pisama/pisama

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page