← Back to blog
steply / blog · memoria-persistente-para-ia-on-premise-ttl-e-controle-de-contexto.md
$ steply blog open memoria-persistente-para-ia-on-premise-ttl-e-controle-de-contexto
▸ loading article…
✓ ready

Persistent memory in on-premise AI: TTL, context control and the tools that make AI remember without flooding the prompt

bySteply23 min read

Imagine hiring the most brilliant employee on the market under a single condition: every morning, he forgets everything. Who you are, what the company does, what was agreed yesterday, which client hates being called "sir". Every day the same introduction, every day the same mistakes corrected all over again. Nobody would sign that contract. And yet that is exactly the contract most companies sign when they put an AI to work: a brilliant model, with total amnesia between one conversation and the next.

This is the longest post we have ever published, and that is on purpose. Persistent memory is the subject that separates the AI that impresses in the demo from the AI that actually works in your operation, and it is a subject almost nobody treats with the depth it deserves. Here you will understand why the context window is not memory (and why confusing the two gets expensive), how an AI organizes what it remembers into levels, what TTL is and why memory without an expiration date becomes a liability, how to use memory from outside the context window without flooding the prompt, which tools control all of this, and why this entire memory should live inside your company, not on a third party's server.

Let's take it step by step, calmly and with scientific grounding. At the end there is an implementation checklist for anyone who wants to go straight from the text to practice.

The context window is a desk, not a filing cabinet

Every AI model has a context window: the amount of information it can "see" at once while answering. Everything you wrote in the conversation, the attached documents, the system instructions, all of it takes up space in that window. When the conversation ends, the window empties. Nothing survives.

The best analogy is a desk. While you work, papers lie spread across the desk, within arm's reach. At the end of the day, someone clears the desk completely. The desk is not the company's filing cabinet; it is just the surface where today's work happens. The context window is exactly that: working memory, not real memory.

The most common reaction to this limit is brute force: "then let's use a model with a giant window and dump everything into it". Vendors encourage that reading, advertising windows of a million tokens (a token is the little chunk of text the model processes, and also the unit it bills by). It sounds like the solution. It is not, for three reasons that compound.

Reason 1: you pay for everything on the desk, all the time

The model reprocesses the entire context for every answer. If you dump 200 pages of history into the prompt, you pay for processing those 200 pages on every single question, even when the question only needed one paragraph. It is the mechanism we detailed in the post about the invisible cost of tokens: the bill does not go up because the AI failed, it goes up because the AI is hauling dead weight on every round.

Reason 2: the more you dump, the worse the model reads

This is the part the giant-window marketing does not mention, and there is a study behind it. In 2023, Stanford researchers published the paper that became known as "Lost in the Middle" (Liu et al., 2023). They measured what happens when the relevant information changes position inside a long context. The result: models do well when the answer sits at the very beginning or the very end of the context, and degrade sharply when it sits in the middle. In some tests, the model with the full document in context scored worse than the same model with no document at all.

Translating that to your operation: dumping the client's complete history into the prompt does not guarantee the AI will use that information. It guarantees you paid for it. The critical fact buried on page 40 of 200 has a real chance of being simply ignored. A giant context without criteria is not memory, it is expensive noise.

Reason 3: the window empties anyway

Even if the cost did not hurt and the reading were perfect, the window remains temporary. Tomorrow is another conversation and the desk wakes up clean. Any strategy based only on context starts from zero every session. The operation's accumulated learning (what worked, what the client asked for, what was decided) evaporates.

The conclusion is uncomfortable but liberating: there is no context window large enough to replace memory. They are different in nature. Context is where the current reasoning happens; memory is what survives between reasonings. Whoever understands that difference stops buying window and starts building an archive.

The four levels of an AI's memory

The science that studies agent memory went looking for its map in a predictable place: human memory. And the engineering went looking for its mechanism in another predictable place: the computer. The most influential synthesis came in 2023, when Berkeley researchers published MemGPT (Packer et al., 2023), a paper that proposed treating the AI model the way an operating system treats a computer's memory: a fast, small memory (the context window) and a large, cheap memory (external storage), with the model itself deciding what moves up and down between the two. The idea sounds simple. It was a watershed, and it is the foundation of practically everything that exists today in agent memory.

