MemoSift

PYTHON SDK

SDK Reference

Complete reference for the MemoSift Python SDK. Intercept tool results, track conversations, recall memories, and explore the knowledge graph.

terminal
pip install memosift

MemoSift()

#
MemoSift(api_key: str, base_url: str = "https://api.memosift.dev", timeout: float = 30.0)

Create a MemoSift client. The API key is required and can be generated from the dashboard or CLI. All subsequent calls use this client instance.

NameTypeDescription
api_keystrYour MemoSift API key. Starts with "msk_".
base_urlstrAPI base URL. Override for self-hosted deployments.
timeoutfloatHTTP request timeout in seconds. Default 30.
setup.py
from memosift import MemoSift

ms = MemoSift(api_key="msk_live_abc123")

# Self-hosted
ms = MemoSift(
    api_key="msk_live_abc123",
    base_url="https://memosift.internal.corp",
    timeout=60.0,
)

Returns

MemoSift

  • session()Create session-bound client
  • intercept()Intercept tool results
  • recall()Hybrid memory recall

session()

#
session(session_id: str) → MemoSiftSession

Returns a session-bound client that pre-fills session_id on all subsequent calls. Recommended for most workflows.

NameTypeDescription
session_idstrUnique identifier for this conversation session.
example.py
session = ms.session("conv_2024_04_18_a")

# All calls now use session_id automatically
result = await session.intercept(content, tool_name="bash")
await session.track(messages)
memories = await session.recall("auth decisions")

Returns

MemoSiftSession

  • session_idThe bound session identifier

intercept()

#
intercept(content: str | bytes, *, tool_name: str, session_id: str, security_mode: SecurityMode = REDACT, security_config: SecurityConfig | None = None) → InterceptResult

Runs the 5-stage local pipeline on tool output: strip reasoning, security scan, size gate, classify, and metadata extraction. Content under 500 chars passes through unchanged. Content 500+ chars creates an artifact. Content 2000+ chars is replaced with a summary.

NameTypeDescription
contentstr | bytesRaw tool output to intercept.
tool_namestrName of the tool that produced this output (e.g. "read_file").
session_idstrSession identifier.
security_modeSecurityModeREDACT (default), BLOCK, or PASSTHROUGH. Controls how security findings are handled.
security_configSecurityConfig | NoneFine-grained security rules: allow-lists, redaction patterns, and confidence thresholds.
intercept.py
result = await ms.intercept(
    tool_output,
    tool_name="read_file",
    session_id="my-session",
    security_mode=SecurityMode.REDACT,
)

# result.agent_facing_content  → summary or raw
# result.replaced              → True if content was replaced
# result.artifact               → ArtifactRecord if stored
# result.content_type          → ContentType enum
# result.security_findings     → tuple of SecurityFinding

Returns

InterceptResult

  • agent_facing_contentThe processed content to return to the agent
  • replacedTrue if content was replaced with a summary
  • artifactArtifactRecord if content was stored
  • content_typeClassified ContentType enum
  • security_findingsTuple of SecurityFinding objects
  • metadataExtracted metadata dict

track()

#
track(messages: list[dict], *, session_id: str, intercept_results: Sequence[InterceptResult] | None = None) → None

Ships a conversation turn to the MemoSift cloud. If the cloud is unreachable, the turn is queued locally (up to 1000 items) and retried automatically. Attach intercept results to link artifacts with the turn.

NameTypeDescription
messageslist[dict]OpenAI-format message dicts with role and content.
session_idstrSession identifier.
intercept_resultsSequence[InterceptResult] | NoneIntercept results from this turn, to link artifacts.
track.py
await ms.track(
    [
        {"role": "user", "content": "Read the auth module"},
        {"role": "assistant", "content": "Here's the code..."},
    ],
    session_id="my-session",
    intercept_results=[result],
)

Returns

None


recall()

#
recall(query: str | None = None, *, session_id: str, limit: int = 10, scope: str = "session", mode: str = "fast", filters: RecallFilters | None = None, intent_weights: dict[int, float] | None = None, query_embedding: list[float] | None = None, intent_version: int | None = None, turn: int | None = None) → RecallResult

Retrieves memories from the session or project. Supports three mutually exclusive modes: query (text search), intent_version (goal epoch retrieval), and turn (temporal snapshot). Exactly one mode parameter must be provided.

