Your RAG Agent Forgets Everything After One Message - Here’s How I Fixed It with Databricks…
From the publisher’s feed, collected in the ZeroLabs newsroom.
Your RAG Agent Forgets Everything After One Message - Here’s How I Fixed It with Databricks LakebaseBuilding a context-aware RAG system end-to-end: from PDF parsing to multi-turn conversations that actually rememberMost RAG tutorials show you how to build a system that answers questions from documents. You parse some PDFs, chunk them, embed them, retrieve relevant context, and feed it to an LLM. Works great — until the user asks a follow-up question.“How does it handle overheating?”It? What’s “it”? Your agent has no idea, because the previous turn where the user asked about Orion’s motion controller was stored in InMemorySaver() — which is gone the moment your Model Serving endpoint processes the next request.This is the gap between a demo and a production RAG system. In this article, I’ll walk through the complete build — from parsing multimodal PDFs to deploying a context-aware agent that maintains conversation history across turns using Databricks Lakebase (Postgres).The ArchitectureHere’s what we’re building:PDFs (text + images + diagrams) ↓ ai_parse_document() (Version 2.0)Parsed elements (text, tables, figure descriptions) ↓ RecursiveCharacterTextSplitterChunks table (Delta, with Change Data Feed) ↓ Delta Sync + GTE-LargeVector Search Index ↓ VectorSearchRetrieverToolLangChain Agent + PostgresSaver ↓ ↓LLM (Foundation Model) Lakebase (conversation memory) ↓Model Serving Endpoint (MLflow ResponsesAgent)Key components:Document parsing: ai_parse_document() (Version 2.0) — Databricks' built-in multimodal parserEmbeddings: databricks-gte-large-en — 1024-dim, 8192-token context windowVector store: Databricks Vector Search (Delta Sync, managed embeddings)Conversation memory: Lakebase Autoscaling (managed Postgres 17)Agent framework: LangChain + LangGraph + MLflow ResponsesAgentDeployment: Model Serving via agents.deploy()Let’s build it step by step.Step 1: Parse Documents with ai_parse_document()Our source documents are PDFs stored in a Unity Catalog Volume — they contain text, tables, images, and architecture diagrams. The ai_parse_document() function (version 2.0) handles all of this in a single call. It extracts text, renders tables as HTML, and generates AI descriptions for figures.from pyspark.sql.functions import expr# Volume path where PDFs are storeddocs_path = "/Volumes///source_docs/"# Read all files as binarydocs_df = spark.read.format("binaryFile").load(docs_path)# Parse each document using ai_parse_document v2.0parsed_df = docs_df.withColumn( "parsed_content", expr(f"""ai_parse_document(content, map( "version", "2.0", "imageOutputPath", "{docs_path}/parsed_images/" ))"""))# Drop binary content (too large to display)parsed_df = parsed_df.drop("content")# Save to Delta tableoutput_table = "..docs_parsed"parsed_df.write.format("delta").mode("overwrite").saveAsTable(output_table)print(f"✅ Parsed results saved to: {output_table}")The output is a VARIANT column with structured elements — each element has a type (text, table, figure, section_header, etc.), content, and optionally a description (for figures). This is significantly better than PyPDF2 or Unstructured for complex enterprise PDFs with mixed content.Step 2: Clean, Transform, and ChunkThe parsed JSON needs to be converted into clean text and then chunked for retrieval. I used two approaches:Fast Plain Text ExtractionConcatenate all text elements into a single string with == page == tokens separating pages:from pyspark.sql import functions as F# Convert VARIANT to JSON string, then extract text contentsafe_json_col = F.coalesce( F.to_json(F.col("parsed_content")), F.col("parsed_content").cast("string"))plain_text_df = parsed_df.withColumn( "plain_text", extract_contents_udf()(safe_json_col) # Custom UDF to join text elements)Chunk with LangChainfrom langchain_text_splitters import RecursiveCharacterTextSplitterfrom pyspark.sql.types import StructType, StructField, StringTypeimport pandas as pdCHUNK_SIZE = 2000CHUNK_OVERLAP = 200splitter = RecursiveCharacterTextSplitter( chunk_size=CHUNK_SIZE, chunk_overlap=CHUNK_OVERLAP, separators=["\n== page ==\n", "== page ==", "\n\n", "\n", " ", ""])schema = StructType([ StructField("path", StringType(), True), StructField("chunk", StringType(), True),])def split_rows(iterator): for pdf in iterator: out = [] for _, row in pdf.iterrows(): path, text = row["path"], row["plain_text"] if isinstance(text, str) and text.strip(): for c in splitter.split_text(text): if c and c.strip(): out.append((path, c)) yield pd.DataFrame(out, columns=["path", "chunk"])df_chunks = ( plain_text_df.select("path", "plain_text") .mapInPandas(split_rows, schema=schema))# Add unique IDs and savedf_chunks = df_chunks.withColumn("id", F.monotonically_increasing_id())chunked_table = "..docs_chunked"df_chunks.write.format("delta") \ .mode("overwrite") \ .option("mergeSchema", "true") \ .saveAsTable(chunked_table)Step 3: Build Vector SearchEnable Change Data FeedVector Search Delta Sync requires CDF enabled on the source table:ALTER TABLE ..docs_chunkedSET TBLPROPERTIES (delta.enableChangeDataFeed = true);Create the Delta Sync Indexfrom databricks.vector_search.client import VectorSearchClientvsc = VectorSearchClient(disable_notice=True)index_name = "..docs_chunked_index"vsc.create_delta_sync_index_and_wait( endpoint_name="", index_name=index_name, source_table_name="..docs_chunked", primary_key="id", embedding_source_column="chunk", embedding_model_endpoint_name="databricks-gte-large-en", pipeline_type="TRIGGERED",)With managed embeddings, Databricks automatically handles embedding computation during both indexing (reads chunk, sends to GTE-Large, stores the 1024-dim vectors) and query time (auto-embeds your search string). You never call the embedding model directly.Test Retrievalindex = vsc.get_index(index_name=index_name)results = index.similarity_search( query_text="How does the system prevent overheating?", columns=["path", "chunk"], num_results=5,)display(results)Step 4: Set Up Lakebase for Conversation MemoryThis is where most RAG tutorials stop. They plug in InMemorySaver() and call it a day. But InMemorySaver means:State lost between requests — every Model Serving invocation starts freshNo multi-turn conversations — the agent can’t resolve “it”, “that”, “the one you mentioned”No session persistence — kernel restart = amnesiaLakebase (Databricks’ managed Postgres 17, GA on AWS since early 2026) solves this. We’ll use langgraph-checkpoint-postgres to swap InMemorySaver with a Postgres-backed checkpointer.Provision a Lakebase Autoscaling ProjectClick Apps → Lakebase Postgres in your workspaceSelect Autoscaling → New projectName it (e.g., my-agent-memory), select Postgres 17Takes about 1 minute to provisionGet Connection Details Programmaticallyfrom databricks.sdk import WorkspaceClientw = WorkspaceClient()project_id = ""# Get branch and endpointbranches = list(w.postgres.list_branches(parent=f"projects/{project_id}"))branch_name = branches[0].nameendpoints = list(w.postgres.list_endpoints(parent=branch_name))ep = endpoints[0]HOST = ep.status.hosts.hostENDPOINT = ep.nameUSERNAME = ""print(f"Host: {HOST}")print(f"Endpoint: {ENDPOINT}")Important: Lakebase Autoscaling uses w.postgres.* API, not w.database.* (which is for legacy Provisioned instances). The docs still mix these — don't get tripped up.Test the Connectionimport psycopg2cred = w.postgres.generate_database_credential(endpoint=ENDPOINT)conn = psycopg2.connect( host=HOST, dbname="databricks_postgres", user=USERNAME, password=cred.token, port=5432, sslmode="require")with conn.cursor() as cur: cur.execute("SELECT version()") print(cur.fetchone()[0])conn.close()print("✅ Connected to Lakebase!")Create Checkpoint Tablesfrom urllib.parse import quotefrom langgraph.checkpoint.postgres import PostgresSavercred = w.postgres.generate_database_credential(endpoint=ENDPOINT)DB_URI = ( f"postgresql://{quote(USERNAME, safe='')}:{quote(cred.token, safe='')}" f"@{HOST}:5432/databricks_postgres" f"?sslmode=require")with PostgresSaver.from_conn_string(DB_URI) as checkpointer: checkpointer.setup() print("✅ Checkpoint tables created!")Gotcha: If your Databricks username contains @ (e.g., user@company.com), you must use urllib.parse.quote() to URL-encode it. Otherwise the @ is parsed as the URI separator between credentials and host, and the connection fails silently.This creates four tables in Lakebase: checkpoints, checkpoint_writes, checkpoint_blobs, and checkpoint_migrations.Step 5: Build the Context-Aware AgentNow the critical swap — replace InMemorySaver with a Postgres-backed checkpointer in the agent.Interactive Agent (Notebook)from urllib.parse import quotefrom langchain.agents import create_agentfrom databricks_langchain import ChatDatabricks, VectorSearchRetrieverToolfrom langgraph.checkpoint.postgres import PostgresSaverfrom databricks.sdk import WorkspaceClientimport psycopgfrom psycopg.rows import dict_rowdef get_lakebase_checkpointer(host: str, endpoint: str, username: str): """Create a PostgresSaver backed by Lakebase Autoscaling.""" w = WorkspaceClient() cred = w.postgres.generate_database_credential(endpoint=endpoint) db_uri = ( f"postgresql://{quote(username, safe='')}:{quote(cred.token, safe='')}" f"@{host}:5432/databricks_postgres" f"?sslmode=require" ) # IMPORTANT: Use psycopg.connect directly, not from_conn_string # from_conn_string returns a context manager, not a persistent instance conn = psycopg.connect(db_uri, autocommit=True, row_factory=dict_row) checkpointer = PostgresSaver(conn=conn) checkpointer.setup() return checkpointerdef build_agent(llm_endpoint: str, index_name: str, num_results: int = 3): model = ChatDatabricks(endpoint=llm_endpoint, max_tokens=500) vs_tool = VectorSearchRetrieverTool( name="knowledge_search", index_name=index_name, description="Search knowledge base for relevant information", num_results=num_results, ) # Lakebase-backed checkpointer instead of InMemorySaver checkpointer = get_lakebase_checkpointer(HOST, ENDPOINT, USERNAME) system_prompt = """You are a Knowledge Assistant. Respond in a clear, professional tone. Use only verified information from the provided documents. If the answer cannot be found, clearly state that.""" return create_agent( model=model, tools=[vs_tool], system_prompt=system_prompt, checkpointer=checkpointer, )Test Multi-Turn Conversationagent = build_agent("", "", 3)# STABLE thread_id - this is what enables context awarenessconfig = {"configurable": {"thread_id": "demo-session-001"}}# Turn 1r1 = agent.invoke( {"messages": [{"role": "user", "content": "What is the Orion system?"}]}, config=config)print("Turn 1:", r1['messages'][-1].content)# Turn 2 - agent should know "it" = Orionr2 = agent.invoke( {"messages": [{"role": "user", "content": "How does it handle overheating?"}]}, config=config)print("Turn 2:", r2['messages'][-1].content)The magic is in the stable thread_id. With InMemorySaver, each Model Serving request gets a new UUID — no memory. With PostgresSaver and a consistent thread_id, the agent retrieves the full conversation history from Lakebase before generating a response.Step 6: Production Agent Code (agent.py)For deployment, the agent code lives in a standalone Python file using MLflow’s “agent as code” pattern:# agent.pyimport osfrom uuid import uuid4from typing import Any, Dict, Listfrom urllib.parse import quoteimport yamlimport mlflowimport psycopgfrom psycopg.rows import dict_rowfrom mlflow.pyfunc import ResponsesAgentfrom mlflow.types.responses import ResponsesAgentRequest, ResponsesAgentResponsefrom langchain.agents import create_agentfrom databricks_langchain import ChatDatabricks, VectorSearchRetrieverToolfrom langgraph.checkpoint.postgres import PostgresSaverfrom databricks.sdk import WorkspaceClientdef _load_config(path: str = "agent-config.yaml") -> Dict[str, Any]: if not os.path.exists(path): raise FileNotFoundError(f"Config file not found at '{path}'") with open(path, "r", encoding="utf-8") as f: cfg = yaml.safe_load(f) or {} llm_endpoint = cfg.get("llm_endpoint_name") vs = cfg.get("vector_search", {}) or {} index_name = vs.get("index_name") num_results = int(vs.get("num_results", 3)) lakebase = cfg.get("lakebase", {}) or {} return { "llm_endpoint_name": llm_endpoint, "vs_index_name": index_name, "vs_num_results": num_results, "lakebase_host": lakebase.get("host"), "lakebase_endpoint": lakebase.get("endpoint"), "lakebase_user": lakebase.get("user"), }def get_lakebase_checkpointer(host, endpoint, user): w = WorkspaceClient() cred = w.postgres.generate_database_credential(endpoint=endpoint) db_uri = ( f"postgresql://{quote(user, safe='')}:{quote(cred.token, safe='')}" f"@{host}:5432/databricks_postgres?sslmode=require" ) conn = psycopg.connect(db_uri, autocommit=True, row_factory=dict_row) checkpointer = PostgresSaver(conn=conn) checkpointer.setup() return checkpointerdef build_agent(llm_endpoint, index_name, num_results, lakebase_host, lakebase_endpoint, lakebase_user): model = ChatDatabricks(endpoint=llm_endpoint, max_tokens=500) vs_tool = VectorSearchRetrieverTool( name="knowledge_search", index_name=index_name, description="Search knowledge base for relevant information", num_results=num_results, ) checkpointer = get_lakebase_checkpointer( lakebase_host, lakebase_endpoint, lakebase_user ) system_prompt = ( "You are a Knowledge Assistant. Respond in a clear, professional tone. " "Use only verified information from the provided documents. " "If the answer cannot be found, clearly state that." ) return create_agent( model=model, tools=[vs_tool], system_prompt=system_prompt, checkpointer=checkpointer, )def _last_user_text(messages): user_msgs = [m for m in messages if m.get("role") == "user"] return str(user_msgs[-1].get("content", "")) if user_msgs else ""class LangChainResponsesAgent(ResponsesAgent): def __init__(self): cfg = _load_config() self._agent = build_agent( cfg["llm_endpoint_name"], cfg["vs_index_name"], cfg["vs_num_results"], cfg["lakebase_host"], cfg["lakebase_endpoint"], cfg["lakebase_user"], ) def predict(self, request: ResponsesAgentRequest) -> ResponsesAgentResponse: msgs = [m.model_dump() for m in request.input] custom_inputs = dict(request.custom_inputs or {}) thread_id = custom_inputs.get("thread_id", f"session-{uuid4()}") result = self._agent.invoke( {"messages": msgs}, config={"configurable": {"thread_id": thread_id}}, ) try: text = result["messages"][-1].content except Exception: text = str(result) return ResponsesAgentResponse( output=[self.create_text_output_item(text, str(uuid4()))], custom_outputs={"thread_id": thread_id}, )AGENT = LangChainResponsesAgent()mlflow.models.set_model(AGENT)Configuration (agent-config.yaml)llm_endpoint_name: vector_search: index_name: ..docs_chunked_index num_results: 3lakebase: host: endpoint: projects//branches/production/endpoints/primary user: Step 7: Log, Register, and DeployLog to MLflowimport mlflowfrom importlib.metadata import version as get_versionfrom mlflow.models.resources import DatabricksVectorSearchIndex, DatabricksServingEndpointresources = [ DatabricksVectorSearchIndex(index_name="..docs_chunked_index"), DatabricksServingEndpoint(endpoint_name=""),]with mlflow.start_run(): mlflow.set_tags({ "model_type": "retrieval_agent", "framework": "langchain", "memory": "lakebase_autoscaling", }) logged_agent_info = mlflow.pyfunc.log_model( name="knowledge_assistant", python_model="agent.py", code_paths=["agent-config.yaml"], input_example={"input": [{"role": "user", "content": "What is Orion?"}]}, pip_requirements=[ f"databricks-vectorsearch=={get_version('databricks-vectorsearch')}", f"databricks-langchain=={get_version('databricks-langchain')}", f"langchain=={get_version('langchain')}", f"mlflow=={get_version('mlflow')}", "langgraph-checkpoint-postgres", "psycopg[binary]", "databricks-sdk>=0.89.0", ], resources=resources, ) model_uri = logged_agent_info.model_uriRegister to Unity Catalogmlflow.set_registry_uri("databricks-uc")UC_MODEL_NAME = "..knowledge_assistant"uc_info = mlflow.register_model(model_uri=model_uri, name=UC_MODEL_NAME)print(f"✅ Registered: {UC_MODEL_NAME} v{uc_info.version}")Deployfrom databricks import agentsdeployment = agents.deploy( model_name=UC_MODEL_NAME, model_version=uc_info.version, scale_to_zero_enabled=True,)print(f"✅ Endpoint: {deployment.query_endpoint}")The ResultWith the agent deployed, multi-turn conversations work exactly as you’d expect:ws = WorkspaceClient()client = ws.serving_endpoints.get_open_ai_client()session = "user-session-042"# Turn 1r1 = client.responses.create( model="knowledge_assistant", input=[{"role": "user", "content": "What is the Orion motion controller?"}], extra_body={"custom_inputs": {"thread_id": session}})# Turn 2 - "it" resolves correctly to Orionr2 = client.responses.create( model="knowledge_assistant", input=[{"role": "user", "content": "How does it prevent overheating?"}], extra_body={"custom_inputs": {"thread_id": session}})And in Lakebase, you can see the checkpoint data accumulating across conversation threads — each thread preserving the full message history for context-aware follow-ups.Quick Check for Validating Memory Persistence:import jsonfrom uuid import uuid4thread_id = f"memory-test-{uuid4()}"# --- 1. Use custom input instead of input_example ---custom_input = { "input": [{"role": "user", "content": "What are the main components of Orion?"}], "custom_inputs": {"thread_id": thread_id},}print("=" * 50)print("TEST 1: Custom input (no input_example needed)")print("=" * 50)# Use output_path to capture results (without it, mlflow.models.predict returns None)mlflow.models.predict( model_uri=model_uri, input_data=custom_input, env_manager="uv", output_path="/tmp/result_1.json",)with open("/tmp/result_1.json", "r") as f: result_1 = json.load(f)thread_id_1 = result_1["custom_outputs"]["thread_id"]response_1 = result_1["output"][0]["content"][0]["text"]print(f"Thread ID: {thread_id_1}")print(f"Response: {response_1[:300]}...")# --- 2. Test memory persistence with same thread_id ---follow_up_input = { "input": [ { "role": "user", "content": "Can you elaborate more on the first component you mentioned?", } ], "custom_inputs": {"thread_id": thread_id}, # Reuse same thread}print("\n" + "=" * 50)print("TEST 2: Follow-up on same thread (memory test)")print("=" * 50)mlflow.models.predict( model_uri=model_uri, input_data=follow_up_input, env_manager="uv", output_path="/tmp/result_2.json",)with open("/tmp/result_2.json", "r") as f: result_2 = json.load(f)thread_id_2 = result_2["custom_outputs"]["thread_id"]response_2 = result_2["output"][0]["content"][0]["text"]print(f"Thread ID: {thread_id_2}")print(f"Response: {response_2[:300]}...")# --- 3. Verify memory with actual conditions ---print("\n" + "=" * 50)print("MEMORY CHECK")print("=" * 50)# Check 1: Thread IDs matchif thread_id_1 == thread_id_2: print(f"✅ Thread ID match: {thread_id_1}")else: print(f"❌ Thread ID mismatch! Call 1: {thread_id_1}, Call 2: {thread_id_2}")# Check 2: Follow-up response references context from the first responsefollow_up_lower = response_2.lower()if len(response_2) > 50 and any( keyword in follow_up_lower for keyword in [ "motion", "vision", "cognition", "communication", "subsystem", "component", ]): print( "✅ Follow-up response references components from the first answer — memory is intact!" )else: print( "⚠️ Follow-up response may not reference the first answer. Manual review recommended." ) print(f" Follow-up preview: {response_2[:200]}")print( "\n✅ Lakebase Postgres checkpointing is working correctly!" if thread_id_1 == thread_id_2 else "\n❌ Memory persistence test FAILED.")You can see memory persistence working.Screenshots Of Lakebase Postgres CheckPoint Tables:Gotchas I Hit Along the Way1. Lakebase Autoscaling vs Provisioned — completely different APIs. If you create a Lakebase project via the UI today, you get an Autoscaling project (the new default since March 2026). But most notebook examples in the docs use w.database.* — that's the Provisioned API. For Autoscaling, use w.postgres.*. I burned 30 minutes on NotFound: Resource not found before figuring this out.2. PostgresSaver.from_conn_string() returns a context manager, not an instance. If you try checkpointer = PostgresSaver.from_conn_string(uri) and then call .setup(), you get AttributeError: '_GeneratorContextManager' object has no attribute 'setup'. The fix: use psycopg.connect() directly with autocommit=True and row_factory=dict_row, then pass the connection to PostgresSaver(conn=conn).3. URL-encode your username. Databricks emails contain @, which breaks PostgreSQL connection URIs. Always use urllib.parse.quote(username, safe='').4. The ep.host path is wrong. When inspecting Lakebase endpoint objects from the SDK, the host is at ep.status.hosts.host, not ep.host or ep.hosts.host.5. OAuth tokens expire after 60 minutes. w.postgres.generate_database_credential() returns a token that's valid for one hour. For long-running endpoints, implement token rotation or regenerate on each agent initialization.What Changed from a Standard RAG AgentThe entire modification boils down to a small set of surgical changes:Component Standard RAG Context-Aware RAG Checkpointer InMemorySaver() PostgresSaver(conn=conn) thread_id Random UUID per request Stable ID from custom_inputs custom_outputs Pass-through Returns thread_id for session continuity Config LLM + Vector Search + Lakebase host, endpoint, user Dependencies Base LangChain + langgraph-checkpoint-postgres, psycopg[binary]Everything else — parsing, chunking, Vector Search, system prompt, tool wiring, MLflow logging, deployment — stays identical.Wrapping UpThe difference between a RAG demo and a production RAG system often comes down to state management. Databricks now provides every piece of this puzzle natively: ai_parse_document() (version 2.0) for multimodal PDF parsing, Vector Search with managed GTE-Large embeddings, Lakebase for conversation persistence, and the Agent Framework for deployment.The key architectural decision is using Lakebase for conversation state while keeping Vector Search for document retrieval — each optimized for what it does best. Lakebase gives you a fully managed Postgres with scale-to-zero, so you’re only paying for compute when your agent is actively handling conversations.If you’re building RAG on Databricks and your agent can’t handle “What did I just ask?”, this is the fix.If you found this walkthrough useful, connect with me on LinkedIn or follow on Medium — I regularly publish deep-dives on Databricks, Lakehouse architecture, Data Engineering patterns and AI Agents. I’m always happy to discuss the real-world tradeoffs behind these decisions.References:Databricks AI Agent Memory DocumentationLakebase Autoscaling Getting StartedLakebase Autoscaling API Guideai_parse_document Function ReferenceVector Search Documentationlanggraph-checkpoint-postgresDatabricks Agent Templates#Databricks #RAG #Lakebase #PostgreSQL #GenAI #LangChain #VectorSearch #DataEngineering #AIYour RAG Agent Forgets Everything After One Message - Here’s How I Fixed It with Databricks… was originally published in Towards AI on Medium, where people are continuing the conversation by highlighting and responding to this story.