In practice, a well-built AI organizes what it knows into four levels:

Level 1, working memory. The context window, the desk. It holds the current conversation and whatever was pulled from the archive for the task at hand. Fast, expensive, temporary.

Level 2, session memory. The living summary of the conversation in progress. When the talk gets too long to fit on the desk, the system condenses what already happened ("the client asked for X, Y was decided") and keeps only the summary. It is the day's notepad.

Level 3, long-term memory. The real archive: a database outside the model, which survives across sessions, across days, across months. This is where the persistent memory in this post's title lives, and the next several sections are entirely about this level.

Level 4, the model's frozen knowledge. What the model learned during training. Vast, but frozen in time and generic: it knows nothing about your company and cannot be updated without retraining. Useful as general culture, useless as the operation's memory.

Within level 3, research distinguishes three types of content, and the distinction matters because each type calls for a different expiration rule (hold on to that sentence, it comes back in the TTL section):

Episodic memory: events. "On Tuesday, client So-and-so complained about the delivery deadline and asked for a refund." It is the operation's diary.

Semantic memory: distilled facts. "Client So-and-so is deadline-sensitive." Notice that this is not an event, it is a conclusion extracted from several events. It is the enriched record, the consolidated knowledge.

Procedural memory: ways of doing things. "A deadline complaint gets answered within 2 hours, offering tracking before offering a discount." These are the rules and processes the operation has learned.

A mature agent converts one type into another all the time: repeated episodes become a semantic fact ("three deadline complaints this month, this client is deadline-sensitive"), repeated facts become procedure. It is the same path a human employee walks between day one and the moment they "learn how the house works". The difference is that, in AI, this path does not happen on its own: it needs tools, expiration rules and a safe place to live. Those three things are the rest of this post.

What TTL is and why memory without an expiration date becomes a junk room

TTL stands for Time To Live. It is a number attached to each piece of stored information saying how long it remains valid. Once the deadline passes, the information expires: it is deleted or flagged as no longer trustworthy, automatically, without anyone having to remember to do the cleaning.

The concept was not born with AI. It has held the internet up for decades: the system that translates site names into addresses (DNS) uses TTL to decide how long an answer can be reused before asking again; the caching systems that make websites load fast (like Redis, which reappears in the tools section) have TTL built into every record. It is mature, cheap, battle-tested technology. The novelty is applying that discipline to an AI's memory.

The household analogy is food expiration. Your fridge holds milk that expires in days, preserves that expire in years and salt that never expires. Nobody in their right mind would apply the same expiration date to all three. And nobody would store food with no label at all, because in three months the fridge becomes a place where you trust nothing you open. AI memory without TTL is exactly that fridge: it grows without stopping, mixes the fresh with the spoiled and contaminates new decisions with old information.

What happens without TTL (and why you only notice late)

The cruel detail is that expired memory gives no warning. It does not throw an error, does not crash the system, does not show up in a log. It just makes the AI answer, with full confidence, something that was true in March: the old price, the stock that ran out, the manager who left the company, the exchange policy that changed. The symptom arrives months later, disguised as "the AI hallucinates", when the correct diagnosis was "the AI remembered something nobody told it to forget". And then the typical reaction is to switch off memory entirely, throwing out the asset along with the liability.

Without expiration, memory also bloats. Bloated, search gets slower and less precise (more similar candidates competing for every query), storage and retrieval costs rise, and every piece of wrong information gains an infinite lifespan. Memory without TTL is not an archive, it is a storage room where nobody ever throws anything away. Every room like that ends the same way: nobody finds anything anymore and nobody trusts what they find.

The TTL policy: each type of memory has a natural shelf life

