Skip to main content

langchain-okf

PyPI version Python 3.10+ OKF v0.2 Compliant License: MIT

Plug-and-play, self-improving memory layer and knowledge graph retriever for LangChain agents, fully conforming to Google's Open Knowledge Format (OKF v0.2).

Delivers sub-second cold loads, search, retrieval, real-time memory writes, and trust verification with both local models (LM Studio, Ollama) and cloud providers (OpenAI, Anthropic, Gemini).


Highlights

  • ⚡ Sub-Second Performance:
    • Cold Index Reload in 7–10 ms (via .okf_index_hash content fingerprinting — bypasses re-embedding).
    • In-Memory Graph Search in 0.02–0.4 ms (instant bidirectional link traversals).
    • Dynamic Agent Memory Writes in 0.69 ms (OKFWriteConceptTool).
    • Trust Verification in 0.78 ms (OKFVerifyConceptTool).
    • Incremental Bundle Re-generation in 5–8 ms (via SHA-256 .okf_cache.json).
  • 🛡️ OKF v0.2 Spec Conformance:
    • Trust Tiers: Automatic derivation: human-reviewed > machine-confirmed > unverified.
    • Provenance Tracking: sources: [{ resource, title, author }] preserved through ingestion.
    • Lifecycle & Freshness: status: stable | draft | deprecated and stale_after automatic eviction.
    • Audit Logging: Root index.md (okf_version: "0.2") and sequential log.md event logs.
  • 🧠 Self-Improving Agent Memory:
    • Agents can read, search, synthesize, write new concepts, and verify knowledge during execution.
  • 📈 Extreme Scalability (1 to 100s of PDFs):
    • Parallel LLM chunk extraction with thread-pool concurrency (max_concurrency).
    • Native PDF ingestion (pypdf, docling, or pymupdf) with lazy loading.
    • Deduplication and prompt optimization to eliminate quadratic prompt bloat.

Benchmark Results (LM Studio Live Server)

