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.
Author & System Architect: Aditya Nema — linkedin.com/in/adityaxnema • GitHub Repository
🚀 Quickstart (Under 60 Seconds)
No API keys, no cloud subscriptions, no external services required to get started.
1. Install the Python SDK
pip install anchor-runtime
2. Boot Local Cluster & UI
# 🌟 Recommended (1-Click Launch & Auto-Browser):
anchor dev # On Windows if PATH is not configured: python -m anchor.cli dev
Scaffolds workspace, boots PostgreSQL 16, Redis 7, API Server (
:8000), 3 worker replicas, and auto-opens the Operator Console UI athttp://localhost:3000.Alternative manual boot:
anchor init(Windows:python -m anchor.cli init) followed bydocker compose up -d.
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_INTENTsequence 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 theneeds_reviewqueue 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 (I1–I5) 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 LOCKEDCTEs. 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:- Phase 1 (
TOOL_INTENT): Pre-execution intent is written to PostgreSQL with a deterministic, canonical idempotency hash. - Execution: The tool function runs (e.g. calling an external HTTP API or processing a payment).
- Phase 2 (
TOOL_RESULT): Post-execution result is committed to the journal.
- Phase 1 (
- SIGKILL Fault Tolerance: If a worker process is terminated by Kubernetes
SIGKILLduring tool execution, recovery workers inspect theTOOL_INTENTjournal 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
epochinteger 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 (I1–I5) after every test run:
I1(Idempotency):COUNT(duplicate_side_effects) == 0I2(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
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 anchor_runtime-1.5.3.tar.gz.
File metadata
- Download URL: anchor_runtime-1.5.3.tar.gz
- Upload date:
- Size: 674.0 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via:
twine/7.0.0 CPython/3.12.10
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
1e0d5efc60adbda900d755a6fce488704748282acadca90ad8a0f027b0a2324d
|
|
| MD5 |
555b47acb30ca7f0889d218ded90cfae
|
|
| BLAKE2b-256 |
886674f6469b296b3577e9cca66e53b621e72e58669f22b0bd73f47662cfb444
|
File details
Details for the file anchor_runtime-1.5.3-py3-none-any.whl.
File metadata
- Download URL: anchor_runtime-1.5.3-py3-none-any.whl
- Upload date:
- Size: 220.5 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via:
twine/7.0.0 CPython/3.12.10
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
821884e4023f96638afbc300e8ca980d680d11e0a2575a27ea28a835d4babf5a
|
|
| MD5 |
14769bb37a5b5a1146dc8678b9d54b27
|
|
| BLAKE2b-256 |
a5960bd909580c87437f666afacf8ed8c41e6ede4b40b79f3fc5ff7b7290943d
|