Skip to main content

Constraint Architecture

LLM reasons. Code decides what's allowed. Audit keeps it traceable.

Python 3.10+ License: MIT Version

A production-grade abstract skeleton for AI agent systems. Four deterministic constraint layers wrapped around an LLM reasoning core — independently validated in two completely unrelated domains.


Quickstart

pip install constraint-architecture
from constraint_architecture import ConstraintEngine, ValidationResult

class MyGuardrails(ConstraintEngine):
    async def validate(self, context, reasoning_output):
        if "rm -rf" in reasoning_output.get("command", ""):
            return ValidationResult(
                allowed=False,
                reason="destructive command blocked"
            )
        return ValidationResult(allowed=True)

Done. Your agent now has a deterministic constraint layer the LLM cannot override.


The Architecture

User Input → [1. LLM Reasoning] → [2. Code Constraints] → [3. Sandbox Execute] → [4. Audit Trail]
                  "think"            "decide if allowed"      "do it safely"        "prove it happened"
Layer What it does Runs on
1. Reasoning LLM freely reasons about what to do Probabilistic
2. Constraints Deterministic validation. Rules only increase, never decrease Code
3. Execution Tiered execution (auto / confirm / veto). Role enforcement Code
4. Audit Immutable SHA256 hash chain. Every step, every decision Code

Core principle: the LLM can only propose. Code decides what actually executes. No amount of prompt engineering can bypass a deterministic constraint.


What's Inside

This package is an abstract skeleton — not a framework, not a library, not a service. It defines the interfaces and data contracts that every agent system needs:

constraint_architecture/
├── skeleton.py          # 1384 lines — the full skeleton
│   ├── 7 ABCs           # Shell, Identity, Scheduler, Memory, Protocol, Tools
│   ├── 7 Protocols      # Duck-typing versions (for non-inheritance wiring)
│   ├── 4 Constraint ABCs # ReasoningLayer, ConstraintEngine, ExecutionProxy, AuditTrail
│   ├── 12 dataclasses   # 10 frozen (immutable), 2 mutable
│   ├── 4 custom exceptions # VetoError, ExecutionRefusedError, AuditIntegrityError
│   ├── 4 enums          # Tier (auto/confirm/veto), EventType, MemoryLevel
│   └── 25 self-checks   # Automated: ABCs uninstantiable, frozen immutable, bounds validated
└── __init__.py           # Re-exports the full public API

You subclass the ABCs for your domain. The wiring is yours — the skeleton doesn't know whether you're building a security agent or a music DJ. That's the point.


Why This Exists

I'm a freshman. Three weeks after touching AI agents for the first time, I had built two of them:

  • Kylin-Agent: security-hardened ops agent deployed on Kylin OS V11. 16 red-team attacks, 0 breaches. 149 tests.
  • Malio: embodied AI music agent. 800 particles, 9 physics systems. PersonaEngine with central-bank-style emotional currency.

After finishing both, I realized they share the same four-layer architecture — despite being in completely unrelated domains. This skeleton is the extraction of that pattern.

If someone uses this architecture in a third domain (healthcare? finance? education?), that third independent validation would prove the pattern is real.


Certified: Production Engineering Quality

  • ABC + Protocol dual-interface: every subsystem exposes both an abstract class (for inheritance) and a Protocol (for duck-typing)
  • 10 of 12 dataclasses frozen: downstream code cannot silently flip a veto into a pass
  • __post_init__ validation: risk_score bounded 0-9, ScheduleTrigger type-enforced, AuditEvent.prev_hash mandatory
  • 4 custom exceptions: each carries structured, machine-consumable fields (VetoError has reason + alternative + ref)
  • 25 automated self-checks: run python -m constraint_architecture.skeleton to verify integrity
  • Design rationale in every class: not just "what", but "why this design and not the alternative"

Requirements

  • Python 3.10+
  • Zero dependencies. Zero.

You don't need FastAPI, Redis, PostgreSQL, JWT, or any other infrastructure. This skeleton defines contracts. The implementation is yours.


Reference Implementations

Project Domain Key Metric
Kylin-Agent Security ops on Kylin OS V11 16 attacks, 0 breaches
Malio Embodied AI music 800 particles, 9 physics systems
Quant Trading Daily multi-factor strategy Sharpe 1.36, MDD 12.7%

Same skeleton. Three domains. Different wiring.


Related Reading


License

MIT — Xu Renwu (续仁舞), 2026.

大一。三周。一个人。

Metadata

Release files for constraint-architecture 1.5.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 constraint-architecture 1.5.1
File Size Uploaded Provenance Actions
constraint_architecture-1.5.1.tar.gz 24.7 kB With provenance Details for constraint_architecture-1.5.1.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for constraint-architecture 1.5.1
File Interpreter ABI Platform Actions
constraint_architecture-1.5.1-py3-none-any.whl Python 3 none any Details for constraint_architecture-1.5.1-py3-none-any.whl

Total release size: 49.0 kB

Release files / constraint_architecture-1.5.1.tar.gz

Download URL constraint_architecture-1.5.1.tar.gz
Size 24.7 kB
Tags Source
SHA-256 checksum
How to use checksums
5c5bac9903d71af7af8b6f450453f4e7d88b7a2ffec49ad20f667b9d25ccb7cb
BLAKE2b-256 checksum
How to use checksums
9c0665b7d69fbb539302b80730fe15a5f75d9a943c748404a63033c5e2af1e55
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/6.1.0 CPython/3.13.12

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.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Jun 20, 2026.

Transparency log

Release files / constraint_architecture-1.5.1-py3-none-any.whl

Download URL constraint_architecture-1.5.1-py3-none-any.whl
Size 24.3 kB
Tags Python 3
SHA-256 checksum
How to use checksums
9b12dba8308c581d9895237a924853e51fb09f6c03d1016d7353cb4d03520b1b
BLAKE2b-256 checksum
How to use checksums
2525f5dffd04360edc8ec8d601bfc3fe0d32b4f13819a36a0c4a62f5440a6665
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/6.1.0 CPython/3.13.12

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.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Jun 20, 2026.

Transparency log

Security for constraint-architecture 1.5.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.

Full provenance

All 2 files in this release have been uploaded with provenance metadata.

Select from 2 files

Attestation for constraint_architecture-1.5.1.tar.gz

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Jun 20, 2026.

Transparency log

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

1.5.1 This release

2 release files Full 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