Last updated

LangGraph Integration

Add Papr memory retrieval and writeback as graph nodes so long-running LangGraph flows retain durable context.

What You Build

  • A retrieval node that loads relevant Papr memory
  • A writeback node that persists final outputs
  • Graph-state context that carries memory between steps

Prerequisites

  • Papr API key in PAPR_MEMORY_API_KEY
  • LangGraph flow with node insertion points
  • Stable external_user_id in graph state

Integration Pattern

  1. Insert a Papr retrieval node near graph entry.
  2. Merge retrieved memory into graph state.
  3. Insert a writeback node before graph completion.

Minimal Setup

  1. Add a retrieval node to populate state memory.
  2. Add a writeback node near completion.
  3. Run a repeated flow and verify context carryover.

TypeScript Skeleton

import Papr from '@papr/memory';

type MemoryItem = {
  content: string;
};

type GraphState = {
  prompt: string;
  external_user_id: string;
  memories: MemoryItem[];
  final_response?: string;
};

const client = new Papr({
  xAPIKey: process.env['PAPR_MEMORY_API_KEY'],
});

export async function paprRetrieveNode(state: GraphState): Promise<GraphState> {
  const response = await client.memory.search({
    query: state.prompt,
    external_user_id: state.external_user_id,
    enable_agentic_graph: true,
    max_memories: 20,
    max_nodes: 15,
  });

  const memories = (response.data?.memories ?? []).map((item) => ({
    content: item.content,
  }));

  return { ...state, memories };
}

export async function paprWritebackNode(state: GraphState): Promise<GraphState> {
  if (!state.final_response) {
    return state;
  }

  await client.memory.add({
    content: state.final_response,
    external_user_id: state.external_user_id,
    metadata: { role: 'assistant', category: 'result' },
    memory_policy: { mode: 'auto' },
  });

  return state;
}

Validation Checklist

  • Retrieval node adds relevant memory to state
  • Final response is written back for future runs
  • Graph retains continuity across sessions for the same user

Troubleshooting

If memory context is empty, verify node ordering and payload fields, then use Error Playbook.