integrations

LangChain Integration

Add Wait-Die concurrency to your LangChain swarms in one line of code.

LangChain is one of the fastest ways to hit the Multi-Agent Race Condition (MARC). The klock-langchain adapter wraps your existing LangChain tools so Klock can serialize conflicting writes with Wait-Die scheduling.

The @klock_protected Decorator

The simplest way to use Klock is to decorate the _run method of your custom BaseTool.

pip install klock klock-langchain langchain-core
from klock import KlockClient
from klock_langchain import klock_protected
from langchain_core.tools import BaseTool

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

class RefactorFileTool(BaseTool):
    name = "refactor_file"
    description = "Modifies existing source code files."

    @klock_protected(
        klock_client=klock_client,
        agent_id="langchain-bot-01",
        session_id="session-123",
        resource_type="FILE",
        resource_path_extractor=lambda kwargs: kwargs["path"],
        predicate="MUTATES",
    )
    def _run(self, path: str, content: str) -> str:
        with open(path, 'w') as f:
            f.write(content)
        return "Success"

How It Works

Under the hood, @klock_protected is a retry-aware loop that executes the KLIS protocol:

  1. Translates the tool call into an acquire_lease(MUTATES) request.
  2. If blocked by a younger agent, the older agent waits and retries after Klock's suggested delay.
  3. If blocked by an older agent, the younger agent gets a DIE signal and should retry later.
  4. Once the lease is granted, your _run method is invoked.
  5. On return, a .finally() block releases the lease automatically.

This is a tool-level integration that works today. Dedicated LangGraph and CrewAI adapters can come later without changing this surface.