by SparkofSpike
Committed to building the world’s most cutting-edge, CJK-first memory module for all mainstream agents. Any issues and PRs are welcome.
# Add to your Claude Code skills
git clone https://github.com/SparkofSpike/Brain4AgentBrain4Agent is an open-source ai agents skill for AI coding assistants such as Claude Code, Codex CLI, and ChatGPT, built by SparkofSpike. Committed to building the world’s most cutting-edge, CJK-first memory module for all mainstream agents. Any issues and PRs are welcome. It has 178 GitHub stars.
Brain4Agent's catalog security scan is still queued. You can run an instant dependency and prompt-injection check now with the "Scan for vulnerabilities" button above.
Clone the repository with "git clone https://github.com/SparkofSpike/Brain4Agent" and add it to your Claude Code skills directory (see the Installation section above).
Brain4Agent is primarily written in Python. It is open-source under SparkofSpike on GitHub, so you can review or fork the full source.
Yes. AgentsLLM lists many other AI Agents skills you can browse and compare side by side. Open the AI Agents category from the badge at the top of this page, or use the Related Skills and comparison links further down to weigh Brain4Agent against similar tools.
No comments yet. Be the first to share your thoughts!
Unlocks once the catalog security scan passes (runs nightly).
The deep catalog scan for this skill is still queued. Run an instant dependency check now instead.
Agents: start here. FOR_AGENTS.md is this page written for you — what the store does for an agent, how to install it, how to call it, and where it stops being useful.
Long-term memory for agents. CJK-first retrieval, no model in the core path, and evidence instead of confidence scores.
pip install "Brain4Agent[precise]" needs Python 3.11+, plus a 20 MB optional
dictionary. Everything works without it.
An agent working on a codebase accumulates context: why a decision was made, which approach failed, where a deployment lives. Brain4Agent stores those turns in one SQLite file and retrieves them by session.
corpus_missing) and which ones a given session
lacked (missing_terms). The caller decides what that means.Memories also carry tags (scope), superseded_by (replacement) and refs
(one-hop links), supplied by the writer.
pip install "Brain4Agent[precise]" # recommended: adds jieba, hit@1 0.950
pip install Brain4Agent # zero dependencies: bigrams only, hit@1 0.875
Windows and macOS both work; Python 3.11 or newer is required. The package is on
PyPI as https://pypi.org/project/Brain4Agent/. To work on the code itself,
git clone and pip install -e ".[precise,dev]".
jieba is a 20 MB static dictionary. Without it the code falls back to bigrams.
Windows, non-ASCII input. The MCP server and the CLI pin their stdin/stdout
to UTF-8, so CJK content works regardless of the console code page. Command
line arguments are decoded by Python before any of that runs, though — if you
pass non-ASCII text as an argument, set PYTHONUTF8=1.
$ brain4agent exchange \
"中文查询搜不到记忆,recall 返回空" \
"根因在 keywords.py 第 443 行,正则不含汉字范围"
stored exchange in session 913bcac81341
$ brain4agent exchange \
"杭州那台测试机的地址是 203.0.113.42" \
"部署目录 /opt/demo-app,跑的是 PostgreSQL 14"
stored exchange in session c060a5294037
$ brain4agent search "中文查询为什么搜不到" -k 1
1 sessions for '中文查询为什么搜不到'
[ 6.73 cov=1.00] 913bcac81341
--- 913bcac81341 score=6.73 coverage=1.00 ---
user: 中文查询搜不到记忆,recall 返回空
assistant: 根因在 keywords.py 第 443 行,正则不含汉字范围
A question the store cannot answer:
$ brain4agent search "杭州测试机上装的是什么数据库" -k 1
1 sessions for '杭州测试机上装的是什么数据库'
corpus has no: 数据库, 机上
[ 4.81 cov=0.28] c060a5294037 session lacks: 数据库, 机上
Bulk-load an existing history from JSONL (one object per line, content
required):
$ brain4agent import conversations.jsonl
imported 4 turns across 2 sessions
{"session_id": "s1", "role": "user", "content": "中文查询搜不到记忆"}
{"session_id": "s1", "role": "assistant", "content": "根因在 keywords.py 第 443 行"}
{"mcpServers": {"brain4agent": {
"command": "python",
"args": ["-m", "brain4agent.mcp_server"],
"env": {"BRAIN4AGENT_DB": "/path/to/brain.db"}
}}}
Seven tools:
| Tool | Purpose |
|---|---|
b4a_remember |
Store one turn; returns a session id |
b4a_supersede |
Replace an outdated memory with an existing correction; keeps history |
b4a_unsupersede |
Undo that link; makes the old memory current again |
b4a_recall |
Search; returns sessions with corpus_missing and per-session missing_terms |
b4a_ask |
Retrieve and answer in one step; needs a model, and says so when none is configured |
b4a_session |
Read a whole session in order |
b4a_stats |
Counts |
To correct a fact, store its replacement with b4a_remember, then call
b4a_supersede with old_id and new_id. Memory IDs are returned by
b4a_remember, as memory_id in recall evidence, and as id in session turns.
Recall follows the replacement even when queried with the old wording;
b4a_session keeps both turns and shows superseded_by on the old one. The
replacement also inherits the old memory's tags, so a scope-filtered query still
reaches the correction. b4a_unsupersede takes the link back if the correction
turns out to be wrong.
The same library is also packaged as an agent skill.
from brain4agent import MemoryStore
with MemoryStore("brain.db") as store:
sid = store.new_session()
store.add("杭州那台测试机的地址是 203.0.113.42", session_id=sid, role="user")
store.add("部署目录 /opt/demo-app", session_id=sid, role="assistant")
result = store.search("杭州测试机上装的是什么数据库")
print(result.corpus_missing) # ['数据库', '机上']
print(result.best().coverage) # 0.28
add_exchange(user=..., assistant=...) stores a pair as one session. Retrieval
is session-level, not turn-level.
Bulk imports go through batch(), which defers the commit to one per group:
with store.batch():
for turn in turns:
store.add(turn.text, session_id=turn.sid, role=turn.role)
Off by default. Enabled only when a key is present.
from brain4agent import Judge, MemoryStore
judge = Judge.from_env() # reads BRAIN4AGENT_JUDGE_KEY; None if unset
if judge:
r = store.search_judged("question", judge)
print(r.verdict) # found / not_found / uncertain
uncertain is a third state, distinct from not_found: a judge that times out
is not evidence that the memory does not exist.
write read
│ │
bigrams + dictionary words dictionary words
│ │
▼ ▼
┌──────────────────────────────────────────┐
│ SQLite: id, session, role, content, │
│ created_at, tokens │
└──────────────────────────────────────────┘
│ │
▼ ▼
BM25 index (in memory) → session-level evidence
+ corpus_missing
+ per-session coverage
One SQLite file. Rows are turns; the unit of retrieval is the session. Index tokens are computed at write time and cached in a column, so rebuilding the in-memory index is a counting exercise.
import rebuild query
127 turns 44 sessions 3.9 ms 1.8 ms
1016 turns 352 sessions 0.25 s 29 ms 3.7 ms
Writes commit in batches. A 1,016-turn import takes 3.8 s one commit at a time
and 0.25 s inside batch().
The rebuild column in that table is the oldest measurement in this README. Two later changes both live in the rebuild: the phrase pair index (D-041), which roughly doubles the column, and the sequence cache (D-042). Re-measured on the same machine against one real store, rebuilding now costs 18.0 ms at 127 turns, 101 ms at 1,016 and 393 ms at 2,925 -- three to four times the numbers above. The table is kept because its shape is still right and the import column is still current; use the new figures for anything that depends on open latency (D-043).
The file is SQLite because of the writes, not the reads: retrieval happens in
the in-memory index, while every search credits access counts with one commit,
and the MCP server and the CLI can hold the same file at once. WAL mode means
"one file" is three on disk -- brain.db, brain.db-wal, brain.db-shm -- and
a snapshot must go through SQLite's backup API rather than a file copy, which
leaves the log behind and drops committed rows.
The cost to know about is not the file, and as of 0.2.0 it is no longer the write-then-read cycle either. A local write extends the index in place instead of dirtying it, so the first search after a write costs 16.3 ms at 2,930 memories instead of 408 ms, against 4.4 ms once warm (D-049). Three derived tables cannot be extended, because every idf is a function of the document count; that pass is deferred to the next query and costs 11.3 ms, already inside that 16.3 ms. Resident memory is the other ceiling -- 145 bytes per index token, about 1.2 GB at 100k memories. Both belong to the in-memory index layer, not to SQLite (D-043).
Indexing and querying use different tokenizers. Measured over a 3×3 grid of index strategy against query strategy (D-019), then two changes on top of the best cell (D-037):
| index | query | hit@1 | MRR |
|---|---|---|---|
| bigram | bigram | 0.825 | 0.891 |
| jieba | jieba | 0.875 | 0.911 |
| bigrams + words | words | 0.925 | 0.940 |
| bigrams + words | words − question words | 0.950 | 0.961 |
The last row adds two things the 3×3 grid did not test: interrogatives are removed from the query side, and a session's best three turns are folded into one score. Both are measured in D-037;