Skip to main content

Spectron agent memory for the OpenAI Agents SDK

Project description

Spectron for the OpenAI Agents SDK

Give agents built with the OpenAI Agents SDK a durable, shared memory backed by Spectron, SurrealDB's memory and knowledge layer.

Spectron stores facts, past turns, preferences, and knowledge as a graph with temporal awareness, and lets several agents read and write the same memory. This package wires that into the Agents SDK so an agent can remember across runs and recall what it needs before it answers.

How it works

There are two ways to use memory, and they compose:

  1. Function tools the agent calls itself. The agent decides when to remember, recall, context, reflect, or forget. This is the pattern used by the mem0 and cognee integrations.
  2. Automatic memory around a run. run_with_memory recalls memory relevant to the input, injects it into the prompt, runs the agent, and stores the result. The agent needs no memory tools of its own.

Both talk to Spectron through a single SpectronClient, scoped by a MemoryScope (agent_id, session_id, user_id).

Installation

pip install spectron-openai-agents-sdk

This pulls in the Spectron SDK (surrealdb), which the integration uses to reach a deployment.

Set the connection and model credentials:

export OPENAI_API_KEY="your-openai-api-key"
export SPECTRON_ENDPOINT="https://your-spectron-endpoint"
export SPECTRON_CONTEXT="your-memory-context"
export SPECTRON_API_KEY="your-api-key"   # optional for local, unsecured instances

Usage

Memory as function tools

from agents import Agent, Runner
from spectron_openai_agents_sdk import get_spectron_tools

agent = Agent(
    name="assistant",
    instructions=(
        "You are a helpful assistant. Use recall to check memory before you "
        "answer, and use remember to store anything worth keeping."
    ),
    tools=get_spectron_tools(session_id="user-123"),
)

Runner.run_sync(agent, "My name is Ada and I work on databases.")

result = Runner.run_sync(agent, "What do you know about me?")
print(result.final_output)

get_spectron_tools builds the tools from SPECTRON_* environment variables by default. Pass a client= to use an explicit SpectronClient, and include= to choose a subset of operations.

Automatic memory around a run

import asyncio
from agents import Agent
from spectron_openai_agents_sdk import MemoryScope, run_with_memory

agent = Agent(name="assistant", instructions="You are a helpful assistant.")
scope = MemoryScope(session_id="user-123")

async def main():
    await run_with_memory(agent, "My name is Ada.", scope=scope)

    result = await run_with_memory(agent, "What is my name?", scope=scope)
    print(result.final_output)

asyncio.run(main())

Persisting output with hooks

To save an agent's output while running it yourself, attach SpectronMemoryHooks:

from agents import Runner
from spectron_openai_agents_sdk import SpectronClient, SpectronMemoryHooks, MemoryScope

client = SpectronClient.from_env()
hooks = SpectronMemoryHooks(client, MemoryScope(session_id="user-123"))

await Runner.run(agent, "Summarize our project decisions.", hooks=hooks)

Operations

Operation Purpose
remember Store a fact, preference, or event.
recall Search memory for content relevant to a query.
context Assemble a single ranked context block for a query.
reflect Synthesize stored memory into a higher-level summary.
forget Remove matching memory.

Multi-agent shared memory

Agents that share a MemoryScope read and write the same Spectron memory, so knowledge one agent stores is available to another. See examples/04_multi_agent_shared_memory.py.

Examples

Runnable scripts live in examples/:

  • 01_quickstart.py stores a fact and recalls it with the memory tools.
  • 02_function_tools.py exercises the full tool set.
  • 03_automatic_memory.py uses run_with_memory with no memory tools on the agent.
  • 04_multi_agent_shared_memory.py shares one memory across two agents.

Configuration notes

This integration targets the Spectron client in surrealdb 3.x, which exposes Spectron and AsyncSpectron. The SDK is imported lazily, only when a client is built from settings or the environment, and all SDK calls live in src/spectron_openai_agents_sdk/client.py. If a future SDK release changes a method name or constructor argument, that file is the single place to adjust; the tools, hooks, and examples do not change.

The five operations map onto SDK methods as remember, recall, context (the SDK's query_context), reflect, and forget. Scope is applied per call: session_id maps to the SDK session_id, user_id to on_behalf_of, and agent_id to scopes on writes and lens on reads.

License

Licensed under the Apache License 2.0.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

spectron_openai_agents_sdk-0.1.1.tar.gz (20.3 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

spectron_openai_agents_sdk-0.1.1-py3-none-any.whl (18.9 kB view details)

Uploaded Python 3

File details

Details for the file spectron_openai_agents_sdk-0.1.1.tar.gz.

File metadata

File hashes

Hashes for spectron_openai_agents_sdk-0.1.1.tar.gz
Algorithm Hash digest
SHA256 21acafa633b8db3ca29fbeb6eca9b07b2da53e054bd26adfc18e43c38117a5f7
MD5 3c9581db72ab2c6758cc3b49d0a916b5
BLAKE2b-256 83e95f438b8873503223e6977ead509d48e3ab6b5e3f73605d03d1eee483e6e4

See more details on using hashes here.

Provenance

The following attestation bundles were made for spectron_openai_agents_sdk-0.1.1.tar.gz:

Publisher: release.yml on surrealdb/spectron-openai-agents-sdk

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file spectron_openai_agents_sdk-0.1.1-py3-none-any.whl.

File metadata

File hashes

Hashes for spectron_openai_agents_sdk-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 f3baa63db2d79c09a50e232a8646b640849a39a46c63a4fa04c2002ef604441c
MD5 92bbb2dccca780b54e3c527ace774487
BLAKE2b-256 37547fbb11fe8c8aad622029a66217b93c207b304616610f6b4e19eec69fd952

See more details on using hashes here.

Provenance

The following attestation bundles were made for spectron_openai_agents_sdk-0.1.1-py3-none-any.whl:

Publisher: release.yml on surrealdb/spectron-openai-agents-sdk

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page