NameTypeDescription
querystr | NoneNatural language search query. Triggers query mode.
session_idstrSession identifier.
limitintMax number of results to return. Default 10.
scopestr"session" (default) or "project". Project scope searches across all sessions.
modestr"fast" (default) or "deep". Deep mode uses re-ranking for higher quality.
filtersRecallFilters | NoneFilter by content types, entities, topics, tags, turn range, or intent version.
intent_weightsdict[int, float] | NoneCustom weights for intent version scoring.
query_embeddinglist[float] | NonePre-computed embedding vector. Skips server-side embedding.
intent_versionint | NoneRetrieve memories for a specific intent epoch. Triggers intent mode.
turnint | NoneRetrieve memories active at a specific turn number. Triggers temporal mode.
recall_query.py
# Query mode
result = await ms.recall(
    "auth middleware decisions",
    session_id="s1",
    scope="project",
    mode="deep",
)
recall_intent.py
# Intent mode
result = await ms.recall(intent_version=2, session_id="s1")
recall_temporal.py
# Temporal mode
result = await ms.recall(turn=15, session_id="s1")

Returns

RecallResult

  • itemsList of RecallItem matches
  • total_availableTotal matching items (before limit)
  • session_intentCurrent session intent summary
  • diagnosticsTiming and scoring diagnostics

explore()

#
explore(item_id: str, *, kind: Literal["memory", "artifact", "proposition"], session_id: str, limit: int = 10) → ExploreResult

Multi-axis graph exploration from a given anchor item. Returns connected entities, related artifacts, temporal neighbors, and intent connections.

NameTypeDescription
item_idstrID of the anchor item to explore from.
kind"memory" | "artifact" | "proposition"Type of the anchor item.
session_idstrSession identifier.
limitintMax connections per axis. Default 10.
explore.py
related = await ms.explore(
    item_id="mem_4f8a2c",
    kind="memory",
    session_id="s1",
    limit=5,
)

for item in related.items:
    print(f"{item.via}: {item.content[:80]}")

Returns

ExploreResult

  • anchor_idID of the anchor item
  • anchor_kindType of the anchor item
  • itemsConnected items, each with a via field indicating the connection axis

fetch()

#
fetch(artifact_id: str) → FetchResult

Retrieves artifact metadata and content. For artifacts under 500KB, the content is returned inline as base64. For larger artifacts, a signed download URL is provided.

NameTypeDescription
artifact_idstrArtifact identifier (e.g. "art_7f2a3b").
fetch.py
content = await ms.fetch("art_7f2a3b")

if content.bytes_b64:
    raw = base64.b64decode(content.bytes_b64)
elif content.download_url:
    # Stream large artifacts
    async with httpx.AsyncClient() as client:
        resp = await client.get(content.download_url)

Returns

FetchResult

  • artifact_idThe artifact identifier
  • artifact_typeMIME type or content classification
  • byte_sizeSize in bytes
  • download_urlSigned URL for large artifacts (>500KB)
  • bytes_b64Base64-encoded content for small artifacts
  • summaryGenerated summary of the artifact

compress()

#
compress(*, session_id: str) → CompressResult

Assembles a compressed context window for the session. Uses pure SQL and template rendering with no LLM calls. Completes in under 50ms.

NameTypeDescription
session_idstrSession to assemble context for.
compress.py
compressed = await ms.compress(session_id="s1")

# Use as system prompt or prepend to messages
system_msg = {"role": "system", "content": compressed.rendered_text}

# Or use the structured representation
for section in compressed.structured:
    print(f"{section.label}: {len(section.content)} chars")

Returns

CompressResult

  • rendered_textReady-to-use compressed context string
  • structuredStructured sections with labels and content
  • diagnosticsAssembly timing and stats

get_at_turn()

#
get_at_turn(session_id: str, turn: int) → SessionSnapshot

Bi-temporal snapshot of the session at a specific turn. Returns the intent that was active, memories that existed, and artifacts that had been seen up to that point.

NameTypeDescription
session_idstrSession identifier.
turnintTurn number to snapshot.
get_at_turn.py
snapshot = await ms.get_at_turn("s1", turn=15)

print(snapshot.intent_at_turn)
print(f"Memories active: {len(snapshot.memories_active_at_turn)}")
print(f"Artifacts seen: {len(snapshot.artifacts_seen_by_turn)}")

Returns

SessionSnapshot

  • intent_at_turnThe intent version active at this turn
  • memories_active_at_turnMemories that existed at this turn
  • artifacts_seen_by_turnArtifacts created or referenced by this turn

get_intent()

#
get_intent(*, session_id: str) → SessionIntent

Retrieves the full intent history for a session, including all versions and the turn each was derived at. Useful for understanding how the conversation goal evolved.

NameTypeDescription
session_idstrSession identifier.
get_intent.py
intent = await ms.get_intent(session_id="s1")

for version in intent.versions:
    print(f"v{version.version} (turn {version.derived_at_turn}): {version.summary}")

Returns

SessionIntent

  • versionsList of intent versions with summary and derived_at_turn

ADAPTERS

Framework Integrations

Drop-in wrappers for popular agent frameworks. Each adapter handles tool interception, turn tracking, and memory injection automatically.

openai_agents_tool_wrapper()

#
openai_agents_tool_wrapper(*, session_id: str) → Callable

Returns a decorator that wraps OpenAI Agents SDK tool functions. Automatically intercepts tool output and tracks turns.

