quickstart

Quick Start

Write your first Agent in 5 minutes

Welcome to Klock. This guide will walk you through preventing the Multi-Agent Race Condition (MARC) in a real application.

1. Install the package

Install the Klock core and the LangChain adapter if you are using LangChain.

pip install klock klock-langchain

2. Configure the Klock Agent

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

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)

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: str, content: str):
        with open(path, "w") as handle:
            handle.write(content)

3. Test Your Agent

Run multiple agents pointing at the same file simultaneously. You will notice the Wait-Die logs in your terminal preventing the data loss.

The Wait-Die scheduling ensures that older agents wait, while younger agents gracefully die and retry to prevent deadlocks.

What just happened?

Our framework evaluated the resulting response:

  • Wait-Die: Safely paused the conflicting agent.
  • Integrity preserved: All intended codes were written without loss.
  • Zero corrupted files: In your codebase.

For more advanced configurations of Klock, read the Architecture Overview.