Skip to content
AgentLabs.

Improving AI agent memory performance: controlling context length

Improving AI agent memory performance: controlling context length

When an AI agent starts remembering you: why it is exciting and why it is dangerous

A few months ago, a friend told me he had built himself a small AI agent, a personal assistant that remembered tasks, project ideas and even things he said in the middle of the night. “Suddenly,” he told me, “there is one machine in the world that knows more about me than any colleague I have worked with for a decade.” Then he paused, thought for a moment and added: “But every time I give it another memory, it becomes slower. As though memory is holding it back.”

In a way, that sentence captures one of today's hottest AI agent dilemmas: how do we build long-term memory, or agent memory, that accompanies us over time, understands us and puts us in context, without harming performance, collapsing under load or turning every conversation into slow, frustrating ping-pong?

This article does not claim to offer a perfect recipe. There is none. It tries something else: to unpack the tension between memory and performance, show where the technical complexity really lies, where the discussion becomes somewhat mythical and what we can learn from real projects, abroad and in Israel, trying to build AI agents that feel more like people who remember and less like chatbots with conversation histories.

Why do we need memory? An AI agent without agent memory is essentially a polished chatbot

Let us clear away the noise. Large language models, the LLMs behind every fashionable AI agent, do not actually remember you. They receive a long prompt, process it and return an answer. That is it. Without additional mechanisms around them, all the information mentioned in a conversation disappears once the request ends.

What has made agent memory so central is the desire to turn this interaction from a series of one-off questions and answers into an ongoing relationship. An AI agent recognises that you are returning to the same subject, knows which projects you are working on and even understands your working style or how you make decisions.

The psychological gap: why we expect a machine to behave like a person

There is an emotional dimension here, alongside technology. Once we speak to an AI agent through text, it enters the part of our brain reserved for people. We begin expecting it to remember who we are, what we said and the advice it gave yesterday. When it does not remember, it feels like a particular kind of trust has been broken.

But saying memory in this context actually means discussing several different layers:

Common memory layers in a modern AI agent

Without making the classification overly rigid, several types recur in many systems:

1. Short-term memory: the immediate context of the latest conversation or few conversations. 2. Medium-term memory: things important over the coming weeks, such as an active project, an open task or a document draft. 3. Long-term memory: preferences, professional background, values and decision history. 4. Factual memory: documents, data and recorded organisational knowledge that the AI agent can retrieve.

The more types of memory we load onto an AI agent, the more human it feels. It remembers last month's conversation about the start-up, knows you dislike overly formal emails and does not suggest technology you have already said is irrelevant. But every memory layer costs money, computing time and a risk of disorder.

Where the trouble really begins: the hidden price of long-term memory

At first glance, it looks simple: save everything. There is room in the cloud. Record every AI agent interaction, push it into some database and retrieve it when needed. In practice, this is a dangerous game. As memory grows, so do search time, retrieval costs and the likelihood of returning something irrelevant.

Context is king, but it costs money

For an AI agent to use agent memory intelligently, it must decide every time which parts of the past matter now. That is not straightforward.

When models were relatively small, an almost naïve approach addressed this: push large portions of history into the prompt and hope the model manages. Today, with projects running thousands of daily interactions, that no longer holds up.

Every additional memory passage in an AI agent's context adds tokens. More tokens mean:

- More money for every model call. - More waiting time. - A greater chance of the model becoming distracted instead of focusing on the current question.

Surprisingly, sometimes less memory makes an AI agent feel more intelligent to the user, simply because it is more accurate and faster.

Indexes, embeddings and all the impressive words

Most serious agent memory systems do not store texts simply as they are. They convert them into embeddings, mathematical vector representations that measure similarity between ideas rather than words. These are stored in a special database, usually some kind of vector database, and searched for similar memories.

It sounds wonderful until you live with it every day. The more memories there are, the heavier the search becomes, requiring more optimisation, maintenance and tuning. If the AI agent serves hundreds of users, agent memory can quickly become the main bottleneck.

Suddenly, the problem is not the model itself, but the index around it: code, infrastructure, networking, server memory and even database version management.

