Skip to main content

AISquare Explainability SDK

An explainability and governance stack for AI agents. Captures execution traces from any Python agent (Agno, LangChain, plain Python), delivers them to a FastAPI gateway, and projects them into a Neo4j graph with RML semantic analysis and policy detection.

How it works

Your Agent Code
    │  OpenInference / OTel spans
    ▼
SDK (aisquare_explainability_sdk/)
    │  serialised to local SQLite inbox — no gateway dependency in the hot path
    ▼
InboxSweeper (background thread)
    │  POST /v1/traces/ingest
    ▼
Gateway (FastAPI — gateway/)
    ├── Structural Worker  →  Neo4j  (Run / Span / Artifact / Policy graph)
    ├── RML Extractor      →  Postgres (claims, assumptions, evidence, inference chain)
    └── Policy Detector    →  Neo4j + Postgres (PolicyCandidate nodes, crystallization loop)
         ▼
Explainability Studio UI (modular-ai-space frontend)

Repo layout

aisquare_explainability_sdk/   SDK — capture, inbox, sweeper, adapters, manual tracers
gateway/                        FastAPI ingest gateway + worker pipeline
graph/                          Neo4j Cypher and React Flow transformation helpers
examples/                       Runnable agent examples
docs/                           Guides, concepts, API reference
tests/                          Unit and integration tests

What the SDK captures

The SDK collects two layers of signal:

Auto-instrumentation (zero-code): The AgnoAdapter installs openinference-instrumentation-agno, which automatically wraps every Agno agent run, LLM call, and tool invocation as an OTel span.

Manual tracers (governance-grade): Seven context-manager tracers you can add to any Python code — framework-agnostic:

Tracer Purpose
AgentRunTracer Wraps a full agent run as the root span
LLMCallTracer Records an LLM inference call with I/O and token counts
ToolCallTracer Records a tool invocation with parameters, result, and errors
RetrievalTracer Records a RAG retrieval with documents and scores
HumanInterventionTracer Records a human-in-the-loop review or correction
RoutingTracer Records a routing/delegation decision with selected and rejected paths
MemoryTracer Records memory read/write operations

The manual tracers also provide decorators: @trace_tool and @trace_retrieval.

What the gateway adds

The gateway worker pipeline runs after each trace is ingested:

  • Structural projection: Every span becomes a Run, Span, Artifact, or policy node in Neo4j with typed edges (CONTAINS, EXECUTED, PRODUCED, CONSUMED, FLAGGED_AS).
  • RML extraction: GPT-4o-mini analyzes AGENT and LLM spans to produce claims, assumptions, evidence attribution, inference chain, and policy triggers. Confidence is a blended score: 40% LLM-reported + 60% deterministic structural signals.
  • Policy detection: Recurring system-prompt instructions are counted across traces. When an instruction exceeds the occurrence threshold it is promoted to a PolicyCandidate node for human review and activation.

Data model

Neo4j nodes: Studio, Run, Span, Event, Artifact, Agent, Tool, Human, PolicyCandidate, Policy

Neo4j relationships: HOSTS, CONTAINS, NEXT, EXECUTED, INTERVENED, CONSUMED, PRODUCED, REFERENCES, FLAGGED_AS, PROPOSES

Postgres tables: ingest_batches, trace_states, processed_traces, rml_analyses, policy_observations, artifact_content, extraction_failures, studio_config

SDK local durability: SQLite inbox (explainability_inbox.db)

Local setup

1. Install dependencies

pip install -e ".[dev,agno]"

2. Start infrastructure

docker compose up -d

