I spent two weeks debugging a chatbot that kept forgetting context, only to realize I'd been treating a conversational API like a stateless REST endpoint. Each request went out alone, naked, with zero memory of what came before. The LLM had no idea what "it" meant in my follow-up question because I never told it.
That's when I realized I'd been thinking about AI integrations all wrong. I'd shipped production systems that cached database queries and managed session state without blinking, but somehow I approached LLM APIs like they were magic boxes that just "knew" things. They don't. And once I stopped treating them like black boxes and started managing conversation memory explicitly, everything clicked.
The Core Problem Nobody Explains Well Enough
Large language models are fundamentally stateless. Each API call is an isolated transaction. The model doesn't remember your last five requests, it can't, by design. If you want conversational continuity, your application has to maintain it.
I've seen this trip up everyone from interns to mid-level engineers. The mental model breaks because we're conditioned by chat interfaces like ChatGPT, where the app handles memory transparently. So when you're building something yourself, the responsibility shifts to you, and that's not obvious until you've shipped something that forgets.
The fix is straightforward but requires discipline: accumulate every message exchange (user input and model response) and pass the entire history with each new request. The model processes that full context window and responds accordingly.
Different APIs, Different Philosophies
What I found interesting about the original article's approach is how it compares OpenAI's message structure against Google's Gemini SDK. Both solve the same problem, maintaining conversation state, but they expose it differently.
OpenAI uses a flat list of message dictionaries with roles: system, user, and assistant. The system role primes behavior. User messages are inputs. Assistant messages are what the model said earlier. It's simple and predictable.
Gemini separates concerns differently: it pulls system instructions out into a config object and structures conversation history as Content objects with Parts. This matters because Gemini's architecture assumed multimodal conversations from the start, each turn can bundle text, images, or files together.
Both work. Both require the same fundamental discipline: you must track and persist the conversation state yourself. The API won't do it for you.
My Take: This Is Deceptively Non-Trivial
Here's what bothers me about how this is typically presented: tutorials make it sound like "just pass the full history each time" is the complete solution. It's not. In production, you hit real constraints immediately.
Token limits are real. Sending the entire conversation history with every request burns through your token quota fast. A ten-turn conversation becomes expensive at scale. You need strategies, trimming old messages, summarizing history, using sliding windows.
Latency compounds. Each request now carries baggage. Response times creep up. Users notice.
Storage and retrieval add complexity. JSON persistence works fine for prototypes, but you need to think about how you're storing these conversations. Single files? Databases? How do you query them? What if the same user has fifty simultaneous conversations?
The original article keeps this simple, which is good for learning but glosses over production realities. That's not a criticism, beginner content should start simple. But I'd push back on the implicit assumption that JSON file persistence is sufficient for anything beyond a learning project.
Practical Code Pattern I Use
Here's the pattern I've settled on after a few iterations:
class ConversationManager:
def __init__(self, max_history: int = 10):
self.history: list[dict] = []
self.max_history = max_history
def add_message(self, role: str, content: str) -> None:
self.history.append({"role": role, "content": content})
# Keep only recent messages to manage tokens
if len(self.history) > self.max_history:
self.history = self.history[-self.max_history:]
def get_context(self) -> list[dict]:
# Always include system message
system = [{"role": "system", "content": "You are HelpBot..."}]
return system + self.history
This approach gives me control over what gets sent and when. I can tune max_history based on token budgets and response time requirements.
Where Do We Go From Here?
The real question I'm sitting with: as LLMs become commodity infrastructure in applications, how do we standardize conversation persistence across teams and companies? JSON files don't scale. Building custom solutions for every project seems wasteful.
I'd love to know how you're handling this. Are you using existing frameworks like LangChain, or rolling your own? Have you hit the token limit wall yet?
Source: This post was inspired by "From Stateless to Stateful: Managing AI Chat Memory and JSON Persistence in Python" by Dev.to. Read the original article