Building an AI agent with memory without shooting ourselves in the foot

What should we do? Keep memory, but avoid drowning the system in it. This section touches on several principles, not as a precise recipe, but as good character traits for an AI agent that wants to remember.

1. Memory is a resource, not a rubbish bin

The most important starting point is that agent memory is not an automatic archive of everything. It is a resource. You must decide what enters it and what does not.

An intelligent AI agent does not save every message. It decides:

  • Is there new information about the person or organisation?
  • Will we return to this in future: an ongoing project, preference or fundamental decision?
  • Is it merely passing noise or small talk that adds no knowledge?

Many teams do this in two layers:

A summary layer, followed by a decision layer

Instead of pushing every conversation line into memory, an AI agent summarises the conversation, or part of it, in a sentence or two: “The user is developing a SaaS product for the American market, currently at the MVP stage.”

Only this summary, rather than the entire lengthy dialogue, is considered for agent memory. Even then, another filter applies: is this repeated information? Does a memory card like this already exist, and would updating it be better than adding a new one?

2. Memory needs an expiry date, or at least a periodic review

People forget. That is a feature, not a bug. In AI agents, controlled forgetting can be one of the strongest tools for maintaining performance.

Agent memory can define:

  • Short memories lasting days or weeks, such as upcoming meetings and tactical tasks.
  • Long-term memories concerning values, preferences and biography.
  • Temporary memories deleted if they have not been used several times.

One increasingly common technique updates a freshness measure every time a memory is retrieved and influences the AI agent's answer. Memories not retrieved for months become candidates for deletion or archiving.

This is not merely computationally efficient. It also protects privacy, reduces legal risks and helps agent memory remain focused on what matters now, rather than what mattered two years ago.

3. Not every request needs memory access

One surprisingly overlooked point is that not every AI agent interaction requires a deep scan of agent memory. If the question is “What does RAG mean?”, there is no need to examine the user's personal history.

To maintain performance, many systems classify the request first:

Step 1: is the question personal or general?

If general, go directly to the model, perhaps supplemented by a general knowledge base, without touching personal memories. If personal, activate agent memory retrieval and incorporate the result into the prompt.

This stage can be inexpensive and fast: a small model, a business rule or even simple regular expressions in some cases. The result is fewer vector database calls, less load and greater speed.

4. Separate memories about people from memories of documents

Another recurring confusion is mixing memory about the user with memory of content. That is not a good idea.

A typical AI agent can manage several information layers simultaneously:

  • A user profile: preferences, personal information and communication style.
  • Conversation history, mainly for immediate context.
  • A knowledge base: documents, procedures, PDF files and business data.

Mixing all this into one large index is a recipe for disorder. Suddenly, a question such as “What is the new policy?” retrieves a memory that the user dislikes video calls, rather than an actual policy document.

A healthy AI agent keeps several separate agent memory layers, with different retrieval mechanisms, and asks which type of memory it actually needs now.

Israel on the map: AI agents entering local organisations

Look beyond research laboratories and heavily funded start-ups, and an interesting pattern appears in Israel's market. Organisations want AI agents, but fear memory, particularly organisations handling sensitive healthcare, financial and public-sector information.

The regulator does not like infinite memory

I asked an information systems manager at one of Israel's largest insurers why progress on an internal AI agent was so slow. The answer was honest: “The problem is not writing code, but deciding what to keep, for how long and where.”

GDPR, Israeli regulations and regulators' instructions all say more or less the same thing: do not retain personal information indefinitely when you do not have to keep it.

Now consider agent memory built without a clear policy. It collects memories about employees, customers and suppliers, but responsibility for deletion, the deletion method and how to prove that information has actually been deleted if somebody eventually asks are not always clear.

What is actually happening in Israel?

In some organisations, the common pattern is fairly conservative:

  • An internal AI agent with document access, but no personal agent memory for each user.
  • A short-lived cache, providing memory for hours or days, perhaps for the same task.
  • Small pilots building personal agent memory for a limited group, under internal regulatory oversight.

Israeli start-ups, by contrast, take more risks. There, we see more advanced agent memory systems, with rich user profiles, tracked preferences and even emotional analysis over time.

