Klock Protocol v1.0

The Mutex for the Agent Economy

When LangChain or CrewAI agents race to modify the same files, they corrupt each other's work—silently. Klock is the coordination kernel that stops it.

pip install klock-langchain

The Problem

The Multi-Agent
Race Condition

When two AI agents modify the same file, the last one to save wins— and your code breaks. We call this the Multi-Agent Race Condition (MARC).

MARC is worse than ordinary data races because the build still passes. You get syntactically valid but semantically incomplete code—with no error signal.

24%
of intended code changes silently lost in our real-world study with 5 Claude Code agents on a shared Express codebase.Build status: passing.

Common Failure Modes

Lost Updates

Agent A's changes overwritten by Agent B. Silent data loss.

Duplicate Artifacts

Rename/move operations conflict, causing repository corruption.

Livelock

Retry storms under contention. 0% forward progress.

Corrupted State

Interleaved partial writes cause build/deploy failures.

Research Results

Proven in the Lab
and in the Wild

Klock achieves near-pessimistic safety while preserving parallelism. These are real numbers from our research paper and implementation.

Simulation: 5 Agents, 70% Contention+ dynamic scope
ProtocolSuccessAbortRetries
Pessimistic100%0%0.0
Klock
100%0%1.7
Optimistic84.8%15.2%3.1
Chaos16.4%83.6%
Real-World: 5 Claude Code AgentsExpress codebase
MetricSolo baselineConcurrentLoss
Lines changed3,0272,311−716
Auth routes
100%0%100% lost
Validation schemas12 of 121 of 12−92%
Build status Pass Pass
The build passed. No error signal. 24% of work silently lost.
This is why Klock exists.

The Klock Protocol

Three Steps to
Safe Concurrency

STEP 01

Declare Intent

Before touching any file, agents declare what they intend to modify. Klock's conflict matrix detects collisions in O(1).

ACQUIRE /src/auth.ts [WRITE]
STEP 02

Acquire Lease

Klock grants an exclusive time-bounded lease using Wait-Die scheduling. Deadlocks are mathematically impossible.

VERDICT: GRANTED (lease_id: a4f2)
STEP 03

Execute Safely

The agent works within its declared scope. On completion, the lease releases and waiting agents proceed.

RELEASE a4f2 → next agent unblocked

One decorator. Full protection.

The @klock_protected decorator wraps any LangChain tool with Wait-Die concurrency control. No architecture changes required.

tools.py
# klock-langchain integration
from klock import KlockClient
from klock_langchain import klock_protected
from langchain_core.tools import BaseTool

klock_client = KlockClient()
klock_client.register_agent("refactor-bot-01", 100)

# Wrap any tool with Wait-Die control
class WriteFileTool(BaseTool):
    name = "write_file"

    @klock_protected(
        klock_client=klock_client,
        agent_id="refactor-bot-01",
        session_id="session-001",
        resource_type="FILE",
        resource_path_extractor=lambda kwargs: kwargs["path"],
        predicate="MUTATES"
    )
    def _run(self, path, content):
        # Exclusive lease guaranteed
        with open(path, "w") as handle:
            handle.write(content)

Integrations

Works where you work.

LangChain ships today. LangGraph and CrewAI can already use the same protection at the tool layer while dedicated adapters stay on the roadmap.

LangChain
klock-langchain
Available
LangGraph
use klock-langchain
Tool-level today
CrewAI
use klock-langchain
Tool-level today
AutoGPT
klock-autogpt
Coming soon
OpenDevin
klock-opendevin
Planned
Native Rust
cargo add klock-core
Available

Ready to stop agent collisions?

Install the open-source kernel in 30 seconds. Coordinate your first multi-agent system today.