Tested on local LM Studio (http://127.0.0.1:1234/v1) using google/gemma-4-12b and text-embedding-nomic-embed-text-v1.5 (768-dim):

Operation Latency Sub-Second? Mechanism
LM Studio Embedding API Call 73.4 ms ✅ YES Direct /v1/embeddings HTTP batch
Cached Bundle Re-generation 8.46 ms ✅ YES SHA-256 chunk cache in .okf_cache.json
Persisted Index Reload 7.94 ms ✅ YES Fingerprint caching via .okf_index_hash (92.9x speedup)
Vector Similarity Search 25.2 ms ✅ YES ChromaDB cosine similarity + graph hop
Graph In-Memory Search 0.026 ms ✅ YES In-memory OKFKnowledgeGraph traversal
Agent Real-Time Memory Write 0.70 ms ✅ YES Atomic file creation + graph update
Agent Trust Tier Upgrade 0.83 ms ✅ YES In-place frontmatter mutation + log.md event

Install

pip install langchain-okf

Install optional integrations as needed:

# Embedding & LLM providers
pip install "langchain-okf[openai]"       # OpenAI / LM Studio / Local OpenAI-compatible
pip install "langchain-okf[ollama]"       # Ollama
pip install "langchain-okf[openrouter]"   # OpenRouter

# Vector stores
pip install "langchain-okf[chroma]"       # Chroma (file-based, default)
pip install "langchain-okf[pgvector]"     # PostgreSQL + pgvector
pip install "langchain-okf[pinecone]"     # Pinecone
pip install "langchain-okf[qdrant]"       # Qdrant

# PDF & Document loaders
pip install pypdf                         # Fast, BSD-licensed PDF ingestion
pip install "langchain-okf[loaders]"      # Docling (MIT) — structured text + tables + OCR

Quick Start

1. Sub-Second Retriever with Local Models (LM Studio or Ollama)

from langchain_openai import OpenAIEmbeddings
from langchain_okf import OKFRetriever

# Connect to LM Studio local server
embeddings = OpenAIEmbeddings(
    base_url="http://127.0.0.1:1234/v1",
    api_key="lm-studio",
    model="text-embedding-nomic-embed-text-v1.5",
)

# Loads in ~7 ms on subsequent runs (skips re-embedding unchanged bundles)
retriever = OKFRetriever.from_bundle(
    "./my_knowledge/",
    embeddings=embeddings,
    persist_directory="./my_knowledge/.chroma",
    min_trust_tier="machine-confirmed",  # Filter out unverified hallucinations
    exclude_stale=True,                  # Automatically skip expired concepts
    expand_depth=1,                      # Traverse markdown cross-links
)

docs = retriever.invoke("What are the core context engineering boundaries?")
for doc in docs:
    print(f"[{doc.metadata.get('trust_tier')}] {doc.metadata.get('title')}")

2. Self-Improving Agent Memory Layer

Equip your agent with the complete OKF v0.2 toolkit, enabling both retrieval and active memory authoring:

from langchain.agents import create_agent
from langchain_openai import ChatOpenAI
from langchain_okf import OKFToolkit

toolkit = OKFToolkit.from_bundle(
    "./my_knowledge/",
    actor="agent/customer-support-v1",
    include_write_tools=True,
)
tools = toolkit.get_tools()

agent = create_agent(
    model=ChatOpenAI(model="gpt-4o", temperature=0),
    tools=tools,
    system_prompt="You are an autonomous agent with persistent OKF memory.",
)

The agent receives 6 specialized tools:

Tool Action Latency Purpose
okf_search Keyword & title search < 0.1 ms Exact lookups across titles, tags, and bodies
okf_read_concept Read full concept < 0.1 ms Inspect full body, metadata, trust tier, and sources
okf_related_concepts Follow graph edges < 0.1 ms Inspect outgoing links and incoming backlinks
okf_list_concepts Filtered browsing < 0.1 ms Filter by type:, tag:, trust:, or status:
okf_write_concept Write new memory 0.69 ms Agent saves learned rules, edge cases, and facts
okf_verify_concept Upgrade trust tier 0.78 ms Mark verified (human-reviewed or machine-confirmed)

3. Generate Knowledge Bundles from PDFs and Documents

Ingest PDFs, markdown, text files, or CSVs and convert them into structured OKF concepts:

from langchain_openai import ChatOpenAI
from langchain_okf import OKFBundleGenerator

generator = OKFBundleGenerator(
    llm=ChatOpenAI(model="gpt-4o", temperature=0),
    output_dir="./my_knowledge/",
    bundle_name="Technical Architecture",
    domain="Microservices, distributed caching, and auth",
    stale_after_days=90,     # OKF v0.2 lifecycle management
    max_concurrency=8,       # Concurrently extract chunks
)

generator.add_sources([
    "architecture_doc.pdf",
    "deployment_runbook.md",
    "service_api.txt",
])

# Cold run extracts concepts; subsequent runs use SHA-256 chunk cache (5-8 ms)
bundle_path = generator.generate()

4. Conformance Audit (OKF v0.2 Spec §11)

Verify bundle health against the official OKF specification:

from langchain_okf import validate_bundle

# Strict mode validates ISO 8601 UTC offsets, actor conventions, and sources
issues = validate_bundle("./my_knowledge/", strict=True)
if not issues:
    print("✅ 100% OKF v0.2 spec compliant")
else:
    for issue in issues:
        print(f"⚠️ {issue}")

Scalability: Handling 20 to 100s of PDFs

langchain-okf is engineered from the ground up for high-throughput multi-document ingestion:

[20–100+ PDFs] ──▶ Direct pypdf Stream (1–6s) ──▶ SHA-256 Chunk Hash Check
                                                        │
                      ┌─────────────────────────────────┴─────────────────────────────────┐
                      ▼                                                                   ▼
             Uncached Chunks (New)                                               Cached Chunks (Unchanged)
          Multi-Worker Thread Pool                                              Instant In-Memory Load (< 10 ms)
        (Cloud LLM / local vLLM / LM Studio)
                      │                                                                   │
                      └─────────────────────────────────┬─────────────────────────────────┘
                                                        ▼
                                         OKF v0.2 Markdown Knowledge Bundle
                                                        │
                                                        ▼
                                   .okf_manifest.json + .okf_index_hash
                                                        │
                      ┌─────────────────────────────────┴─────────────────────────────────┐
                      ▼                                                                   ▼
             Initial Build (Cold)                                                Incremental Sync (Hot)
        Batch Embeddings + ChromaDB                                         Embeds ONLY Delta ΔN Concepts
           O(N log N) or O(N/P)                                             O(ΔN log N) -> O(1) for single additions
  1. Streaming PDF Extraction:
    • Uses zero-overhead pypdf stream processing to extract text and pages in memory.
    • Extracts ~20 PDFs (200 pages) in 1.2s and ~100 PDFs (1,000 pages) in ~6s.
  2. $O(1)$ Hash Caching (.okf_cache.json):
    • Each text chunk is hashed with SHA-256.
    • If 100 PDFs are ingested and only 2 are updated, only the modified chunks call the LLM. The other 98 PDFs load from cache in milliseconds.
  3. Multi-Worker Concurrency (max_concurrency):
    • Extraction runs on a configurable ThreadPoolExecutor.
    • With cloud models or local vLLM / high-throughput endpoints, extraction scales linearly across available workers.
  4. $O(\Delta N)$ Incremental Vector Indexing:
    • Tracks a per-concept content manifest (.okf_manifest.json).
    • When a bundle is updated with new or modified documents, OKFRetriever detects the exact diff and embeds/indexes only the changed concepts ($\Delta N$) instead of rebuilding the whole index.
    • For adding a single concept or document, time complexity drops from $O(N \log N)$ to $O(1)$ (~15–20 ms).
  5. Persistent Fingerprint Reload (.okf_index_hash):
    • For unchanged bundles, OKFRetriever reattaches to the persisted vector store in 7–15 ms ($O(1)$) regardless of whether the bundle contains 10 or 10,000 concepts.
  6. Memory Footprint:
    • In-memory graph of 10,000 concepts uses less than 25 MB of RAM, with sub-millisecond Dijkstra and BFS hop lookups.

Dynamic Benchmark Tool

A built-in benchmark harness lets you evaluate ingestion and retrieval performance on your own files:

# Benchmark your own PDFs with LM Studio
uv run python benchmarks/run_benchmark_lmstudio.py paper1.pdf paper2.pdf

# Benchmark an entire directory
uv run python benchmarks/run_benchmark_lmstudio.py ./my_pdf_folder/

# Run with full live LLM extraction
uv run python benchmarks/run_benchmark_lmstudio.py report.pdf --live-llm

Components

Component Type Description
OKFBundleGenerator Generator Multi-format ingestion (PDF, MD, TXT, CSV) with SHA-256 caching and concurrency
OKFBundleLoader Document Loader Generator-based lazy loading (lazy_load) with trust and staleness filters
OKFRetriever Retriever Hybrid vector similarity + cross-link expansion + persistent disk caching
OKFKnowledgeGraph Graph Engine Directed graph for sub-millisecond edge navigation and backlinks
OKFToolkit Agent Toolkit 6 tools for search, graph navigation, atomic writes, and trust verification
validate_bundle Validator Strict validator auditing OKF v0.2 schema, actors, ISO 8601 dates, and sources

Development & Testing

To run tests and linters locally:

uv sync
uv run pytest tests/ -v
uv run ruff check langchain_okf/
uv run mypy langchain_okf/

License

MIT © Ayush Dey

Metadata

Release files for langchain-okf 0.2.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for langchain-okf 0.2.0
File Size Uploaded Provenance Actions
langchain_okf-0.2.0.tar.gz 63.8 kB With provenance Details for langchain_okf-0.2.0.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for langchain-okf 0.2.0
File Interpreter ABI Platform Actions
langchain_okf-0.2.0-py3-none-any.whl Python 3 none any Details for langchain_okf-0.2.0-py3-none-any.whl

Total release size: 112.8 kB

Release files / langchain_okf-0.2.0.tar.gz

Download URL langchain_okf-0.2.0.tar.gz
Size 63.8 kB
Tags Source
SHA-256 checksum
How to use checksums
83dfc73c6a0f41af7fdbfab87ed287c3b1f60cbbd1979c7db9a60c064181dc81
BLAKE2b-256 checksum
How to use checksums
397a9f300ffdb420ffc731af5fb61b7fe8d1f798e0e5a92e1a5262db3e66fb99
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Nov 15, 2026.

Transparency log

Release files / langchain_okf-0.2.0-py3-none-any.whl

Download URL langchain_okf-0.2.0-py3-none-any.whl
Size 49.0 kB
Tags Python 3
SHA-256 checksum
How to use checksums
9b6721e1c0181f39744f32fcf9ae5d6c59ec355fceaa5ec7c3516a4def376009
BLAKE2b-256 checksum
How to use checksums
78b83c610224b3758fc166a5327e97a659394a03b8cf3f16b39b6ff1bde1fc75
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Nov 15, 2026.

Transparency log

Security for langchain-okf 0.2.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

Full provenance

Both files in this release have been uploaded with provenance metadata.

Select from 2 files

Attestation for langchain_okf-0.2.0.tar.gz

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Nov 15, 2026.

Transparency log

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page