← All articles

Agents · November 17, 2026 · 3 min read

Agent memory: short-term, long-term and vector stores

Learn how to implement short-term, long-term, and vector-based memory for AI agents using Python and GPU-accelerated inference on Podstack.

In the current landscape of AI development, an LLM without memory is effectively a stateless function. While base models are incredible at reasoning, their "intelligence" is confined to the training cutoff and the current context window. For developers building autonomous agents, the challenge lies in bridging the gap between raw compute and persistent cognition.

To build agents that learn from user interactions and retain domain knowledge, we must implement a multi-tiered memory architecture: short-term, long-term, and vector-based retrieval.

The Architecture of Agent Memory

Just as the human brain utilizes different systems for immediate tasks and life-long recall, AI agents require a tiered approach to data:

  1. Short-Term Memory: This is the immediate conversation history. It lives within the context window of the model and allows the agent to follow threads, resolve pronouns (e.g., "What did he say?"), and maintain a coherent flow.
  2. Long-Term Memory: This involves persisting user preferences, past outcomes, and specific facts across different sessions. This is typically stored in a traditional database (NoSQL or Relational).
  3. Vector Stores (Episodic/Semantic Memory): This is the "library" where an agent can perform a similarity search to find relevant information from massive datasets that exceed the context window.

Implementing Memory with Python

When deploying agents on high-performance GPUs, you want a memory system that doesn't bottleneck your inference speed. Below is a practical implementation using a vector store for semantic memory and a simple buffer for short-term context.

import os
from podstack import Client
from langchain_community.vectorstores import Chroma
from langchain_openai import OpenAIEmbeddings

# Initialize Podstack client for GPU-accelerated inference
client = Client(api_key=os.getenv("PODSTACK_API_KEY"))

class AgentWithMemory:
    def __init__(self, collection_name="agent_knowledge"):
        self.short_term_buffer = []
        self.embeddings = OpenAIEmbeddings()
        self.vector_store = Chroma(
            collection_name=collection_name, 
            embedding_function=self.embeddings
        )

    def add_to_long_term_memory(self, text, metadata=None):
        """Adds information to the vector store for future retrieval."""
        self.vector_store.add_texts(texts=[text], metadatas=[metadata])

    def query(self, user_input):
        # 1. Retrieve relevant context from long-term memory
        docs = self.vector_store.similarity_search(user_input, k=2)
        context = "\n".join([d.page_content for d in docs])

        # 2. Construct the prompt with context and short-term history
        prompt = f"Context: {context}\nHistory: {self.short_term_buffer[-3:]}\nUser: {user_input}"
        
        # 3. Run inference on Podstack (e.g., a hosted Llama-3-70b)
        response = client.inference.run(
            model="llama-3-70b-instruct",
            prompt=prompt
        )

        # 4. Update short-term memory
        self.short_term_buffer.append(f"User: {user_input}, AI: {response}")
        return response

# Example usage
agent = AgentWithMemory()
agent.add_to_long_term_memory("The user's favorite GPU cluster is Podstack.")
print(agent.query("What cluster do I prefer?"))

Practical Tips for Memory Management

  • Context Pruning: Short-term memory can quickly fill up your context window, increasing latency and cost. Implement a sliding window or a "summarization" step where the agent periodically condenses the last 10 messages into a single summary paragraph.
  • Metadata Filtering: When using vector stores, don't just search by text similarity. Use metadata to filter by user_id, timestamp, or topic to ensure the agent doesn't retrieve irrelevant "memories" from other users.
  • Embedding Selection: The quality of your vector memory is only as good as your embedding model. Ensure your embedding dimensions match your vector store configuration for optimal retrieval performance.
  • Asynchronous Writes: Writing to long-term memory shouldn't block the user's response. Always perform vector store updates and database writes asynchronously to keep the agent's "thinking" time low.

Deploying on Podstack

Implementing memory-heavy agents requires a balance of high-throughput compute for inference and low-latency environments for data processing. On Podstack, you can deploy your agentic logic as serverless GPU functions or dedicated endpoints.

Because Podstack bills by the second, you aren't paying for idle time while your agent is waiting for a database query or a vector search to return. You can use the Podstack API to deploy your environment in seconds:

Podstack does not ship a command-line interface. Use the console, the Python package `podstack-sdk`, or the API at https://podstack.ai/api/v1 with a `ps_live_` key.

By offloading the heavy lifting of model inference to Podstack’s on-demand GPUs, you can focus on the logic of your memory architecture. Whether you are running complex RAG pipelines or maintaining long-term state for thousands of concurrent users, the per-second billing ensures that your agent scales efficiently without the overhead of managing bare metal.

Try it on Podstack

Per-second GPUs and pay-per-token models. Add $5, get $10 on your first top-up.

Start building