Skip to main content

Anchor

The PostgreSQL-Authoritative Durable Execution Engine for AI Agent Workflows.

Eliminate lost state and duplicate API calls when executing multi-step LLM agent pipelines. Anchor guarantees atomic two-phase tool journaling, monotonic epoch fencing, and sub-second crash recovery.

Apache 2.0 License PyPI version Python 3.12

Author & System Architect: Aditya Nemalinkedin.com/in/adityaxnemaGitHub Repository


🚀 Quickstart (Under 60 Seconds)

No API keys, no cloud subscriptions, no external services required to get started.

1. Install the Python SDK & Initialize Workspace

pip install anchor-runtime && anchor init

Scaffolds starter app.py workflow template and docker-compose.yml cluster configuration in your current directory.

2. Boot Local Cluster Stack

docker compose up -d

Brings up PostgreSQL 16 (with DDL triggers AN001AN004), Redis 7, the API Server (http://localhost:8000), 3 worker replicas, and the Operator Console UI at http://localhost:3000.

3. Write & Run Your Agent (app.py)

Create app.py:

import anchor, json

# 1. Custom Tool 0: Fetch Customer Data (Retry-Safe)
@anchor.tool(safety="retry_safe", naturally_idempotent=True)
def fetch_customer(customer_id: str) -> dict:
    return {"id": customer_id, "email": "aditya@anchor.dev", "tier": "VIP"}

# 2. Custom Tool 1: Dispatch Email Notification (Unsafe Side-Effect)
@anchor.tool(safety="unsafe")
def send_welcome_email(email: str, tier: str) -> dict:
    return {"status": "sent", "to": email, "tier": tier}

# 3. Multi-Tool Durable Agent Workflow
@anchor.agent(name="onboarding_agent")
def onboarding_agent(ctx: anchor.StepContext):
    customer = yield anchor.ToolCall("fetch_customer", {"customer_id": ctx.input["customer_id"]})
    email_res = yield anchor.ToolCall("send_welcome_email", {"email": customer["email"], "tier": customer["tier"]})
    yield anchor.Done({"status": "completed", "customer": customer, "email": email_res})

# 4. Trigger & Submit to Cluster
if __name__ == "__main__":
    result = anchor.run("onboarding_agent", input={"customer_id": "cust_99"})
    print(json.dumps(result, indent=2))

Run python app.py. anchor.run() serializes the workflow AST and submits it to PostgreSQL. Cluster workers claim the run, execute steps, and log atomic two-phase tool journals. Inspect live execution at http://localhost:3000!


💡 Why Choose Anchor?

Current AI agent frameworks (LangGraph, CrewAI) rely on in-memory buffers or naive Redis checkpoints — causing process crashes to re-execute non-idempotent tool calls, double-charge payment APIs, and corrupt database state.

Meanwhile, legacy enterprise orchestrators (Temporal, AWS Step Functions) require hosting massive external clusters ($5,000+/mo cloud tax) built for microservices, not non-deterministic Python LLM loops.

Anchor fills this void as a lightweight, PostgreSQL-authoritative engine embedding atomic two-phase tool journaling (INTENT / RESULT) and monotonic epoch fencing (AN001) to guarantee zero duplicate side-effects and sub-second recovery natively in SQL.

📊 Competitive Architecture Comparison Matrix

Feature Anchor Runtime LangGraph / CrewAI Temporal / Step Functions
State Authority PostgreSQL 16 Engine (FOR UPDATE SKIP LOCKED) Volatile Memory / Redis Checkpoints Dedicated Cassandra / MySQL Cluster
Infrastructure Tax $0/mo (Runs in existing DB) $0/mo (Unsafe) $5,000+/mo (Massive External Cluster)
Two-Phase Side-Effect Guard Atomic INTENT / RESULT Journal ❌ Duplicate API Calls ⚠️ Activity Heartbeats
Monotonic Epoch Fencing Database Constraint (AN001) ❌ Split-Brain Risk ❌ Application-Level
SIGKILL Recovery Time P50 < 3.1s ❌ Process Crash Data Loss ⚠️ 10s+ Timeout Window
Developer API Native Python Generators Complex Graph State Handoffs Multi-File SDK Boilerplate

💰 Financial ROI & Execution Safety

When AI agents execute multi-step tasks — searching database records, calling third-party APIs, or processing payments — server crashes normally result in lost progress and double-billing. Anchor acts as an immutable flight recorder: every step is saved before it runs, so if a server dies, another takes over instantly with zero wasted credits.

  • Financial Savings Analysis: On 1,000,000 multi-step LLM requests per month with a 2% node crash rate, unmanaged retries cost over $12,400/mo in duplicate prompt tokens. Anchor's step-level result cache reduces wasted token charges to $0.
  • Idempotent Side-Effect Guarding: If a worker process is terminated by Kubernetes SIGKILL while calling a payment endpoint or database mutation, Anchor checks the TOOL_INTENT sequence ID on recovery to prevent duplicate charges or corrupt row insertions.

🏗️ Deep Architectural Intricacies & System Invariants

Anchor enforces mathematical correctness through five formal SQL invariants verified continuously by an automated chaos harness:

1. Database-Authoritative State Engine

All run ownership, sequence allocation, and lease renewals occur inside single PostgreSQL transactions using SELECT ... FOR UPDATE SKIP LOCKED CTEs. No component outside the database is ever authoritative about who owns an agent run.

2. Two-Phase Tool Intent Journaling

Before a side-effect tool call is executed, Anchor writes a TOOL_INTENT record. Upon completion, it commits TOOL_RESULT. On crash recovery, replayed steps load cached results in <5ms without executing side effects a second time.

3. Monotonic Epoch Fencing (AN001_FENCED_WRITE)

Every worker lease renewal or run claim increments the run's monotonic epoch token. Delayed writes from a zombie worker with a stale epoch are blocked at the database constraint boundary with AN001_FENCED_WRITE.

4. Explicit Tool Safety Spectrum

  • retry_safe: Read-only or naturally idempotent tools. Safe to re-execute immediately on worker crash recovery.
  • reconcilable: Side-effecting tools accepting idempotency keys. Anchor queries external system state before re-running.
  • unsafe: Non-idempotent side effects (e.g., sending emails or wire transfers). If a crash lands in the uncertainty window, Anchor halts the run to the needs_review queue for human approval instead of guessing.

5. Automated Adversarial Chaos Harness

An integrated chaos harness continuously injects process SIGKILL signals against active worker nodes and runs automated SQL assertions (I1I5) after every test, proving zero duplicate tool calls and zero stranded runs under load.


🛠️ Repository Layout

Anchor/
├── anchor/                      # Python Core SDK & Engine Daemon
│   ├── api/                     # FastAPI Router & Endpoint Definitions
│   ├── core/                    # PostgreSQL Protocol Logic, Fencing & Replay
│   ├── chaos/                   # Automated Chaos Harness & SQL Invariant Asserter
│   └── worker/                  # Worker Claim Loop & Process Lifecycle
├── web/                         # Production Next.js 14 Operator Console UI
├── demo-site/                   # Standalone Interactive Demo & Scaffold Site
├── ops/
│   ├── compose/                 # Production Docker Compose Stack & Dockerfiles
│   └── migrations/              # Alembic DDL Migrations (001_foundation to 006_chaos)
└── pyproject.toml               # Python Package Spec (anchor-runtime)

Architecture

An end-to-end FAANG-level architectural breakdown of the Anchor Durable Execution Engine:

                                    ┌───────────────────────┐
                                    │    Client SDK / API   │
                                    │  (POST /api/runs)     │
                                    └───────────┬───────────┘
                                                │
                                                ▼
                                    ┌───────────────────────┐
                                    │   FastAPI Daemon      │
                                    │   (Stateless Router)  │
                                    └───────────┬───────────┘
                                                │
                                                ▼
  ┌───────────────────────────────────────────────────────────────────────────────────┐
  │                           PostgreSQL 16 Engine Core                               │
  │  ┌───────────────────────┐   ┌───────────────────────┐   ┌─────────────────────┐  │
  │  │  SELECT FOR UPDATE    │   │ Two-Phase Tool        │   │ AN001 Epoch Fencing │  │
  │  │  SKIP LOCKED Queue    │   │ INTENT/RESULT Journal │   │ Monotonic Triggers  │  │
  │  └───────────────────────┘   └───────────────────────┘   └─────────────────────┘  │
  └─────────────────────────────────────▲─────────────────────────────────────────────┘
                                        │
           ┌────────────────────────────┼────────────────────────────┐
           │                            │                            │
  ┌────────┴────────┐          ┌────────┴────────┐          ┌────────┴────────┐
  │ Worker Replica  │          │ Worker Replica  │          │ Worker Replica  │
  │  (Process A)    │          │  (Process B)    │          │  (Process C)    │
  └─────────────────┘          └─────────────────┘          └─────────────────┘

1. Database-Authoritative State Engine (Zero External Broker Tax)

  • Mechanism: Rather than relying on external distributed orchestrators (Temporal, AWS Step Functions) or volatile brokers (Redis, RabbitMQ), Anchor delegates run ownership, sequence allocation, and lease renewals exclusively to PostgreSQL 16.
  • Concurrency: Workers claim unassigned or expired runs using SELECT ... FOR UPDATE SKIP LOCKED CTEs. This guarantees atomicity under high-throughput parallel worker fleets without lock contention or double-claiming.

2. Two-Phase Tool Intent Journaling (TOOL_INTENT $\rightarrow$ TOOL_RESULT)

  • Protocol: Every side-effecting tool invocation (@anchor.tool) undergoes a two-phase transactional commit protocol:
    1. Phase 1 (TOOL_INTENT): Pre-execution intent is written to PostgreSQL with a deterministic, canonical idempotency hash.
    2. Execution: The tool function runs (e.g. calling an external HTTP API or processing a payment).
    3. Phase 2 (TOOL_RESULT): Post-execution result is committed to the journal.
  • SIGKILL Fault Tolerance: If a worker process is terminated by Kubernetes SIGKILL during tool execution, recovery workers inspect the TOOL_INTENT journal ID and execute the declared tool policy (retry_safe, reconcilable, unsafe), preventing duplicate charges or corrupt row mutations.

3. Monotonic Epoch Fencing (AN001_FENCED_WRITE)

  • Constraint Layer: Every lease acquisition or renewal increments the run's monotonic epoch integer token.
  • Zombie Worker Shield: If a worker experiences a long GC pause or network partition, another worker claims the run and increments the epoch. Delayed writes from the stale worker are rejected at the database trigger boundary with AN001_FENCED_WRITE, preventing split-brain state corruption.

4. Continuous Chaos Invariant Audit Suite

An integrated, continuous chaos harness injects hard process terminations (SIGKILL) against active worker nodes and executes 5 automated SQL invariant assertions (I1I5) after every test run:

  • I1 (Idempotency): COUNT(duplicate_side_effects) == 0
  • I2 (Log Monotonicity): Append-only event sequence integrity.
  • I3 (Single Writer): Epoch-fenced single-active-writer guarantee.
  • I4 (Terminal Reachability): All runs reach deterministic terminal states (completed, failed, needs_review).
  • I5 (Replay Determinism): Step replays reconstruct full generator state from journal logs in <5ms.

📄 License & Commercial Rights

Anchor is open-source software licensed under the Apache License 2.0. You are free to use, modify, distribute, and embed Anchor in commercial products without copyleft restrictions.

Author & Creator: Aditya Nema
Connect on LinkedIn: linkedin.com/in/adityaxnema
GitHub Repository: github.com/n43ms/Anchor

Download files

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

Source Distribution

anchor_runtime-1.4.4.tar.gz (670.3 kB view details)

Uploaded Source

Built Distribution

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

anchor_runtime-1.4.4-py3-none-any.whl (218.0 kB view details)

Uploaded Python 3

File details

Details for the file anchor_runtime-1.4.4.tar.gz.

File metadata

  • Download URL: anchor_runtime-1.4.4.tar.gz
  • Upload date:
  • Size: 670.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/7.0.0 CPython/3.12.10

File hashes

Hashes for anchor_runtime-1.4.4.tar.gz
Algorithm Hash digest
SHA256 90c36266fe55d49a2101ff938b78d5b2f335a5d2725ea4a89ca899653b1d038b
MD5 4a33ab8123870c102bed1bfb610045ee
BLAKE2b-256 1bc4ad7f51b45c913288a998a2485794654604d3e7c254ceff0cfa6161f24337

See more details on using hashes here.

File details

Details for the file anchor_runtime-1.4.4-py3-none-any.whl.

File metadata

  • Download URL: anchor_runtime-1.4.4-py3-none-any.whl
  • Upload date:
  • Size: 218.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/7.0.0 CPython/3.12.10

File hashes

Hashes for anchor_runtime-1.4.4-py3-none-any.whl
Algorithm Hash digest
SHA256 f5bd67d1cdeef01f1480c77bd714da797f4f9b7e7399fa77540e7424fb34a68a
MD5 4b5d40e0c1bcd4d2e1fdbd50c9f197e7
BLAKE2b-256 f672505d0c94f2faa6bc08ac7ca532448f2a53ce84500b19c0fe8ba1f8415c5b

See more details on using hashes here.

Release history Release notifications | RSS feed

1.5.6

2 files

1.5.4

2 files

1.5.3

2 files

1.5.2

2 files

1.5.1

2 files

1.5.0

2 files

1.4.8

2 files

1.4.7

2 files

1.4.6

2 files

1.4.5

2 files

This release

1.4.4 This release

2 files

1.4.3

2 files

1.4.2

2 files

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