Skip to main content

langchain-harny

Long-term memory for LangChain agents. HarnyRetriever, two agent tools and a LangChain v1 agent middleware recall what each person told your app, with the date each fact happened, and keep the newest answer on top when a fact changed. Your agent remembers across sessions, restarts and devices.

Install

pip install langchain-harny                # retriever and tools
pip install "langchain-harny[agents]"      # adds HarnyMemoryMiddleware for create_agent

Python 3.10+. Needs langchain-core>=1,<2 (tested against 1.7.1; the agents extra is tested against langchain 1.4.7).

Start without a key

You do not need an account first. From the project folder:

pip install harny
harny init --name "<business>" --about "<what the app does>" --customers "<who its customers are>" --preset cost_first

That creates the app and saves HARNY_API_KEY to .env without printing it. Never print, log or commit the key. (Raw API route: POST https://api.harny.ai/v1/auth/provision with {"app_name": "<business>"}; the key is shown once.)

One-minute example

from langchain_harny import HarnyRetriever

retriever = HarnyRetriever(user="nadia", k=5)  # reads HARNY_API_KEY; "nadia" is YOUR id for the person
for doc in retriever.invoke("What is the Acme pilot budget?"):
    print(doc.metadata["occurred_at"], doc.page_content)

user is your own id for the person. The API reads only that person's memory, so one app key can serve every user safely.

The canonical example, with LangChain

Same facts as every Harny surface (Nadia, the Acme pilot, the budget that moved from 32k to 40k):

from langchain_harny import HarnyMemoryClient, HarnyRetriever

with HarnyMemoryClient(user="nadia") as harny:  # reads HARNY_API_KEY
    harny.ingest(
        "Nadia confirmed the Acme pilot ships March 14. Budget is 32k.",
        id="call-8820", label="Account review call", timestamp="2026-02-03T09:00:00Z",
    )
    receipt = harny.ingest(
        "Nadia raised the Acme pilot budget from 32k to 40k.",
        id="call-8821", label="Budget change", timestamp="2026-03-01T09:00:00Z",
    )
    harny.wait_for_ingestion(receipt["ingestionId"])  # facts are processed in the background

docs = HarnyRetriever(user="nadia", level="medium").invoke("What is the Acme pilot budget?")
for rank, doc in enumerate(docs, start=1):
    print(rank, doc.page_content)  # "Acme pilot budget: currently 40k." comes first

Give an agent memory (three ways)

1. Middleware: recall before every answer. LangChain v1 agents (saving conversation messages is off by default; switch it on with HarnyMemoryMiddleware(user=..., save_user_messages=True)):

from langchain.agents import create_agent
from langchain_harny import HarnyMemoryMiddleware

agent = create_agent("openai:gpt-5", middleware=[HarnyMemoryMiddleware(user="nadia")])
agent.invoke({"messages": [{"role": "user", "content": "What is the Acme pilot budget?"}]})

For many people from one agent, pass a function: HarnyMemoryMiddleware(user=lambda runtime: runtime.context.user_id).

2. Tools: the agent decides when to look up and when to save.

from langchain.agents import create_agent
from langchain_harny import HarnyToolkit

tools = HarnyToolkit(user="nadia").get_tools()  # harny_recall and harny_remember
agent = create_agent("openai:gpt-5", tools=tools)

3. Retriever: plug memory into any chain or RAG graph (see the one-minute example). It is a normal BaseRetriever, so retriever | prompt | model works.

Options

Argument Default Meaning
user required Your id for the person
api_key HARNY_API_KEY App key (fsa_) or a person's own key (fsk_); text or SecretStr
base_url https://api.harny.ai /v1 is added when missing. HTTPS only; http://localhost allowed for local runs
level / max_tokens medium How much evidence to recall (low, medium, high, extra-high, or a token budget)
k (retriever) / limit (middleware) API default Most facts recalled (1 to 200)
as_of, occurred_from, occurred_to (retriever) none Recall as of a moment, or only facts from a time window
save_user_messages (middleware) False Off by default. True saves what the person says after each run
cache_seconds (middleware) 30 Reuse a recall for the same words, so a tool loop does not call the API at every step
http_client / async_http_client new client Your own httpx client (proxy, test transport); never closed by the package

Behaviour an agent should know

  • Saving conversation messages is off by default (save_user_messages=True switches it on). When on, only the person's own messages are saved, never the model's replies or system text. harny_remember and HarnyMemoryClient.ingest save on request.
  • A saved fact has a stable id (hash of the person and the words), so saying the same words twice is one fact and costs nothing the second time.
  • A memory problem never fails the run. If recall or save is refused, the middleware lets the agent answer without memory and logs one warning with the error code (never content, never keys). The tools answer in words ("Memory is unavailable right now (code)") instead of raising.
  • pending: facts saved a moment ago may still be processing. Retriever documents carry it in metadata["pending"]; the middleware and the tool tell the model in one line.
  • Errors are HarnyAPIError with code, status, debug_message, retry_after (a per-minute rate limit has no body; code is rate-limited).
  • Forget a fact: HarnyMemoryClient(user="nadia").delete_data_points(["call-8821"]).
  • Everything has an async twin: ainvoke on the retriever and tools, AsyncHarnyMemoryClient, and the middleware's async hooks.

The memory API underneath

This package is a thin layer over Harny's public memory API (/v1/memory: ingest, status, recall, delete). The same calls from Python with the Harny SDK:

from harny import SyncHarnyClient

with SyncHarnyClient() as harny:  # reads HARNY_API_KEY
    harny.ingest_data_point(
        "Nadia confirmed the Acme pilot ships March 14. Budget is 32k.",
        id="call-8820", label="Account review call", timestamp="2026-02-03T09:00:00Z", user="nadia",
    )
    receipt = harny.ingest_data_point(
        "Nadia raised the Acme pilot budget from 32k to 40k.",
        id="call-8821", label="Budget change", timestamp="2026-03-01T09:00:00Z", user="nadia",
    )
    harny.wait_for_ingestion(receipt.ingestionId, user="nadia")

    recall = harny.recall("What is the Acme pilot budget?", level="medium", user="nadia")
    for result in recall.results:
        print(result.rank, result.content)

Tests and pinned versions

scripts/ci.sh            # fresh venv, installs the pins in ci-pins.txt, runs the tests (no network, no paid call)
scripts/ci.sh --latest   # the same tests against the newest LangChain release (exit 3 = a LangChain update breaks us)

Pinned: langchain-core==1.7.1, langchain==1.4.7, langchain-tests==1.1.9, httpx==0.28.1. The tests include LangChain's own standard suites for retrievers and tools.

Apache-2.0. https://harny.ai

Metadata

Release files for langchain-harny 0.1.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-harny 0.1.0
File Size Uploaded Provenance Actions
langchain_harny-0.1.0.tar.gz 27.5 kB Without provenance Details for langchain_harny-0.1.0.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for langchain-harny 0.1.0
File Interpreter ABI Platform Actions
langchain_harny-0.1.0-py3-none-any.whl Python 3 none any Details for langchain_harny-0.1.0-py3-none-any.whl

Total release size: 48.2 kB

Release files / langchain_harny-0.1.0.tar.gz

Download URL langchain_harny-0.1.0.tar.gz
Size 27.5 kB
Tags Source
SHA-256 checksum
How to use checksums
a402955de41acd4f8c4013ba48209726f7723926ecc9dc7d6ba3db2e8980bde9
BLAKE2b-256 checksum
How to use checksums
99c3d7046b0211e275320117a5ad5a8388fb8ebe6f29b760ed9676cce7d54dfe
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.11.15

Release files / langchain_harny-0.1.0-py3-none-any.whl

Download URL langchain_harny-0.1.0-py3-none-any.whl
Size 20.7 kB
Tags Python 3
SHA-256 checksum
How to use checksums
414715cbaa3e1c10d95e9fcdc6371c6c2794ff9329685a881ba8dc461367d77f
BLAKE2b-256 checksum
How to use checksums
d4f9835a728f495e4816f723fe333bc580fc224e5876b52cd5d43143dd03f9ef
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.11.15

Security for langchain-harny 0.1.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.

No provenance found

None of the files in this release were uploaded with provenance metadata.

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.

Release history Release notifications | RSS feed

This release

0.1.0 This release

2 release files No provenance

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