Remember the distinction between episodic, semantic and procedural memory? This is where it pays for its ticket. A serious TTL policy does not set a single deadline, it sets deadlines by the nature of the information:

  • Volatile operational data (stock, queue, order status): valid for minutes or hours. Once past, it expires. This kind of data should not even become long-term memory; it belongs to the company's systems, and the AI queries it live.
  • Episodic memory (what happened in each interaction): valid for weeks or months. The raw episode loses value over time; what it teaches should be distilled before it expires.
  • Semantic memory (facts about clients, suppliers, products): long validity, months to years, with renewal. A reconfirmed fact stays alive; a fact nobody has touched in a year deserves review or expiration.
  • Procedural memory (the house rules and processes): no automatic expiration, but with an owner and periodic review, like any internal policy.
  • Sensitive personal data: here TTL stops being good practice and becomes a legal obligation. Hold that thought, because the data protection section comes back to it.

Fixed TTL, renewable TTL and the forgetting curve

There is a refinement that brings the machine closer to the human. In 1885, the German psychologist Hermann Ebbinghaus experimentally measured how human memory decays: exponentially, fast at first, slower afterwards. And he discovered the antidote: every time a memory is used, the curve resets and forgetting slows down. What is used, remains; what is not used, fades. One hundred and forty years later, that is exactly the logic of renewable TTL: every time a memory is retrieved and proves useful, its expiration date is extended. The memory of the client who buys every week never expires, because it is reconfirmed every week. The memory of the quote that went nowhere expires on its own in 90 days, with no meeting, no manual cleanup, no decision from anyone.

That is what TTL buys for your operation: forgetting on purpose. It sounds paradoxical to invest in memory and celebrate deletion, but any professional archivist will confirm it: an archive is only trustworthy because someone decides what leaves it. The AI that remembers everything forever is not the smartest one in the room; it is the one answering with 2024 prices in the middle of 2026.

How to use memory from outside the context window without flooding the prompt

We have arrived at the central engineering problem of memory, the reason this post exists. Long-term memory lives outside the model, in a database. The model only reasons over what is in the context window. Therefore, at some point, memory has to be brought from the archive to the desk. The question that separates good systems from expensive, dumb ones is: how much to bring, and how to choose?

The wrong answer is the most tempting one: bring everything, or bring too much "just in case". Whoever does that recreates the junk room from the previous section inside the prompt, and also runs straight into the "Lost in the Middle" effect from the first section: paying dearly for the model to read poorly. Flooding the prompt with memory is the equivalent of answering a simple question from your boss by dragging four boxes of dead files onto his desk. The answer is in there, somewhere. That is not how you answer a question.

Retrieval on demand: memory comes when called

The correct architecture inverts the flow. Instead of pushing the entire memory into every conversation, the system retrieves on demand: for each question, it searches the archive for only the items relevant to that question, and injects only those items into the prompt. The search uses the same semantic similarity mechanics we explained in detail in the post about RAG and vector databases: the question is converted into a numeric vector representing its meaning, and the database returns the memories with the closest meaning, even if the words are different.

But closeness of meaning, on its own, is not enough. A memory can resemble the question and still be old, irrelevant or trivial. The scientific work that best solved this ranking was Generative Agents, from Stanford (Park et al., 2023), the famous experiment with 25 agents living in a simulated small town. To decide what each agent should remember at each moment, the authors combined three multiplied scores:

  • Relevance: how much the memory has to do with the current situation (the semantic similarity);
  • Recency: how fresh it is, with the weight decaying over time (Ebbinghaus again, turned into a formula);
  • Importance: the intrinsic weight of the fact. "Client threatened to terminate the contract" weighs more than "client prefers email over phone", even though both are true.

The result of the combination decides which memories reach the desk and in what order. It is a mechanism simple to describe and powerful in practice, and it became the reference standard for memory retrieval in agents. Notice how TTL and ranking complete each other: TTL decides what still exists in the archive; the score decides what, among the living, deserves the desk right now.

Distill before storing, summarize before injecting

