Skip to main content

LangChain integration for Klock

Project description

klock-langchain

PyPI version License: MIT

Official LangChain integration for Klock, the coordination infrastructure that prevents Multi-Agent Race Conditions (MARC).

This package provides the @klock_protected decorator, allowing you to wrap any LangChain BaseTool with Klock's Wait-Die concurrency control. This ensures that when multiple autonomous agents try to modify the same resource simultaneously, they do not corrupt your data or cause silent data loss.

Installation

pip install klock-langchain klock langchain-core

Quick Start

Wrap your LangChain tools to enforce intent-based concurrency control before they execute:

from langchain_core.tools import BaseTool
from klock import KlockClient
from klock_langchain import klock_protected

# Initialize your Klock kernel client
klock_client = KlockClient("http://localhost:8080")

# Define a tool and protect it with Klock
class WriteFileTool(BaseTool):
    name = "write_file"
    description = "Writes content to a file on disk"
    
    # Protect the _run method with Wait-Die concurrency control
    @klock_protected(
        klock_client=klock_client,
        agent_id="refactor-agent-1",
        session_id="session-123",
        resource_type="FILE",
        resource_path_extractor=lambda kwargs: kwargs.get("filepath"),
        predicate="MUTATES"
    )
    def _run(self, filepath: str, content: str) -> str:
        with open(filepath, 'w') as f:
            f.write(content)
        return f"Successfully wrote to {filepath}"

How It Works

Klock uses Wait-Die priority scheduling, a classic database concurrency control algorithm, mapped specifically to LLM agents:

  • Older agents wait for younger agents to finish (Wait).
  • Younger agents abort immediately to prevent deadlocks (Die).

If a "Die" abort occurs, klock_protected raises a RuntimeError. LangChain's built-in error handling catches this and returns it to the LLM agent, allowing the agent to gracefully pause and retry the operation later.

License

MIT License

Project details


Download files

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

Source Distribution

klock_langchain-0.1.0.tar.gz (3.3 kB view details)

Uploaded Source

Built Distribution

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

klock_langchain-0.1.0-py3-none-any.whl (3.8 kB view details)

Uploaded Python 3

File details

Details for the file klock_langchain-0.1.0.tar.gz.

File metadata

  • Download URL: klock_langchain-0.1.0.tar.gz
  • Upload date:
  • Size: 3.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.14.3

File hashes

Hashes for klock_langchain-0.1.0.tar.gz
Algorithm Hash digest
SHA256 a4547ea56684cd5247d30c3bf437754a9291ed7fbb5508156ca7c111001a1c3e
MD5 1bbc3932f84bda24a4db96bb47d4161b
BLAKE2b-256 659019b4a7301950ef73ed0128e0209845645e7579c926fca8585aced76b4084

See more details on using hashes here.

File details

Details for the file klock_langchain-0.1.0-py3-none-any.whl.

File metadata

File hashes

Hashes for klock_langchain-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 5b2cdd44e166945c72843e5dedff3bc3b8ab12534ac8d7a7221007ef46d13464
MD5 27a318291f1bd3c5cbb344e1dded2d40
BLAKE2b-256 e8221d608d68d0f5e0314f636cc427c29322a358d69dc38f79acb74c3aaf9209

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page