Skip to content

Agent Framework history

Microsoft Agent Framework loads and stores conversation history through a HistoryProvider. agent-framework-persql keeps that history in a PerSQL database, so agent threads survive restarts and live in an isolated SQLite database you can query like any other.

pip install agent-framework-persql
import os
from persql import PerSQL
from agent_framework_persql import PerSQLHistoryProvider
client = PerSQL(token=os.environ["PERSQL_TOKEN"])
provider = PerSQLHistoryProvider(client.database("acme/agent-state"))
agent = chat_client.as_agent(instructions="Be helpful.", context_providers=[provider])
session = agent.create_session(session_id="user-42")
await agent.run("hi", session=session)
await agent.run("remember me?", session=session) # history persists

The standard HistoryProvider flags work as in the built-in providers, so the same class covers primary memory, audit-only storage (load_messages=False), and evaluation capture. Async PerSQL databases are awaited natively; sync databases run via asyncio.to_thread.

Histories are keyed by session id and can share one database. For hard isolation, give each tenant its own database instead:

provider = PerSQLHistoryProvider(client.database(f"acme/tenant-{tenant_id}"))

PerSQL databases are provisioned on first write and billed by usage, so per-tenant history stores cost what they’re actually used for.

SELECT json_extract(message_data, '$.role') AS role, created_at
FROM agent_framework_messages
WHERE session_id = 'user-42'
ORDER BY id;