Starts Neo4j (bolt://localhost:7687), Postgres (localhost:5433), and Redis (localhost:6379).

3. Configure environment

Copy .env.example to .env and fill in the required values:

# Database
NEO4J_URI=bolt://localhost:7687
NEO4J_USER=neo4j
NEO4J_PASSWORD=password
POSTGRES_DSN=postgresql://postgres:postgres@localhost:5433/explainability
REDIS_URL=redis://localhost:6379/0

# Auth
ENV=development
ALLOW_TEST_AUTH_BYPASS=true

# SDK → gateway
EXPLAINABILITY_GATEWAY_URL=http://127.0.0.1:8000
EXPLAINABILITY_API_KEY=test-key

# LLM extraction
OPENAI_API_KEY=sk-...
EXTRACTOR_MODEL=gpt-4o-mini

4. Run the gateway

uvicorn gateway.main:app --host 0.0.0.0 --port 8000

Health checks:

curl http://127.0.0.1:8000/live    # → {"status":"ok"}
curl http://127.0.0.1:8000/ready   # → {"neo4j":"ok","postgres":"ok","redis":"ok"}

5. Run an example

python examples/basics/openai_chat.py

6. Start the frontend

The UI lives in the modular-ai-space repository:

cd ../modular-ai-space
npm install
npm run dev:localdev    # http://localhost:3001

SDK usage

import aisquare_explainability_sdk as sdk

# Reads EXPLAINABILITY_GATEWAY_URL and EXPLAINABILITY_API_KEY from environment
sdk.init_from_env(service_name="my-agent")

# Run your agent — Agno is auto-instrumented, all spans captured automatically
agent.print_response("...")

# IMPORTANT for short-lived scripts: flush ensures traces reach the gateway
# before the process exits. Long-running services don't need this.
sdk.flush()

Manual instrumentation (any framework)

import aisquare_explainability_sdk as sdk

sdk.init_from_env()

with sdk.AgentRunTracer(agent_name="MyAgent", run_id="abc-123") as run:
    run.set_input("User query")

    with sdk.LLMCallTracer(model="gpt-4o-mini", provider="openai") as llm:
        response = call_openai(...)
        llm.set_input_messages([{"role": "user", "content": "..."}])
        llm.set_output_messages([{"role": "assistant", "content": response}])
        llm.set_token_counts(prompt=100, completion=50)

    with sdk.RoutingTracer(decision_type="tool_selection") as rt:
        rt.set_selected("web_search", reason="Query requires fresh data")
        rt.set_rejected([{"name": "cached_search", "reason": "Cache is stale"}])

    run.set_output("Agent final answer")

sdk.flush()

Examples

File What it shows
examples/basics/openai_chat.py Simplest traced Agno agent
examples/agents/deep_nesting.py 3-level agent delegation team
examples/agents/multi_agent_branching.py Multi-agent research team
examples/agents/routing_decision.py Tool routing and conditional paths
examples/agents/tool_heavy_workflow.py Many tool calls in one run
examples/governance/error_handling.py Error nodes (red) in graph
examples/governance/policy_detection.py 3 runs → policy candidate detection
examples/governance/policy_workflow.py Full detect → review → activate cycle
examples/governance/e2e_governed_workflow.py Complete governed workflow with activation
examples/streaming/monitor_execution.py SSE-based live execution monitoring

API surface

Ingest:

POST /v1/traces/ingest
Authorization: Bearer <api-key>
Content-Type: application/json

Studio UI reads:

GET  /v1/studios/{id}/ui/runs
GET  /v1/studios/{id}/ui/runs/{run_id}
GET  /v1/studios/{id}/ui/runs/{run_id}/graph?mode=span|event
GET  /v1/studios/{id}/ui/runs/{run_id}/xtrace
GET  /v1/studios/{id}/ui/runs/{run_id}/rml
GET  /v1/studios/{id}/ui/runs/{run_id}/nodes/{node_id}
GET  /v1/studios/{id}/ui/policies
GET  /v1/studios/{id}/ui/agents
GET  /v1/studios/{id}/ui/setup
GET  /v1/studios/{id}/artifacts/{artifact_id}

Policy management:

PATCH /v1/studios/{id}/policies/{fingerprint}/activate
PATCH /v1/studios/{id}/policy-config

Admin:

GET  /metrics
GET  /v1/admin/worker-health
GET  /v1/admin/circuit-breakers
POST /v1/admin/recover
GET  /v1/admin/extraction/failures
GET  /v1/admin/policy-candidates

Documentation

Guides:

Reference:

Concepts:

Tests

python -m pytest -q
python -m ruff check .

Docker-backed integration test:

RUN_DOCKER_INTEGRATION=1 python -m pytest tests/test_integration_stack.py -q

License

MIT

Metadata

Release files for aisquare-explainability 0.1.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for aisquare-explainability 0.1.1
File Size Uploaded Provenance Actions
aisquare_explainability-0.1.1.tar.gz 115.3 kB Without provenance Details for aisquare_explainability-0.1.1.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for aisquare-explainability 0.1.1
File Interpreter ABI Platform Actions
aisquare_explainability-0.1.1-py3-none-any.whl Python 3 none any Details for aisquare_explainability-0.1.1-py3-none-any.whl

Total release size: 145.1 kB

Release files / aisquare_explainability-0.1.1.tar.gz

Download URL aisquare_explainability-0.1.1.tar.gz
Size 115.3 kB
Tags Source
SHA-256 checksum
How to use checksums
9062506e2edec88959f1da45bc5eebe13fcc7f8b9c2b4243340cf7d836e3f529
BLAKE2b-256 checksum
How to use checksums
ef354b0cd489ca6b5b98b0e8eabcf948fbac7cf0a71afceb59d880d8cd99f0c1
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.12.3

Release files / aisquare_explainability-0.1.1-py3-none-any.whl

Download URL aisquare_explainability-0.1.1-py3-none-any.whl
Size 29.8 kB
Tags Python 3
SHA-256 checksum
How to use checksums
b6071d6ec47ab22888c96fbda429b1c713af23332058bec214a11fda5efb9f2f
BLAKE2b-256 checksum
How to use checksums
4ee8dd09d0a7cdb2786d4e3d9b604ac8e3776a516d5a8b4280ddfa050a3ef869
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.12.3

Security for aisquare-explainability 0.1.1

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Release history Release notifications | RSS feed

This release

0.1.1 This release

2 release files No provenance

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page