Add long-term memory to HuggingFace Smolagents with CortexDB.

Smolagents

CortexDB provides persistent long-term memory tools for HuggingFace's Smolagents framework, enabling CodeAgent and ToolCallingAgent to store, search, and forget memories across sessions.

Installation

pip install cortexdb-smolagents

Quick Start

from smolagents import CodeAgent
from cortexdb import Cortex
from cortexdb_smolagents import get_cortexdb_tools

client = Cortex(base_url="http://localhost:3141")
tools = get_cortexdb_tools(client, tenant_id="my-app")

agent = CodeAgent(tools=tools, model=my_model)
agent.run("Store the fact that our API rate limit is 100 req/min, then search for rate limits.")

As Agent Tools

CortexDB provides three Smolagents-compatible tools that implement the Tool class with name, description, inputs, output_type, and forward():

Search Tool

from cortexdb import Cortex
from cortexdb_smolagents import CortexDBSearchTool

client = Cortex(base_url="http://localhost:3141")
search = CortexDBSearchTool(client=client, tenant_id="my-app")

# Use directly
result = search.forward("deployment schedule")
print(result)

Store Tool

from cortexdb_smolagents import CortexDBStoreTool

store = CortexDBStoreTool(client=client, tenant_id="my-app", namespace="notes")

result = store.forward("The deployment window is 2-4am UTC on Tuesdays.")
print(result)  # "Memory stored successfully in CortexDB."

Forget Tool

from cortexdb_smolagents import CortexDBForgetTool

forget = CortexDBForgetTool(client=client, tenant_id="my-app")

result = forget.forward("old deployment schedule", "Schedule changed")
print(result)  # "Memories matching 'old deployment schedule' have been forgotten."

With CodeAgent

from smolagents import CodeAgent
from cortexdb import Cortex
from cortexdb_smolagents import get_cortexdb_tools

client = Cortex(base_url="http://localhost:3141")
tools = get_cortexdb_tools(client, tenant_id="engineering")

agent = CodeAgent(tools=tools, model=my_model)

# The agent can now write and execute code that uses these tools
agent.run("Save the fact that our Q2 target is $1M ARR, then look up our targets.")

With ToolCallingAgent

from smolagents import ToolCallingAgent
from cortexdb import Cortex
from cortexdb_smolagents import CortexDBSearchTool, CortexDBStoreTool

client = Cortex(base_url="http://localhost:3141")

agent = ToolCallingAgent(
    tools=[
        CortexDBSearchTool(client=client, tenant_id="my-app"),
        CortexDBStoreTool(client=client, tenant_id="my-app"),
    ],
    model=my_model,
)

agent.run("What do you remember about our deployment process?")

Configuration

| Parameter | Default | Description | |---|---|---| | client | Required | Initialized Cortex client instance | | tenant_id | "default" | Tenant identifier for multi-tenant isolation | | namespace | None | Namespace for organizing memories |

Complete Example

from smolagents import CodeAgent, ToolCallingAgent
from cortexdb import Cortex
from cortexdb_smolagents import get_cortexdb_tools

client = Cortex(base_url="http://localhost:3141")

# Create tools scoped to an engineering team
tools = get_cortexdb_tools(
    client=client,
    tenant_id="engineering",
    namespace="incidents",
)

# Build an incident analysis agent
agent = ToolCallingAgent(
    tools=tools,
    model=my_model,
    system_prompt=(
        "You are an incident analysis assistant. Use your memory tools to "
        "store incident details, search for past incidents with similar "
        "patterns, and forget resolved incidents when asked."
    ),
)

# First interaction — store an incident
agent.run("We had a database timeout incident at 3am. The root cause was connection pool exhaustion.")

# Later interaction — search for patterns
agent.run("Have we seen any database timeout incidents before?")

# Cleanup — forget resolved incidents
agent.run("The database timeout issue from last week has been fully resolved. Please forget it.")