All Frameworks

Semantic Kernel Quickstart

Add persistent memory to Microsoft Semantic Kernel as a native plugin.

1Install & Configure

Install the AgenticMemory SDK alongside Semantic Kernel and set your API keys.

pip install agenticmemory-sdk semantic-kernel
export AGENTICMEMORY_API_KEY="amk_your_key_here"
export OPENAI_API_KEY="sk-..."

2Create a Semantic Kernel Plugin

Wrap AgenticMemory methods as kernel functions so the LLM can store, recall, and search memory natively.

import asyncio
from agenticmemoryai import AgenticMemory
from semantic_kernel import Kernel
from semantic_kernel.functions import kernel_function
from semantic_kernel.connectors.ai.open_ai import OpenAIChatCompletion

memory = AgenticMemory()

# Create a space for this kernel
space = memory.create_space("SK Agent", "sk-agent")
SPACE_ID = space["space"]["_id"]


class AgenticMemoryPlugin:
    """Persistent memory plugin for Semantic Kernel."""

    @kernel_function(name="store_memory", description="Store a message to long-term memory")
    def store_memory(self, content: str, role: str = "assistant") -> str:
        memory.store(SPACE_ID, role=role, content=content)
        return f"Stored to memory: {content[:50]}..."

    @kernel_function(name="recall_memory", description="Recall recent messages from memory")
    def recall_memory(self, limit: int = 10) -> str:
        result = memory.recall(SPACE_ID, limit=limit)
        messages = result.get("messages", [])
        if not messages:
            return "No messages in memory."
        return "\n".join(
            f"[{m['role']}] {m['content']}" for m in messages
        )

    @kernel_function(name="search_memory", description="Search memory for relevant past interactions")
    def search_memory(self, query: str) -> str:
        results = memory.search(SPACE_ID, query, scope="all", tags=[])
        hits = results.get("results", [])
        if not hits:
            return "No matching memories found."
        return "\n".join(h["content"] for h in hits)

    @kernel_function(name="set_context", description="Store a key-value pair in persistent context")
    def set_context(self, key: str, value: str) -> str:
        memory.set_context(SPACE_ID, key, value)
        return f"Context '{key}' set."


# Build the kernel
kernel = Kernel()
kernel.add_service(OpenAIChatCompletion(service_id="chat"))
kernel.add_plugin(AgenticMemoryPlugin(), plugin_name="memory")

# Bootstrap prior context from previous runs
ctx = memory.bootstrap(SPACE_ID, entries=5, messages=10)
prior = "\n".join(
    m["content"] for m in ctx.get("messages", [])
)

async def main():
    # Invoke the kernel with auto function calling
    settings = kernel.get_prompt_execution_settings_from_service_id("chat")
    settings.function_choice_behavior = "auto"

    result = await kernel.invoke_prompt(
        f"""You have access to a persistent memory system.
Prior conversation context:
{prior}

Store important facts and search memory when needed.
User: What did we discuss in our last session?""",
        settings=settings
    )
    print(result)

asyncio.run(main())

3Run & Verify

Execute the kernel and confirm memory is persisted across runs.

python sk_memory_agent.py
# Based on our memory, in your last session you discussed...
# [The kernel automatically calls recall_memory and search_memory]

# Run again — the kernel remembers the previous session
python sk_memory_agent.py
# I can see from our conversation history that we previously...

Free tier: 1 space, 1K messages/month. Upgrade to Pro ($24.99/mo) for semantic search + auto-summaries.

Help
Help

Click the icon on any page for context-sensitive help.

Quick Links