By using this site, you agree to the Privacy Policy and Terms of Use.
Accept
World of SoftwareWorld of SoftwareWorld of Software
  • News
  • Software
  • Mobile
  • Computing
  • Gaming
  • Videos
  • More
    • Gadget
    • Web Stories
    • Trending
    • Press Release
Search
  • Privacy
  • Terms
  • Advertise
  • Contact
Copyright © All Rights Reserved. World of Software.
Reading: Why I Stopped Letting aI Agents Write Directly to my Database (and Built MemState) | HackerNoon
Share
Sign In
Notification Show More
Font ResizerAa
World of SoftwareWorld of Software
Font ResizerAa
  • Software
  • Mobile
  • Computing
  • Gadget
  • Gaming
  • Videos
Search
  • News
  • Software
  • Mobile
  • Computing
  • Gaming
  • Videos
  • More
    • Gadget
    • Web Stories
    • Trending
    • Press Release
Have an existing account? Sign In
Follow US
  • Privacy
  • Terms
  • Advertise
  • Contact
Copyright © All Rights Reserved. World of Software.
World of Software > Computing > Why I Stopped Letting aI Agents Write Directly to my Database (and Built MemState) | HackerNoon
Computing

Why I Stopped Letting aI Agents Write Directly to my Database (and Built MemState) | HackerNoon

News Room
Last updated: 2025/12/05 at 5:50 PM
News Room Published 5 December 2025
Share
Why I Stopped Letting aI Agents Write Directly to my Database (and Built MemState) | HackerNoon
SHARE

Recently, I decided to build a classic AI agent for ordering pizza. The goal was simple: ask for the pizza type, ask for toppings, confirm the order, and save it.

I  used the standard stack: LangChain, LangGraph, and SQLite. n Here is what my first version looked like:

import sqlite3
from langchain_core.tools import tool
from langchain.agents import create_agent
from langchain.chat_models import init_chat_model
from langgraph.checkpoint.sqlite import SqliteSaver


@tool
def create_order(pizza_type: str, size: str):
    """Create a new pizza order."""
    # Simulation: Just printing, no real state management here!
    print(f"Creating order: {size} {pizza_type}")
    return "Order created."


@tool
def update_order(new_details: str):
    """Update existing order."""
    return "Order updated."


@tool
def confirm_order():
    """Call this to finalize the order."""
    return f"Order sent to kitchen!"

llm = init_chat_model(model="gpt-4o", model_provider="openai")
agent = create_agent(
    llm,
    tools=[create_order, update_order, confirm_order],
    checkpointer=SqliteSaver(sqlite3.connect("agent.db", check_same_thread=False)),
)

config = {"configurable": {"thread_id": "session_1"}}
agent.invoke(
    {"messages": [("user", "I want a large Pepperoni.")]},
    config=config
)

The logic seems fine, right?

  1. User says “I want Pepperoni” → Agent calls create_order.
  2. Database executes INSERT INTO orders ....
  3. User says “No onions please” → Agent calls update_order.
  4. Database executes UPDATE orders ....

Then I realized the architecture was broken.

Imagine if the user says on step 3: “Actually, I changed my mind. I don’t want pizza, I want sushi.”

Now, my production database has a “dirty” record of a Pepperoni order that was never finished. I have to write logic to delete it, handle cancellations, and clean up the garbage.

I was letting the Agent’s “thought process”, which is chaotic and prone to mistakes, write directly to my production database. This creates Dirty Writes.

Attempt #1: Vector Memory?

Many developers suggest using tools like Mem0 or Zep. But those are for semantic memory. They help the agent remember that “Alice likes spicy food.”

They do not solve the transactional state problem. Vectors cannot guarantee that my order ID is unique or that the price is a valid number.

The Solution: MemState (A “Buffer” for Agents)

I needed a middle layer. A “Draft” area where the agent can mold the data like clay, make mistakes, fix them, and only commit when everything is perfect.

I couldn’t find a simple tool for this, so I built MemState.

Think of it as Git, but for Agent data:

  1. Strict Types (Pydantic): The agent cannot save garbage data.
  2. Transactions: Every change is logged. I can rollback if the agent hallucinates.
  3. Constraints: I can prevent duplicates automatically.

Here is the new Agent code:

from langchain_core.tools import tool
from langchain.agents import create_agent
from langchain.chat_models import init_chat_model
from pydantic import BaseModel
from memstate import MemoryStore, Fact, Constraint, SQLiteStorage
from memstate.integrations.langgraph import MemStateCheckpointer


class PizzaOrder(BaseModel):
    status: str = "new"
    pizza_type: str
    size: str
    toppings: list[str] = []


storage = SQLiteStorage("pizza_shop.db")
memory = MemoryStore(storage)

# 🔥 KILLER FEATURE: Singleton Constraint
# A single user can have ONLY ONE active order in a single session.
# If the agent attempts to create a second one, MemState will automatically update the first.
memory.register_schema("order", PizzaOrder, Constraint(singleton_key="session_id"))

