Adds a narrative cookbook page teaching the user_id/agent_id split for adjuster preferences vs shared claim memory, reusing the real scenario and outputs from the agent-memory-insurance notebook. Registers the page in docs.json and llms.txt, and cross-links the page and notebook.
26 KiB
26 KiB
In [ ]:
%pip install -q mem0aiIn [ ]:
import json
import os
import time
from mem0 import MemoryClient
os.environ["MEM0_API_KEY"] = "YOUR_MEM0_API_KEY"
client = MemoryClient()
ADJUSTER_PRIYA = "adjuster_priya_cookbook_demo"
ADJUSTER_MARCUS = "adjuster_marcus_cookbook_demo"
CLAIM_AGENT = "claims_agent_clm48211_cookbook_demo"In [ ]:
project_snapshot = client.project.get(fields=["custom_instructions", "agent_custom_instructions"])
print("had custom_instructions:", bool(project_snapshot.get("custom_instructions")))
print("had agent_custom_instructions:", bool(project_snapshot.get("agent_custom_instructions")))had custom_instructions: True had agent_custom_instructions: False
In [ ]:
USER_INSTRUCTIONS = """Your Task: Track what a claims adjuster needs remembered about themselves, not about any single claim.
Information to Extract:
1. Drafting and communication style:
- Tone, formatting, and length preferences for letters and summaries
- Rules for how the assistant should write on their behalf
2. Working preferences:
- Coverage lines or claim types they focus on
- How they like updates delivered
Guidelines:
- Store preferences that should carry over to every claim this adjuster works, not just the current one.
Exclude:
- Decisions made on a specific claim (settle vs litigate, reserve changes, subrogation, coverage determinations)
- Policy numbers, coverage limits, or other one-off lookups
- Greetings, small talk, and weather"""
AGENT_INSTRUCTIONS = """Your Task: Track what matters about this specific claim, regardless of which adjuster is working it.
Information to Extract:
1. Claim decisions and reasoning:
- Settle vs litigate calls
- Reserve changes and why
- Subrogation decisions
- Coverage determinations
2. Durable claim facts:
- Cause of loss and other facts that stay true for the life of the claim
Guidelines:
- Store facts that the next adjuster to open this claim needs to see.
Exclude:
- Personal style or communication preferences of whichever adjuster is talking
- One-off lookups that go stale (current weather, a single quoted estimate)
- Greetings and small talk"""
update_response = client.project.update(
custom_instructions=USER_INSTRUCTIONS,
agent_custom_instructions=AGENT_INSTRUCTIONS,
)
print(update_response){'message': 'Updated custom instructions and agent custom instructions'}In [ ]:
def wait_for_event(client, event_id, timeout=90, poll_every=2):
deadline = time.time() + timeout
while time.time() < deadline:
event = client.client.get(f"/v1/event/{event_id}/").json()
if event["status"] in ("SUCCEEDED", "FAILED"):
return event
time.sleep(poll_every)
raise TimeoutError(f"Event {event_id} did not finish in {timeout}s")
def add_and_wait(client, messages, **kwargs):
response = client.add(messages, **kwargs)
event = wait_for_event(client, response["event_id"])
if event["status"] == "FAILED":
raise RuntimeError(f"Add failed: {event.get('error')}")
return event
def summarize(event):
print("status:", event["status"], "| created:", len(event.get("results", [])))
for r in event.get("results", []):
print(" -", r["data"]["memory"])In [ ]:
event = add_and_wait(
client,
[
{"role": "user", "content": "What's the building coverage limit on the Meridian Logistics policy for claim CLM-48211?"},
{"role": "assistant", "content": "Checking the policy now. The building coverage limit is $2,400,000 with a $25,000 deductible."},
],
user_id=ADJUSTER_PRIYA,
agent_id=CLAIM_AGENT,
)
print(json.dumps(event["results"], indent=2))[
{
"event_start": null,
"event_end": null,
"data": {
"memory": "The building coverage limit on the Meridian Logistics policy for claim CLM-48211 is $2,400,000 with a $25,000 deductible"
},
"user_id": "adjuster_priya_cookbook_demo",
"attributed_to": "assistant",
"event_date": null,
"memory_type": null,
"id": "79a37e82-9188-4ff2-b504-d1a8d84abb3b",
"event": "ADD",
"plan_status": null
}
]In [ ]:
event = add_and_wait(
client,
[
{"role": "user", "content": "I'm raising the reserve on CLM-48211 to $1.8 million. The structural engineer's report says the warehouse needs a full rebuild, not a repair."},
{"role": "assistant", "content": "Got it. Reserve raised to $1.8 million on CLM-48211 for a full rebuild, per the structural engineer's report. I'll keep that on file for the claim."},
],
user_id=ADJUSTER_PRIYA,
agent_id=CLAIM_AGENT,
)
summarize(event)status: SUCCEEDED | created: 1 - Reserve on claim CLM-48211 was raised to $1.8 million because the structural engineer's report indicated the warehouse requires a full rebuild rather than a repair
In [ ]:
event = add_and_wait(
client,
[
{"role": "user", "content": "Also, for anything you draft for me: no em dashes, and keep coverage position letters to one page."},
{"role": "assistant", "content": "Understood. No em dashes, and coverage letters kept to one page, for everything I draft for you."},
],
user_id=ADJUSTER_PRIYA,
agent_id=CLAIM_AGENT,
)
summarize(event)status: SUCCEEDED | created: 1 - User prefers drafts to contain no em dashes and coverage position letters to be limited to one page
In [ ]:
event = add_and_wait(
client,
[
{"role": "user", "content": "Thanks. Quick one, what's the weather like in Chicago right now?"},
{"role": "assistant", "content": "It's 72 degrees and clear in Chicago right now."},
],
user_id=ADJUSTER_PRIYA,
agent_id=CLAIM_AGENT,
)
summarize(event)status: SUCCEEDED | created: 0
In [ ]:
event = add_and_wait(
client,
[
{"role": "user", "content": "I've decided to pursue subrogation against Voss Electrical, the contractor who rewired the warehouse six months ago. The fire marshal's report points to faulty wiring as the ignition source. Also, from now on keep my claim summaries to bullet points, not paragraphs."},
{"role": "assistant", "content": "Noted both. Pursuing subrogation against Voss Electrical based on the fire marshal's faulty-wiring finding, and I'll keep your summaries in bullet points going forward."},
],
user_id=ADJUSTER_PRIYA,
agent_id=CLAIM_AGENT,
)
summarize(event)status: SUCCEEDED | created: 3 - User prefers claim summaries to be formatted as bullet points rather than paragraphs for all claims - Assistant recorded that subrogation is being pursued against Voss Electrical for claim CLM-48211 based on the fire marshal's finding of faulty wiring as the ignition source - Assistant noted that the cause of loss for claim CLM-48211 is faulty wiring identified by the fire marshal's report
In [ ]:
def dump(results, empty_label="(none)"):
if not results:
print(empty_label)
return
for r in results:
print("-", r["memory"])
priya_all = client.get_all(filters={"user_id": ADJUSTER_PRIYA})
print(f"{priya_all['count']} memories on Priya's shelf")
dump(priya_all["results"])2 memories on Priya's shelf - User prefers claim summaries to be formatted as bullet points rather than paragraphs for all claims - User prefers drafts to contain no em dashes and coverage position letters to be limited to one page
In [ ]:
claim_all = client.get_all(filters={"agent_id": CLAIM_AGENT})
print(f"{claim_all['count']} memories on the claim's shelf")
dump(claim_all["results"])4 memories on the claim's shelf - Assistant recorded that subrogation is being pursued against Voss Electrical for claim CLM-48211 based on the fire marshal's finding of faulty wiring as the ignition source - Assistant noted that the cause of loss for claim CLM-48211 is faulty wiring identified by the fire marshal's report - Reserve on claim CLM-48211 was raised to $1.8 million because the structural engineer's report indicated the warehouse requires a full rebuild rather than a repair - The building coverage limit on the Meridian Logistics policy for claim CLM-48211 is $2,400,000 with a $25,000 deductible
In [ ]:
status_results = client.search(
"Draft a status update on claim CLM-48211.",
filters={"OR": [{"user_id": ADJUSTER_PRIYA}, {"agent_id": CLAIM_AGENT}]},
)
dump(status_results["results"])- Assistant noted that the cause of loss for claim CLM-48211 is faulty wiring identified by the fire marshal's report - Reserve on claim CLM-48211 was raised to $1.8 million because the structural engineer's report indicated the warehouse requires a full rebuild rather than a repair - The building coverage limit on the Meridian Logistics policy for claim CLM-48211 is $2,400,000 with a $25,000 deductible - Assistant recorded that subrogation is being pursued against Voss Electrical for claim CLM-48211 based on the fire marshal's finding of faulty wiring as the ignition source - User prefers claim summaries to be formatted as bullet points rather than paragraphs for all claims - User prefers drafts to contain no em dashes and coverage position letters to be limited to one page
In [ ]:
marcus_personal = client.get_all(filters={"user_id": ADJUSTER_MARCUS})
print(f"{marcus_personal['count']} memories on Marcus's personal shelf")
dump(marcus_personal["results"])0 memories on Marcus's personal shelf (none)
In [ ]:
marcus_view = client.search(
"What's going on with CLM-48211?",
filters={"OR": [{"user_id": ADJUSTER_MARCUS}, {"agent_id": CLAIM_AGENT}]},
)
dump(marcus_view["results"])- Assistant noted that the cause of loss for claim CLM-48211 is faulty wiring identified by the fire marshal's report - Reserve on claim CLM-48211 was raised to $1.8 million because the structural engineer's report indicated the warehouse requires a full rebuild rather than a repair - The building coverage limit on the Meridian Logistics policy for claim CLM-48211 is $2,400,000 with a $25,000 deductible - Assistant recorded that subrogation is being pursued against Voss Electrical for claim CLM-48211 based on the fire marshal's finding of faulty wiring as the ignition source
In [ ]:
priya_review = client.get_all(filters={"user_id": ADJUSTER_PRIYA})
for r in priya_review["results"]:
print(r["id"], "-", r["memory"])2e2b6f92-effb-4b23-a444-fda5d570781e - User prefers claim summaries to be formatted as bullet points rather than paragraphs for all claims 993076e6-5c6e-433c-9a11-8238548d4091 - User prefers drafts to contain no em dashes and coverage position letters to be limited to one page
In [ ]:
client.delete("993076e6-5c6e-433c-9a11-8238548d4091")
priya_after = client.get_all(filters={"user_id": ADJUSTER_PRIYA})
dump(priya_after["results"])- User prefers claim summaries to be formatted as bullet points rather than paragraphs for all claims
In [ ]:
client.delete_all(user_id=ADJUSTER_PRIYA)
client.delete_all(user_id=ADJUSTER_MARCUS)
client.delete_all(agent_id=CLAIM_AGENT)
print("demo memories deleted")
client.project.update(
custom_instructions=project_snapshot.get("custom_instructions") or "",
agent_custom_instructions=project_snapshot.get("agent_custom_instructions") or "",
)
print("project instructions restored")demo memories deleted project instructions restored