How to beginner · 3 min read

How to use Semantic Kernel memory

Quick answer
Use Semantic Kernel memory by creating a MemoryStore (like VolatileMemoryStore), then add and retrieve context via kernel.memory APIs. This enables your AI to remember and reuse information across interactions.

PREREQUISITES

  • Python 3.8+
  • pip install semantic-kernel
  • OpenAI API key (free tier works)
  • pip install openai>=1.0

Setup

Install the semantic-kernel Python package and set your OpenAI API key as an environment variable.

  • Install Semantic Kernel: pip install semantic-kernel
  • Set environment variable: export OPENAI_API_KEY='your_api_key' (Linux/macOS) or setx OPENAI_API_KEY "your_api_key" (Windows)
bash
pip install semantic-kernel

Step by step

This example shows how to create a Kernel, add a VolatileMemoryStore for memory, and store and retrieve data.

python
import os
import semantic_kernel as sk
from semantic_kernel.connectors.ai.open_ai import OpenAIChatCompletion
from semantic_kernel.memory.memory_store_volatile import VolatileMemoryStore

# Initialize kernel
kernel = sk.Kernel()

# Add OpenAI chat completion service
kernel.add_service(OpenAIChatCompletion(
    service_id="chat",
    api_key=os.environ["OPENAI_API_KEY"],
    ai_model_id="gpt-4o-mini"
))

# Create and set volatile memory store
memory_store = VolatileMemoryStore()
kernel.memory.register_memory_store(memory_store)

# Add memory collection
collection_name = "my_memory"
kernel.memory.create_collection(collection_name)

# Save a memory item
memory_key = "user_favorite_color"
memory_value = "blue"
kernel.memory.save_information(collection_name, memory_key, memory_value)

# Retrieve the memory item
retrieved = kernel.memory.get_information(collection_name, memory_key)
print(f"Retrieved memory: {retrieved}")
output
Retrieved memory: blue

Common variations

You can use different memory stores like FileMemoryStore for persistent memory or integrate with vector databases for semantic memory. Async usage is supported by Semantic Kernel but requires async-compatible memory stores. You can also combine memory with prompt templates for dynamic context injection.

Troubleshooting

  • If memory retrieval returns None, ensure the collection and key exist and that the memory store is registered.
  • For API errors, verify your OPENAI_API_KEY is set correctly and the model ID is valid.
  • Use VolatileMemoryStore only for testing; switch to persistent stores for production.

Key Takeaways

  • Use VolatileMemoryStore for in-memory context storage during development.
  • Register your memory store with kernel.memory.register_memory_store() before use.
  • Persist memory with file or vector stores for production use cases.
  • Combine memory with AI chat completions for context-aware conversations.
  • Always verify environment variables and model IDs to avoid runtime errors.
Verified 2026-04 · gpt-4o-mini
Verify ↗