NameTypeDescription
session_idstrSession identifier for this agent run.
openai_agents.py
wrap = ms.openai_agents_tool_wrapper(session_id="my-session")

@wrap
async def read_file(path: str) -> str:
    with open(path) as f:
        return f.read()

Returns

Callable

  • (decorator)Wrap any async tool function to add interception

langgraph_wrapper()

#
langgraph_wrapper() → Callable

Returns a wrapper compatible with LangGraph's awrap_tool_call parameter. The session ID is automatically extracted from config.configurable.thread_id.

No parameters required

langgraph.py
from langgraph.prebuilt import ToolNode

awrap = ms.langgraph_wrapper()
tool_node = ToolNode(tools=[...], awrap_tool_call=awrap)
# session_id auto-extracted from config.configurable.thread_id

Returns

Callable

  • (wrapper)Compatible with ToolNode's awrap_tool_call parameter

claude_agent_hooks()

#
claude_agent_hooks() → dict

Returns a hooks dictionary for Anthropic's Agent SDK. Large tool results are automatically stored as artifacts and replaced with a fetch reference.

No parameters required

claude_agent.py
hooks = ms.claude_agent_hooks()
# Returns {"PostToolUse": async_handler}

# Large results are replaced with:
# "[MemoSift] Stored as artifact art_abc. Use memosift_fetch('art_abc')"

Returns

dict

  • PostToolUseAsync handler for post-tool-use interception

wrap()

#
wrap(client, *, session_id: str) → WrappedClient

Wraps an OpenAI client for full lifecycle interception. Before each call, memories are recalled and injected. During tool use, large results are intercepted. After each call, the turn is tracked in the background. Context is auto-compressed at 80% window pressure.

NameTypeDescription
clientAsyncOpenAIAn OpenAI async client instance to wrap.
session_idstrSession identifier.
wrap.py
from openai import AsyncOpenAI

wrapped = ms.wrap(AsyncOpenAI(), session_id="my-session")

# Pre-call: recall + inject memories
# Tool use: intercept large results
# Post-call: track in background
# Auto-compress at 80% context pressure

Returns

WrappedClient

  • (client)Drop-in replacement with automatic memory management

tools()

#
tools(*, session_id: str) → list[dict]

Returns 4 OpenAI-compatible function schemas that agents can call directly: memosift_recall, memosift_explore, memosift_fetch, and memosift_compress.

NameTypeDescription
session_idstrSession identifier for tool execution.
tools.py
tool_schemas = ms.tools(session_id="my-session")

# Pass to OpenAI
response = await client.chat.completions.create(
    model="gpt-4o",
    messages=messages,
    tools=tool_schemas,
)

Returns

list[dict]

  • (schemas)4 OpenAI function-calling tool definitions

TYPES

Key Types

Data classes returned by SDK methods. All fields are typed and accessible as attributes.

InterceptResult

agent_facing_contentstrProcessed content for the agent
replacedboolTrue if original content was replaced with summary
artifactArtifactRecord | NoneStored artifact record, if created
content_typeContentTypeClassified content type enum
security_findingstuple[SecurityFinding, ...]Security scan results
metadatadictExtracted metadata (entities, topics, etc.)

RecallResult

itemslist[RecallItem]Matching memory items
total_availableintTotal matches before limit
session_intentstr | NoneCurrent session intent summary
diagnosticsdictTiming and scoring details

RecallItem

idstrUnique item identifier
kindstr"memory", "artifact", or "proposition"
contentstrItem content or summary
scorefloatRelevance score (0-1)
intent_alignmentfloat | NoneAlignment with current intent
turn_numberintTurn when this item was created
artifact_metadict | NoneArtifact metadata if kind is artifact
related_entitieslist[str]Entity names linked to this item

ExploreResult

anchor_idstrID of the anchor item
anchor_kindstrType of the anchor item
itemslist[ExploreItem]Connected items with via field

FetchResult

artifact_idstrArtifact identifier
artifact_typestrMIME type or content classification
byte_sizeintSize in bytes
download_urlstr | NoneSigned URL for large artifacts
bytes_b64str | NoneBase64 content for small artifacts
summarystrGenerated summary

CompressResult

rendered_textstrCompressed context string
structuredlist[Section]Structured sections
diagnosticsdictAssembly timing and stats

SecurityConfig

allowlist[str]Patterns to allow through without scanning
redactlist[str]Patterns to always redact
min_confidencefloatMinimum confidence threshold for findings (0-1)

RecallFilters

content_typeslist[str] | NoneFilter by content type
entitieslist[str] | NoneFilter by entity name
topicslist[str] | NoneFilter by topic
tagslist[str] | NoneFilter by tag
turn_range_fromint | NoneStart of turn range (inclusive)
turn_range_toint | NoneEnd of turn range (inclusive)
intent_versionint | NoneFilter to specific intent epoch