The second anti-flooding mechanism happens earlier, at write time. A naive system stores the entire raw conversation; a mature system distills: it extracts from the conversation the facts that deserve to survive ("prefers bank slip", "the decision maker is the partner", "agreed deadline: the 15th") and stores each fact as a small, updatable record with its own expiration. Recent memory architecture work, such as Mem0 (2025), measured the effect of this discipline of extracting, consolidating and retrieving only the distillate: more accurate answers with a fraction of the tokens per conversation, compared with dumping the full history into the window. Less volume, more accuracy, precisely because the model reads little and reads right.

The same applies in the direction of the ongoing session: when today's conversation grows, the system condenses the closed chapters into a short summary (the progressive summarization of level 2), keeping the desk lean without losing the thread.

Context budget: memory has a ceiling

Finally, the management rule that ties it all together: retrieved memory enters the prompt under a fixed budget. A healthy range in production is to reserve somewhere between 10% and 20% of the window for memory, and to treat that ceiling as law: if the search returned more than fits, cut by score, the best get in, the rest stays in the archive. Without a ceiling, memory grows until it occupies the whole window, and you are back to the buried-desk scenario, paying extra tokens for a worse answer.

Notice what this design delivers as a whole: an archive that can grow to millions of records without the prompt growing a single byte. The context window stays the size of the task, always. The archive stays the size of the company's history, always. Each in its place, and the cost per answer stops climbing along with the size of the memory. That is the difference between having memory and having a bloated context.

The memory control tools: the AI that manages what it remembers

So far we have talked about principles. Now comes the part that usually surprises outsiders: in a modern agent, the one operating the memory is, to a large extent, the AI itself. The system hands the model a set of memory tools (in the jargon, "tools": functions the model can call while reasoning, the same mechanism it uses to query a system or send an email), and the model decides when to use each one. That was MemGPT's great insight: the model is not just the consumer of memory, it is its librarian.

The minimum set has five tools, and it is worth knowing each one by the decision it embodies, not by its technical name:

  • Write (memory_write): creates a new memory. The embedded decision: does this deserve to outlive the conversation? A good agent writes the distilled fact ("the decision maker is the partner, prefers WhatsApp"), not the transcript. And it writes with the label already attached: memory type, owner, expiration.
  • Search (memory_search): queries the archive by meaning, with the relevance, recency and importance ranking from the previous section. The embedded decision: what, from the past, helps right now?
  • Update (memory_update): corrects an existing memory instead of creating a duplicate. The embedded decision: is this a new fact or a new version of an old fact? It is the tool that prevents the archive from holding three conflicting records for the same client.
  • Delete (memory_delete): removes what turned out wrong, was revoked or should never have been written. The embedded decision: is this still true? A "the client cancelled that order" should delete (or correct) the order's memory, not coexist with it.
  • Set expiration (memory_expire, or the TTL passed at write time): labels each memory with the policy's deadline. The embedded decision: until when is this valid without reconfirmation?

If the list sounds familiar, that is because it is the CRUD of any record-keeping system (create, read, update, delete) plus expiration. The revolutionary difference is not in the operations, it is in who performs them: a language model judging, at every turn of the conversation, what deserves to be remembered, corrected or forgotten. It is automated editorial judgment, and that is why the instructions given to the agent about memory (what to write, what never to write, when to update instead of create) matter as much as the database where the memory lives.

Here a contrast is due, one that prevents an expensive mistake: memory is not a log. Your operation already records everything: conversations, tickets, emails, call recordings. That is the raw record, and it should keep existing for auditing. Memory is something else: it is the editorial layer on top of the record, the chosen, distilled facts, with an owner and an expiration. Confusing the two and "giving the agent memory" by pointing it at the entire log is delivering the four boxes of dead files onto the desk with every question. The log remembers everything and understands nothing; memory understands because it chooses.

What runs inside your walls: the memory stack

The good news of this entire section fits in one sentence: everything this post has described runs on your own server, with open, mature software. There is not a single piece of the memory architecture that requires a third party's cloud. Anyone who has read our post about which model runs on your machine knows the model itself already runs in-house; the memory, all the more so, because it is the lightest part of the set.

