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 cortexdbai[smolagents]

Quick Start

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

client = Cortex(base_url="https://api.cortexdb.ai")
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="https://api.cortexdb.ai")
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")

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="https://api.cortexdb.ai")
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="https://api.cortexdb.ai")

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?")

Under the Hood

The integration wraps CortexDB's REST API. Here are the equivalent calls:

# remember() — store a memory
curl -X POST https://api.cortexdb.ai/v1/remember \
  -H "Authorization: Bearer your-api-key" \
  -H "Content-Type: application/json" \
  -d '{
    "content": "The deployment window is 2-4am UTC on Tuesdays.",
    "tenant_id": "my-app"
  }'
# Returns: { "event_id": "019d6359-d3cc-7671-9e4c-9151011fa016" }

# recall() — retrieve relevant context
curl -X POST https://api.cortexdb.ai/v1/recall \
  -H "Authorization: Bearer your-api-key" \
  -H "Content-Type: application/json" \
  -d '{
    "query": "deployment schedule",
    "tenant_id": "my-app"
  }'
# Returns: { "context": "...", "confidence": 0.90, "latency_ms": 11 }

Configuration

| Parameter | Default | Description | |---|---|---| | client | Required | Initialized Cortex client instance | | tenant_id | "default" | Tenant identifier for multi-tenant isolation |

Complete Example

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

client = Cortex(base_url="https://api.cortexdb.ai")

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

# 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.")