Tutorial

How to Add Memory + Routing to a LangChain Agent

An r/LangChain post called LangChain agents amnesic. Walk-through: LangGraph state + Scavio MCP named tools to fix routing and memory.

An r/LangChain post described production LangChain agents as amnesic. The fix has two parts: cross-turn state (LangGraph) and unambiguous tool routing (clearly named MCP tools). This walks both.

Prerequisites

  • LangChain 0.3+
  • LangGraph
  • Scavio API key + langchain-scavio
  • Postgres or SQLite for checkpoint storage

Walkthrough

Step 1: Install LangGraph and the Scavio tool

Both ship as standard PyPI packages.

Bash
pip install langgraph langchain-scavio langchain-anthropic

Step 2: Define the state shape

Track conversation history + tool-call decisions.

Python
from typing import TypedDict, List
from langgraph.graph import StateGraph, END

class AgentState(TypedDict):
    messages: List
    last_tool: str
    decisions: List[str]

Step 3: Wire Scavio's named tools

Each tool has a clear semantic name; the LLM routes by name.

Python
from langchain_scavio import ScavioSearchTool, ScavioRedditTool, ScavioYoutubeTool
tools = [
    ScavioSearchTool(name='web_search'),
    ScavioRedditTool(name='reddit_search'),
    ScavioYoutubeTool(name='youtube_search'),
]

Step 4: Add a checkpointer for cross-turn memory

SQLite for dev, Postgres for prod.

Python
from langgraph.checkpoint.sqlite import SqliteSaver
checkpointer = SqliteSaver.from_conn_string(':memory:')  # or 'agent.db'
graph = builder.compile(checkpointer=checkpointer)

Step 5: Invoke with a thread_id for persistent memory

Each thread_id is a separate conversation memory.

Python
config = {'configurable': {'thread_id': 'user-42'}}
result = graph.invoke({'messages': [HumanMessage('Find top AI agent threads')]}, config=config)

Python Example

Python
# The state survives across turns; the agent recalls prior tool calls and decisions.

JavaScript Example

JavaScript
// LangGraph is Python-first. JS-side, use LangChain.js with manual state in a KV store.

Expected Output

JSON
Agent that remembers prior turns and routes correctly because tools have semantic names (web_search, reddit_search, youtube_search) instead of generic 'search_v1'.

Related Tutorials

Frequently Asked Questions

Most developers complete this tutorial in 15 to 30 minutes. You will need a Scavio API key (free tier works) and a working Python or JavaScript environment.

LangChain 0.3+. LangGraph. Scavio API key + langchain-scavio. Postgres or SQLite for checkpoint storage. A Scavio API key gives you 500 free credits per month.

Yes. The free tier includes 500 credits per month, which is more than enough to complete this tutorial and prototype a working solution.

Scavio has a native LangChain package (langchain-scavio), an MCP server, and a plain REST API that works with any HTTP client. This tutorial uses the raw REST API, but you can adapt to your framework of choice.

Start Building

An r/LangChain post called LangChain agents amnesic. Walk-through: LangGraph state + Scavio MCP named tools to fix routing and memory.