checkpointer = MemStateCheckpointer(memory=memory)


@tool
def update_order(pizza_type: str, size: str, toppings: list[str]):
    """Call this tool to create or update the pizza order."""

    # We use thread_id as a unique key (singleton_key)
    # In a real application, thread_id is passed through the context (config)
    session_id = "session_1"

    # The agent simply "throws" the fact. It doesn't need to check whether the order exists.
    # MemState will decide for itself: INSERT or UPDATE.
    fid = memory.commit(
        Fact(
            type="order",
            payload={
                "session_id": session_id,
                "pizza_type": pizza_type,
                "size": size,
                "toppings": toppings
            }
        )
    )
    return f"Order state saved. Fact ID: {fid}"


@tool
def confirm_order():
    """Call this to finalize the order."""
    orders = memory.query(typename="order", json_filters={"session_id": "session_1"})
    return f"Order {orders[0]['payload']} sent to kitchen!"


llm = init_chat_model(model="gpt-4o", model_provider="openai")
agent = create_agent(
    llm,
    tools=[update_order, confirm_order],
    checkpointer=checkpointer,
)

config = {"configurable": {"thread_id": "session_1"}}
agent.invoke(
    {"messages": [("user", "I want a large Pepperoni.")]},
    config=config
)

Why is this better?

  1. The “Draft” Concept: While the user is changing their mind (“add mushrooms”, “remove cheese”), we are only updating the local MemState. My main production database stays clean.
  2. Validation: If the Agent hallucinates and tries to set the pizza price to “one million”, the Pydantic schema in MemState will reject it before it corrupts the state.
  3. One Clean Write: When the user finally says “Confirm”, I can simply query the final, validated JSON from MemState and do one clean INSERT into my main database.

Summary

MemState turns the chaos of a conversation into a structured transaction. It supports rollback() (Time Travel), LangGraph checkpoints, and runs on SQLite or Redis.

It’s Open Source. I would love your feedback:

https://github.com/scream4ik/MemState

Sign Up For Daily Newsletter

Be keep up! Get the latest breaking news delivered straight to your inbox.
By signing up, you agree to our Terms of Use and acknowledge the data practices in our Privacy Policy. You may unsubscribe at any time.
Share This Article
Facebook Twitter Email Print
Share
What do you think?
Love0
Sad0
Happy0
Sleepy0
Angry0
Dead0
Wink0
Previous Article Shiba (SHIB), GeeFi (GEE) or Cardano (ADA)? Why Analysts Say the Newcomer Should Get More Attention This Winter Shiba (SHIB), GeeFi (GEE) or Cardano (ADA)? Why Analysts Say the Newcomer Should Get More Attention This Winter
Next Article You can now use Pixel phones as a Switch 2 webcam You can now use Pixel phones as a Switch 2 webcam
Leave a comment

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Stay Connected

248.1k Like
69.1k Follow
134k Pin
54.3k Follow

Latest News

Special Tap to Pay at MuyPymes
Special Tap to Pay at MuyPymes
Mobile
The 8-in-1 EDC is the gift they didn’t see coming — and it’s just
The 8-in-1 EDC is the gift they didn’t see coming — and it’s just $20
News
Why is it physically impossible to make an object invisible?
Why is it physically impossible to make an object invisible?
Mobile
Google’s Most Powerful Productivity Tool Can Save You So Much Time – BGR
Google’s Most Powerful Productivity Tool Can Save You So Much Time – BGR
News

You Might also Like

Secure Legion Launches First Metadata-Free Messenger with Zero Servers | HackerNoon
Computing

Secure Legion Launches First Metadata-Free Messenger with Zero Servers | HackerNoon

11 Min Read
Solving the FastAPI, Alembic, Docker Problem | HackerNoon
Computing

Solving the FastAPI, Alembic, Docker Problem | HackerNoon

9 Min Read
When Bots Replace People: Why Your AI Strategy Needs More Humanity | HackerNoon
Computing

When Bots Replace People: Why Your AI Strategy Needs More Humanity | HackerNoon

10 Min Read
The Shift from Ad-Hoc Competitive Research to Always-On Competitive Intelligence in B2B SaaS | HackerNoon
Computing

The Shift from Ad-Hoc Competitive Research to Always-On Competitive Intelligence in B2B SaaS | HackerNoon

10 Min Read
//

World of Software is your one-stop website for the latest tech news and updates, follow us now to get the news that matters to you.

Quick Link

  • Privacy Policy
  • Terms of use
  • Advertise
  • Contact

Topics

  • Computing
  • Software
  • Press Release
  • Trending

Sign Up for Our Newsletter

Subscribe to our newsletter to get our newest articles instantly!

World of SoftwareWorld of Software
Follow US
Copyright © All Rights Reserved. World of Software.
Welcome Back!

Sign in to your account

Lost your password?