crewai-persistence-sql
SQL persistence backend for CrewAI Flows via SQLAlchemy — one provider for SQLite, PostgreSQL, MySQL, and more, selected by connection URL. Optional built-in message pruning via agentstate-reducer. Implements CrewAI's FlowPersistence interface.
Installation
pip install crewai-persistence-sql # SQLite built-in
pip install "crewai-persistence-sql[postgres]" # + psycopg
pip install "crewai-persistence-sql[mysql]" # + pymysql
pip install "crewai-persistence-sql[reducer]" # + message pruning
Requires Python 3.10–3.13.
Usage
from crewai.flow.flow import Flow, start
from crewai.flow.persistence import persist
from crewai_persistence_sql import SQLFlowPersistence
backend = SQLFlowPersistence(url="sqlite:///crewai_flows.db")
# or "postgresql+psycopg://user:pass@host/db" / "mysql+pymysql://user:pass@host/db"
@persist(backend)
class MyFlow(Flow[MyState]):
@start()
def begin(self):
self.state.counter += 1
With message pruning:
from agentstate_reducer import MessageReducer
backend = SQLFlowPersistence(
url="sqlite:///crewai_flows.db",
reducer=MessageReducer(min_messages=10, max_messages=20),
messages_key="messages",
)
API
SQLFlowPersistence(url=None, *, table_name="crewai_flow_states", engine=None, reducer=None, messages_key="messages")
| Parameter | Description |
|---|---|
url |
SQLAlchemy connection URL |
table_name |
table (auto-created if absent) |
engine |
optional pre-built SQLAlchemy Engine (supply this or url) |
reducer |
optional MessageReducer to prune messages_key before save |
messages_key |
state field holding the message list (default "messages") |
Data model
One row per flow: flow_uuid (primary key) + data (the flow state as a JSON string). Each save upserts (portable update-then-insert), so load_state returns the latest.
License
MIT
Long-term memory via on_prune (agentstate-reducer >= 0.4.0)
Messages pruned from the flow state are exactly the ones leaving the model's view. The persistence layer forwards a memory namespace to the reducer, and any on_prune hook receives (pruned_messages, namespace) — so pruned turns can flow straight into CrewAI's unified Memory (or any store), with no package coupling:
from agentstate_reducer import MessageReducer, ReducerConfig, Background
from crewai.memory import Memory
memory = Memory(storage=...) # e.g. crewai-memory-dynamodb
def remember(pruned, namespace):
text = "\n".join(m["content"] for m in pruned)
memory.remember_many(memory.extract_memories(text), scope=namespace) # LLM calls -> run off-path
reducer = MessageReducer(config=ReducerConfig(max_messages=20, on_prune=[Background(remember)]))
class SupportState(BaseModel):
id: str = ""
memory_namespace: str = "/user/kamal" # long-term scope: the USER, not the flow
messages: list = []
@persist(SQLFlowPersistence(..., reducer=reducer))
class SupportFlow(Flow[SupportState]):
...
The persistence layer reads memory_namespace (or whatever ReducerConfig.namespace_key names) from the flow state on every save_state and passes it through untouched. If the state never sets it, the namespace falls back to "/flow/<flow_uuid>". Each pruned message reaches the hooks once.
Metadata
Release files for crewai_persistence_sql 0.2.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| crewai_persistence_sql-0.2.0.tar.gz | 5.7 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| crewai_persistence_sql-0.2.0-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 10.7 kB
Release files / crewai_persistence_sql-0.2.0.tar.gz
| Download URL | crewai_persistence_sql-0.2.0.tar.gz |
|---|---|
| Size | 5.7 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
56fbfcf0eb1613950a38a2b62882441b266f943f1ecfed3a298290a9b964bd1f
|
|
BLAKE2b-256 checksum How to use checksums |
79f2372e4ab87e2034f44acea14c3cf9c3c4fca34a50f11d2ac1edd05f9eea7c
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
uv/0.11.23 {"installer":{"name":"uv","version":"0.11.23","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}
|
Release files / crewai_persistence_sql-0.2.0-py3-none-any.whl
| Download URL | crewai_persistence_sql-0.2.0-py3-none-any.whl |
|---|---|
| Size | 5.0 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
a407f253b2df9bc08e279ed5f19e9be0df3f57f3dbd9025fc73fc8258fb438d1
|
|
BLAKE2b-256 checksum How to use checksums |
8692a45519f0fdb2a63ddb9df763d35696c6d4791cf2b3c3d4c34aed316dfa2b
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
uv/0.11.23 {"installer":{"name":"uv","version":"0.11.23","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}
|