Even there, however, somebody almost always eventually asks, “What happens if a customer asks us to delete everything?” That brings us back to the fundamentals: designing a memory schema, managing deletion and being transparent with users.

Questions and answers: what people really want to know about agent memory

How do I know whether an AI agent needs long-term memory at all?

If most questions you ask the AI agent are technical, general and one-off, full agent memory may well be unnecessary.

For an ongoing process, however, such as supporting a project, personalised learning, coaching, task management or customer service lasting months, memory moves from a nice-to-have to a real need.

Does every AI agent benefit from personal memory?

No. Some AI agents depend on the absence of personal memory in areas such as anonymous advice, initial care or pure information search.

In these cases, very short memory may suffice for the current conversation's context, with everything deleted at the end. This also solves privacy problems, simplifies maintenance and prevents unpredictable behaviour based on old memories.

How do you handle memory errors that an AI agent carries forward?

One fascinating, and genuinely not fully solved, problem arises when agent memory learns something incorrect, such as a mistaken assumption about the user, and repeatedly reinforces it.

A possible approach:

  • Give users a transparent interface for viewing and editing some memories: “The AI agent thinks you are… Is that correct?”
  • Add a review layer within the model itself, detecting contradictions between memories and what is being said now.
  • Avoid treating memory as absolute fact. Treat it as information to cross-check against current context.

Is agent memory always stored in the cloud?

Not necessarily. There is a growing trend towards local AI agents trying to keep some memory locally, on the device itself, an on-premises server or a private environment separated from the public cloud.

This requires more complex infrastructure, but gives organisations, and sometimes individuals, greater control over where their sensitive information flows.

Table: key points about agent memory and performance

TopicChallengeCommon solution directionEffect on performance
Memory volumeGrowing memory slows the system and makes every call more expensiveSummarise conversations; keep the essence rather than all textReduces prompt tokens and eases indexing
Memory relevanceUnrelated retrieved memories confuse the modelEmbedding-based retrieval and contextual filteringImproves answer quality; requires retrieval optimisation
Controlled forgettingOld, unused memory consumes resources without benefitDelete or archive according to age and disuseReduces database size and prevents long-term slowdown
Privacy and regulationLegal risks from retaining personal information for a long timeLimited retention policies, the right to deletion and transparencyAdds management complexity and reduces risks
Request classificationMemory retrieval for general queries is unnecessary and expensiveSeparate general and personal questions before retrievalShortens response time and reduces index load
Separating memory typesMixing user profiles with business documentsSeparate databases for personal memory, documents and chat historyImproves retrieval accuracy; requires careful architecture
Updating memoryIncorrect or outdated memories damage trustA user editing interface and model-based contradiction detectionImproves quality over time and increases logical complexity

What next? An AI agent as a companion, not merely a gadget

Returning to the friend with his home-built AI agent, he eventually did something counterintuitive: reduced its memory. He kept only what really affected daily life: business decisions, medium-term goals and a few important personal details. Everything else, he let the machine forget.

A few weeks later, he told me the experience had improved. “It no longer remembers every silly thing I said at three in the morning,” he laughed, “but it does remember my important project this year. That is what matters.”

Perhaps this is where the agent memory discussion lands. The aim is not an entity that remembers us forever and knows everything. It is an AI agent that acts as a partner: prioritising, filtering, forgetting when necessary and investing its limited resources, and our money, in what actually creates value.

Long-term memory is only part of the story. The right combination of prompt engineering, intelligent retrieval, controlled forgetting and architectural planning determines whether an AI agent becomes another pleasant application used for a fortnight and forgotten, or working infrastructure that accompanies us for years.

If you are considering introducing an AI agent into your organisation or building personal agent memory tailored to your product, pause before filling the cloud with everything. Discuss the balances, risks and opportunities. We would be happy to help with an initial consultation at no cost, simply to map together what actually needs remembering and what might, surprisingly, be better forgotten.

From reading to a pilot

Connect the agent to your knowledge sources

We will map where information is stored, who may access it and how to check an answer against its source.