The pieces, by the role they play:

  • The vault of facts: a relational database, typically PostgreSQL, open software that already underpins half of the world's corporate systems. With the pgvector extension, the same database stores the meaning vectors used in semantic search. For those who want dedicated, high-volume vector search, Qdrant (also open, also self-hostable) is the specialist.
  • The fast memory with native expiration: Redis, the most widely used caching software on the planet, where TTL is not an add-on feature, it is the natural way to write anything. It is the ideal home for session memory and volatile data: write with a deadline, and forgetting happens on its own, at the infrastructure level, without depending on anyone running a cleanup.
  • The librarian: the layer that implements extraction, consolidation, ranking and the five tools from the previous section. You can build it custom (which is what we do when the operation calls for it) or start from open projects that package this role, such as Mem0, Letta (the evolution of MemGPT into a product) and Zep, all with self-hostable versions.

Notice the pattern: relational database, cache with TTL, vector search. None of these three pieces is exotic, expensive or experimental. They are technologies with decades on the road (or, in the case of vector databases, with years of heavy production), operable by any competent infrastructure team, running on modest hardware next to what the model itself demands. The sophistication of persistent memory is not in buying rare tooling; it is in the design: the TTL policy, the distillation rules, the context budget. Tools get installed in days. Judgment is what separates the trustworthy archive from the junk room.

And there is a strategic consequence in this stack that goes unnoticed: since the pieces are open and market standards, memory built this way does not lock you into any AI vendor. The model can be swapped (an open model today, a better one tomorrow) and the archive remains intact, because it lives in your databases, in your format. Invert the scenario: memory accumulated inside a vendor's proprietary platform is forced loyalty. The better their memory works, the more expensive it becomes to leave. Memory is, in the long run, the asset; the model is a replaceable part.

Memory is the most sensitive data your company will ever produce

Now the question in this post's title: why does this entire memory have to be on-premise (running on your server, inside your infrastructure), and not on a third party's AI platform?

Because an agent's memory is not data like any other. Think about what it contains after a year of operation: the distilled history of every client, who decides and who influences in each account, who is price-sensitive and who is deadline-sensitive, which negotiations stalled and why, which internal rules the operation actually follows (not the manual's rules, the practiced ones). No system of yours holds that portrait. The CRM has the fields somebody filled in; the agent's memory has the conclusions. It is the distilled version of the company's commercial and operational intelligence, the document a competitor would most love to read.

When the AI lives in the cloud, every fragment of that memory is born, travels and resides on a third party's server, in another country, under another law, inside a contract you accepted without negotiating and that can change. We have already shown how to have AI without leaking company data and why an agent that does not send data to OpenAI stopped being paranoia and became a requirement. The argument gains another order of weight applied to memory: a leaked prompt exposes one conversation; a leaked memory exposes the accumulation of all of them. The prompt is the drop; the memory is the aquifer.

Data protection law: TTL as a compliance tool

