SabaKore

Home / Articles / AI guides

How Memory Grows When You Keep an AI Agent Running

The typical causes of memory use creeping up in an always-on AI agent, how to notice it, and how effective the simple safeguard of restarting is.

Ren, a junior infrastructure engineer

Ren

I've been keeping an AI agent running, and after a few days the VPS seems to get sluggish...
Procura, a cloud AI robot who explains

Procura

It's quite likely that memory is slowly growing. It's a common pattern: it's light right after you start it, but the longer it runs the more information it hoards, and by the time you notice, it's heavy.

Typical causes of growth

  • It keeps all the conversation history without any cutoff
  • It piles up processed content in memory as logs without limit
  • It moves on to the next job without properly cleaning up the space used in each one
Ren, a junior infrastructure engineer

Ren

How can I notice before it gets slow?
Procura, a cloud AI robot who explains

Procura

The best way is to be able to see memory usage over time. If you see a graph that keeps climbing to the right, that's a sign something is not being cleaned up somewhere.

The real fix and a stopgap

The right response is to find and fix the cause of the growth. But identifying the cause can take time. As a stopgap in the meantime, restarting the process on a schedule is simple but really does work in practice, because it resets everything that has piled up. Just don't forget that this is not a substitute for the real fix.

  • Make memory usage viewable as a time series
  • For conversation history, summarize the old parts or cut it off at a set amount
  • As a stopgap, set up a restart at fixed intervals
  • Don't paper over it with restarts; investigate the cause at the same time

Keep the monitoring light

It defeats the purpose if the mechanism that watches memory usage becomes heavy itself. If you keep recording the state at very short intervals, the records themselves start to strain memory and disk. Even recording once every few minutes is enough to catch an upward trend. Aim for monitoring that is light but continuous.

  • Recording intervals of a few minutes are often enough
  • Have old records deleted automatically so they don't pile up forever
  • Set it up so you're notified only when growth is abnormal
Procura, a cloud AI robot who explains

Procura

The longer something runs around the clock, the more these unglamorous safeguards pay off. Setting up a way to watch before flashy features lets you run it for a long time with peace of mind. The difference shows up after it has been running for a while, rather than right after you build it when it's still fresh.
Ren, a junior infrastructure engineer

Ren

If the agent stops while it restarts, isn't that a problem?
Procura, a cloud AI robot who explains

Procura

The stop is often only a few seconds to a few tens of seconds, so unless the use requires constant responsiveness, it isn't a big problem. If it bothers you, scheduling the restart for a time when there are fewer users makes it even safer to operate.

Don't rely too much on swap

When memory runs short, having swap space makes it look as if things are still running. But swap is a substitute that uses the disk, so the more you lean on it, the more noticeably processing slows down. Don't settle for 'it hasn't crashed, so it's fine'; check swap usage together with memory, and if it's in constant use, treat it as a sign to add memory or investigate the cause.

  • Check swap usage together with the memory trend
  • Don't assume that a state where swap is always in use is normal
  • Keep a stopgap swap setting separate from fixing the underlying memory shortage
Ren, a junior infrastructure engineer

Ren

So you notice by watching how it grows, and you treat restarts as a stopgap.

This article is translated from the Japanese original. If the two differ, the Japanese version is the one we keep up to date.