Integration
Memory for DSPy agents
This page shows how to add Harny memory to a DSPy program so each signed-in user's facts are stored with their dates.
What the framework does not keep
- Which value is current. In Harny, a changed value with a newer timestamp becomes the current value, and the earlier value stays answerable by date.
- When a fact happened. Send a
timestamp(ISO 8601 with zone) with each fact, and recall can answer as of a date withqueryTimestamp. - One person across sources. Harny keeps each person's memory separate, and you name the person with the
X-Harny-Userheader.
The pattern: memory as a tool
Each tool reads or writes one signed-in user's memory through the Memory API.
Your code binds the signed-in user, so the model never chooses whose memory it reads. The app key stays on the server, and each request names the user from your own session.
# dspy.configure(lm=...) has already set your language model
import os
import requests
BASE = "https://api.harny.ai/v1/memory"
def make_memory_tools(user_id: str):
headers = {
"Authorization": f"Bearer {os.environ['HARNY_APP_KEY']}",
"X-Harny-User": user_id, # set by your application, never by the model
}
def recall_memory(question: str) -> str:
"""Look up what is known about this user that is relevant to a question."""
r = requests.post(f"{BASE}/recall", headers=headers,
json={"query": question, "identity": user_id})
return r.text
def store_memory(text: str, happened_at: str) -> str:
"""Save a fact with the ISO time it happened."""
r = requests.post(f"{BASE}/ingest", headers=headers,
json={"content": text, "timestamp": happened_at})
return r.text
return [recall_memory, store_memory]
import dspy
react = dspy.ReAct("question -> answer", tools=make_memory_tools(user_id))
print(react(question="What is the Acme pilot budget?").answer)What recall returns
Recall returns results with sources. Each source has a sourceId, an occurredAt and a label.
POST https://api.harny.ai/v1/memory/ingesttakesid,content,labelandtimestamp.POST https://api.harny.ai/v1/memory/recalltakesqueryandidentity, and returns results withsources.
Example: your agent asks “what is the Acme pilot budget?” and gets this back. Your code hands the model the content and the source label, so it can say who said it and when.
{
"asOf": "2026-07-29T00:00:00Z",
"results": [
{
"rank": 1,
"occurredAt": "2026-07-28T16:00:00Z",
"content": "Acme pilot budget: currently 40k.",
"sources": [
{ "sourceId": "call-8821", "label": "Email from Nadia" }
]
}
]
}When to use which
- Use DSPy's own state for the program's job: its signatures, modules, and the tool calls within a single run.
- Add Harny when facts about a person need to carry dates and stay separate for each user.
Frequently asked questions
Is a separate package needed?
The code on this page calls the Memory API over HTTP with your app key. No SDK is required for it.
Where does the user id come from?
Your app sets it for the signed-in person and sends it in the X-Harny-User header. A user key (fsk_) names its own user and needs no header.
Keep reading
Go on. Get Harny.
The first 500 monthly-active end users are free for good. Create an app key and make your first call in minutes.