And there is the legal dimension, which in Brazil has a name: LGPD, the Brazilian General Data Protection Law (the sibling of Europe's GDPR). Two of its principles speak directly to this post. The first is storage limitation: personal data may only be kept for as long as necessary for the purpose that justified collecting it. The second is the right to erasure: the data subject can demand their data be deleted, and the company has to be able to comply.

Reread those two principles with this post's eyes and the conclusion imposes itself: the law demands exactly what TTL implements. Limited retention is TTL by policy; the right to erasure is the delete tool with an audit trail. A memory designed as we have described (every record with an owner, a type, an expiration and an audit trail) is not just good engineering, it is legal compliance built into the architecture. Memory dumped into a third party's platform, on the other hand, turns every erasure request into a support ticket to another country: you have to trust that the vendor deletes it, with no way to verify. Before the law, the one who answers is you, not them. On-premise, deleting is a command of yours, on your database, with a record that it was done. The difference between the two situations is the difference between complying with the law and rooting for it.

If the cost of this infrastructure is your objection, we have already done that math in detail in the post about how much on-premise AI costs and when it beats the cloud. Honest summary: for intense, daily use, which is precisely the scenario where persistent memory generates the most value, the math closes in months. Memory and on-premise are not two decisions; they are the same one.

Reference architecture: the memory cycle

Putting it all into a single picture. A serious persistent memory is a cycle of seven stages, running continuously:

  • 1. Capture: the conversation or task happens and the raw material becomes available (and the log, remember, keeps existing separately, for auditing).
  • 2. Extraction: the librarian distills from the raw material the facts that deserve to survive, few and small.
  • 3. Consolidation: each new fact is checked against the archive. Already exists? Update. Conflicts? Resolve (or flag for review). Repeated three times? Maybe it is time to become a semantic fact or a procedural rule.
  • 4. Labeled storage: the fact enters the database with type, owner, importance and the policy's TTL. Nothing enters without a label.
  • 5. Expiration: the clock runs on its own. What expired leaves; what was reconfirmed renews. It is the stage nobody sees and the one that keeps the archive trustworthy.
  • 6. Retrieval: a new question arrives, the search ranks by relevance, recency and importance, and the context budget cuts at the ceiling.
  • 7. Injection and feedback: the chosen memories enter the prompt, the model answers, and the usage feeds the cycle back: memory that helped renews its expiration and gains importance; memory that is never used ages in peace until it expires.

And like every management cycle, it is only serious if it is measured. Three numbers tell the health of the memory: the utilization rate (of the memories injected into the prompt, how many did the answer actually use? too low points to bad search or a bloated archive), the hit rate (of the questions that had an answer in the archive, in how many did the search bring the right memory?) and the prompt share (how much of the window is memory occupying? if it grows, the budget has a hole). These are the same instincts from our post on AI observability: what is not measured rots in silence, and memory rots more silently than anything.

The five mistakes that kill an agent's memory

Closing the technical part, the anti-patterns we see most often, each with the symptom that gives it away:

Mistake 1: storing everything. Pointing the memory at the log and recording entire conversations. Symptom: the search returns ten similar excerpts and none useful, and the cost per answer climbs every month. Memory is editorial: if everything is important, nothing is.

Mistake 2: never expiring. Memory without TTL, growing since day one. Symptom: the AI confidently answers things that were true last semester, and the team starts to "not trust its memory", effectively switching the whole feature off.

Mistake 3: injecting everything. No context budget, dumping every related memory into the prompt. Symptom: slower, more expensive, worse answers, the Lost in the Middle paradox in production. More memory on the desk, less memory in the answer.

Mistake 4: memory with no owner. No policy for who can write, who can delete, which types exist, what expiration each one has. Symptom: triplicated, conflicting records, and the question "why does the AI think that?" with no possible answer. Memory without governance is graffiti, not an archive.

Mistake 5: memory without auditing. Not recording when each memory was created, used, changed, expired. Symptom: the day a client demands their data be erased (their right) and nobody can prove what was deleted. Without a trail, memory becomes a legal liability sleeping on the server.

The five mistakes share the same root: treating memory as a technical feature you switch on, instead of an asset you govern. The technology of memory, as the stack section showed, is the easy part. The policy is the product.

The AI that remembers is a different category of tool

Go back to the employee in the first paragraph, the brilliant one with amnesia. This entire post has been about curing him. And the cure, worth repeating the map, was nowhere exotic: an archive outside the context window, an expiration date on every memory, five librarian tools, a three-factor ranking to decide what rises to the desk, a budget ceiling so the desk does not sink, and all of it living on your server, under your law, with your key.

What changes when it works is the category of the relationship. AI without memory is a tool: every use starts from zero, the value is that of the one-off task. AI with governed memory is closer to a colleague: every month of work leaves the operation smarter than the month before, and that accumulation (the archive, not the model) becomes a company asset that no vendor swap takes away. That is why a private AI agent and persistent memory are the same subject told from two angles: there is no agent that learns without memory that persists, and there is no corporate memory that can, responsibly, live in somebody else's house. The right question for the coming years is not "which model to use?". Models come and go, and your data will remain yours. The right question is: who owns your AI's memory?