AI agents start every session from scratch. A persistent memory system lets agents store lessons and retrieve relevant context via vector search. Turso’s embedded database with built-in vector functions makes this possible in a single local file.This guide covers the schema and queries for building a memory system with vector similarity search.
Schema
At its simplest, a memory system needs just two tables: one for memories and one for tracking which memories were used in which tasks.
CREATE TABLE IF NOT EXISTS memories ( id TEXT PRIMARY KEY, content TEXT NOT NULL, embedding F8_BLOB(384), category TEXT NOT NULL, created_at INTEGER NOT NULL, last_retrieved INTEGER, retrieval_count INTEGER DEFAULT 0, source_task TEXT);CREATE TABLE IF NOT EXISTS tasks ( id TEXT PRIMARY KEY, description TEXT, embedding F8_BLOB(384), started_at INTEGER, finished_at INTEGER);
memories stores each lesson with its vector embedding and a category (e.g. correction, insight, user, discovery). The embedding is a 384-dimensional int8-quantized vector (F8_BLOB(384)).
tasks records each task the agent worked on, linking task descriptions to their embeddings for retrieval.
Connecting
import { connect } from "@tursodatabase/database";const db = await connect(".memory/agent.db");await db.exec(SCHEMA);
Storing a memory
When the agent learns something — a correction, a user preference, an insight — store it with an embedding. Use vector8() to convert a JSON float array into an int8-quantized vector:
The parameter for vector8() is a JSON-stringified float array (e.g. 384 dimensions from a model like all-MiniLM-L6-v2). Turso handles the quantization internally:
When a new task starts, find the most relevant memories using Turso’s vector_distance_cos() function:
SELECT id, content, category, created_at, retrieval_count, vector_distance_cos(embedding, vector8(?)) AS distanceFROM memoriesWHERE embedding IS NOT NULLORDER BY distance ASCLIMIT ?;
vector_distance_cos() returns cosine distance (0 = identical, 2 = opposite). Lower is better. To convert to similarity: 1.0 - distance.
After retrieval, update the memory’s metadata:
UPDATE memories SET last_retrieved = ?, retrieval_count = retrieval_count + 1WHERE id = ?;
Correcting wrong memories
When a memory turns out to be wrong, delete it and store a corrected version:
DELETE FROM memories WHERE id = ?;INSERT INTO memories (id, content, embedding, category, created_at)VALUES (?, ?, vector8(?), 'correction', ?);
Task tracking
Record tasks to understand what the agent has been working on:
-- Start a taskINSERT INTO tasks (id, description, embedding, started_at)VALUES (?, ?, vector8(?), ?);-- Complete a taskUPDATE tasks SET finished_at = ? WHERE id = ?;
Purging old memories
Remove memories that have been around a long time but are never retrieved:
DELETE FROM memoriesWHERE retrieval_count = 0 AND created_at < ?;
Key design points
Vector search uses Turso’s vector8() and vector_distance_cos() for cosine similarity directly in SQL. Int8 quantization reduces storage by 75% compared to float32 with minimal impact on search quality.
For small per-project datasets, a full table scan with ORDER BY distance LIMIT k is fast enough without a vector index.
Everything runs in a single embedded database file with no external services.
Going further
The schema above is deliberately minimal. In practice you will want to experiment with strategies for keeping the memory store useful as it grows. Some ideas:
Weighted memories — add a weight REAL column and boost or penalize memories based on whether the agent found them useful after retrieval. Use an exponential moving average to update weights over time.
Time decay — multiply a recency factor into the retrieval ranking so that stale memories rank lower. For example: (1.0 - distance) * POWER(0.95, days_since_last_retrieved).
Garbage collection — periodically remove memories with low weight and high retrieval count (retrieved often, never useful), or memories that have never been retrieved after a threshold period.
Task scoring — track outcome metrics (tokens used, errors, corrections) per task and use them to compute a credit signal for the memories that were retrieved during that task.
Retrieval attribution — add a join table (memory_id, task_id, similarity, credit) to record exactly which memories were used in each task and how they were rated.
There is no single best approach — the right strategy depends on your agent’s workload, how often memories are created, and how aggressively you want to prune. Start simple and iterate.
Example
memelord is an MCP server and hooks system that implements this pattern with reinforcement learning and weight decay as a persistent memory layer for AI coding agents.
{"docsConfig":{"theme":"mint","$schema":"https://mintlify.com/docs.json","name":"Turso","colors":{"primary":"#1EBCA1","light":"#1EBCA1","dark":"#1EBCA1"},"logo":{"light":"https://mintcdn.com/turso/0gN7SecUGL1uVO9v/logo/turso-light.svg?fit=max\u0026auto=format\u0026n=0gN7SecUGL1uVO9v\u0026q=85\u0026s=089d09358d8f5a3e3815861a7419a616","dark":"https://mintcdn.com/turso/0gN7SecUGL1uVO9v/logo/turso-dark.svg?fit=max\u0026auto=format\u0026n=0gN7SecUGL1uVO9v\u0026q=85\u0026s=2946b82b1620bb2c7839f85d7e2e113b"},"favicon":"https://mintcdn.com/turso/0gN7SecUGL1uVO9v/favicon.svg?fit=max\u0026auto=format\u0026n=0gN7SecUGL1uVO9v\u0026q=85\u0026s=3eb0166155fecb6dbad825308e71a8aa","api":{"mdx":{"auth":{"method":"bearer"},"server":"https://api.turso.tech"}},"appearance":{"default":"dark"},"background":{"color":{"light":"#FFFFFF","dark":"#121B22"}},"navbar":{"links":[{"href":"https://turso.tech/app","label":"Dashboard"}],"primary":{"type":"github","href":"https://github.com/tursodatabase/turso"}},"footer":{"socials":{"discord":"https://tur.so/discord","github":"https://github.com/tursodatabase","twitter":"https://twitter.com/tursodatabase","linkedin":"https://www.linkedin.com/company/turso"}},"integrations":{"gtm":{"tagId":"GTM-WW757Q5"}},"contextual":{"options":["copy","view","chatgpt","claude"],"display":"header"},"styling":{"latex":true},"navigation":{"global":{"anchors":[{"anchor":"Turso Homepage","icon":"house","href":"https://turso.tech"},{"anchor":"Discord","icon":"discord","href":"https://tur.so/discord"},{"anchor":"GitHub","icon":"github","href":"https://github.com/tursodatabase/turso"}]},"pages":[]}},"deployment":{"deploymentMetadataValue":{"subdomain":"turso","hasAudioNarration":false,"actualSubdomain":"turso","customDomains":["docs.turso.tech"],"gitSource":{"type":"github","owner":"tursodatabase","repo":"turso-docs","deployBranch":"main","contentDirectory":"","isPrivate":false,"mountPath":"","label":""},"feedback":{"thumbs":true,"edits":true,"issues":true},"entitlements":{"AI_CHAT":{"status":"DISABLED"}},"buildId":"6aec579f9c320f62bb68dc19","clientVersion":"0.0.4143","searchFilterCounts":[{"language":"default","tag":"Turso Cloud","count":223},{"language":"default","tag":"Turso Database","count":72},{"language":"default","tag":"SDKs","count":62},{"language":"default","tag":"API Reference","count":56},{"language":"default","tag":"CLI","count":56},{"language":"default","tag":"Resources","count":52},{"language":"default","tag":"Commands","count":50},{"language":"default","tag":"Official SDKs","count":47},{"language":"default","tag":"SQL Reference","count":46},{"language":"default","tag":"Statements","count":30}]},"assistantConfig":{},"basePath":"","hasChatPermissions":false,"hasWebMcpPermissions":false,"toggles":[{"name":"kill-isr","enabled":true,"variant":{"name":"disabled","enabled":false,"feature_enabled":true},"impressionData":false},{"name":"custom-rbac-roles","enabled":true,"variant":{"name":"disabled","enabled":false,"feature_enabled":true},"impressionData":false},{"name":"agent-mintlify-slack","enabled":true,"variant":{"name":"disabled","enabled":false,"feature_enabled":true},"impressionData":false},{"name":"agent-integrations-settings","enabled":true,"variant":{"name":"disabled","enabled":false,"feature_enabled":true},"impressionData":false},{"name":"rbac-expansion","enabled":true,"variant":{"name":"disabled","enabled":false,"feature_enabled":true},"impressionData":false},{"name":"automations-pr-grouping","enabled":true,"variant":{"name":"disabled","enabled":false,"feature_enabled":true},"impressionData":false},{"name":"custom-prefix","enabled":true,"variant":{"name":"disabled","enabled":false,"feature_enabled":true},"impressionData":false},{"name":"analytics-v7","enabled":true,"variant":{"name":"disabled","enabled":false,"feature_enabled":true},"impressionData":false},{"name":"user-flows-analytics","enabled":true,"variant":{"name":"disabled","enabled":false,"feature_enabled":true},"impressionData":false},{"name":"no-publish-flow","enabled":true,"variant":{"name":"disabled","enabled":false,"feature_enabled":true},"impressionData":false},{"name":"workos-onboarding","enabled":true,"variant":{"name":"disabled","enabled":false,"feature_enabled":true},"impressionData":false},{"name":"snapshot-serving-live","enabled":true,"variant":{"name":"disabled","enabled":false,"feature_enabled":true},"impressionData":false},{"name":"stripTemperatureMiddleware","enabled":true,"variant":{"name":"disabled","enabled":false,"feature_enabled":true},"impressionData":false},{"name":"support-platform-pylon-zendesk","enabled":true,"variant":{"name":"disabled","enabled":false,"feature_enabled":true},"impressionData":false},{"name":"snapshot-serving","enabled":true,"variant":{"name":"disabled","enabled":false,"feature_enabled":true},"impressionData":false},{"name":"instant-rollback","enabled":true,"variant":{"name":"disabled","enabled":false,"feature_enabled":true},"impressionData":false},{"name":"granular-notifications-server","enabled":true,"variant":{"name":"disabled","enabled":false,"feature_enabled":true},"impressionData":false},{"name":"granular-notifications","enabled":true,"variant":{"name":"disabled","enabled":false,"feature_enabled":true},"impressionData":false},{"name":"automations-fable","enabled":true,"variant":{"name":"disabled","enabled":false,"feature_enabled":true},"impressionData":false},{"name":"fixed-ai-action-pricing","enabled":true,"variant":{"name":"disabled","enabled":false,"feature_enabled":true},"impressionData":false},{"name":"automation-resolve-merge-conflict","enabled":true,"variant":{"name":"disabled","enabled":false,"feature_enabled":true},"impressionData":false},{"name":"scoped-publishing","enabled":true,"variant":{"name":"disabled","enabled":false,"feature_enabled":true},"impressionData":false},{"name":"mintql","enabled":true,"variant":{"name":"disabled","enabled":false,"feature_enabled":true},"impressionData":false},{"name":"page-level-analytics","enabled":true,"variant":{"name":"disabled","enabled":false,"feature_enabled":true},"impressionData":false},{"name":"editor-version-history-v2","enabled":true,"variant":{"name":"disabled","enabled":false,"feature_enabled":true},"impressionData":false},{"name":"billing-page-v2","enabled":true,"variant":{"name":"disabled","enabled":false,"feature_enabled":true},"impressionData":false},{"name":"full-page-search","enabled":true,"variant":{"name":"disabled","enabled":false,"feature_enabled":true},"impressionData":false},{"name":"workspace-projects","enabled":true,"variant":{"name":"disabled","enabled":false,"feature_enabled":true},"impressionData":false},{"name":"assistant-v5","enabled":true,"variant":{"name":"disabled","enabled":false,"feature_enabled":true},"impressionData":false},{"name":"fork-pr-previews","enabled":true,"variant":{"name":"disabled","enabled":false,"feature_enabled":true},"impressionData":false},{"name":"revalidation-jwt","enabled":true,"variant":{"name":"disabled","enabled":false,"feature_enabled":true},"impressionData":false},{"name":"workos-sso-scim","enabled":true,"variant":{"name":"disabled","enabled":false,"feature_enabled":true},"impressionData":false},{"name":"hobby-abuse-scan","enabled":true,"variant":{"name":"disabled","enabled":false,"feature_enabled":true},"impressionData":false},{"name":"dashboard-inbox","enabled":true,"variant":{"name":"disabled","enabled":false,"feature_enabled":true},"impressionData":false},{"name":"workos-signup","enabled":true,"variant":{"name":"disabled","enabled":false,"feature_enabled":true},"impressionData":false},{"name":"turbopuffer-dual-write","enabled":true,"variant":{"name":"disabled","enabled":false,"feature_enabled":true},"impressionData":false},{"name":"workos-enabled","enabled":true,"variant":{"name":"disabled","enabled":false,"feature_enabled":true},"impressionData":false},{"name":"approve-review-flow","enabled":true,"variant":{"name":"disabled","enabled":false,"feature_enabled":true},"impressionData":false},{"name":"editor-metadata","enabled":true,"variant":{"name":"disabled","enabled":false,"feature_enabled":true},"impressionData":false},{"name":"sso-setup-deadline-banner","enabled":true,"variant":{"name":"disabled","enabled":false,"feature_enabled":true},"impressionData":false},{"name":"turbopuffer-serve","enabled":true,"variant":{"name":"disabled","enabled":false,"feature_enabled":true},"impressionData":false},{"name":"live-agents","enabled":true,"variant":{"name":"disabled","enabled":false,"feature_enabled":true},"impressionData":false},{"name":"llms-txt-v2","enabled":true,"variant":{"name":"disabled","enabled":false,"feature_enabled":true},"impressionData":false}],"googleFontsInlined":false},"customCss":[],"page":{"pagePath":"guides/ai-memory","slug":"/guides/ai-memory","pageMetadata":{"title":"AI Memory","description":null,"href":"/guides/ai-memory"},"pageType":"default","mdx":{"mdxSource":{"compiledSource":"\"use strict\";const{Fragment:_Fragment,jsx:_jsx,jsxs:_jsxs}=arguments[0];const{useMDXComponents:_provideComponents}=arguments[0];function _createMdxContent(props){const _components={a:`a`,code:`code`,li:`li`,p:`p`,pre:`pre`,span:`span`,strong:`strong`,ul:`ul`,..._provideComponents(),...props.components},{CodeBlock,Heading,Info,Warning}=_components;if(!CodeBlock)_missingMdxReference(`CodeBlock`,true);if(!Heading)_missingMdxReference(`Heading`,true);if(!Info)_missingMdxReference(`Info`,true);if(!Warning)_missingMdxReference(`Warning`,true);return _jsxs(_Fragment,{children:[_jsx(_components.p,{children:`AI agents start every session from scratch. A persistent memory system lets agents store lessons and retrieve relevant context via vector search. Turso’s embedded database with built-in vector functions makes this possible in a single local file.`}),`\n`,_jsx(_components.p,{children:`This guide covers the schema and queries for building a memory system with vector similarity search.`}),`\n`,_jsx(Heading,{level:`2`,id:`schema`,children:`Schema`}),`\n`,_jsx(_components.p,{children:`At its simplest, a memory system needs just two tables: one for memories and one for tracking which memories were used in which tasks.`}),`\n`,_jsx(CodeBlock,{filename:``,numberOfLines:`18`,language:`sql`,children:_jsx(_components.pre,{className:`shiki shiki-themes github-light-default dark-plus`,style:{backgroundColor:`#ffffff`,\"--shiki-dark-bg\":`#0B0C0E`,color:`#1f2328`,\"--shiki-dark\":`#D4D4D4`},language:`sql`,children:_jsxs(_components.code,{language:`sql`,numberOfLines:`18`,children:[_jsxs(_components.span,{className:`line`,children:[_jsx(_components.span,{style:{color:`#CF222E`,\"--shiki-dark\":`#569CD6`},children:`CREATE`}),_jsx(_components.span,{style:{color:`#CF222E`,\"--shiki-dark\":`#569CD6`},children:` TABLE`}),_jsx(_components.span,{style:{color:`#8250DF`,\"--shiki-dark\":`#DCDCAA`},children:` IF`}),_jsx(_components.span,{style:{color:`#CF222E`,\"--shiki-dark\":`#569CD6`},children:` NOT`}),_jsx(_components.span,{style:{color:`#CF222E`,\"--shiki-dark\":`#569CD6`},children:` EXISTS`}),_jsx(_components.span,{style:{color:`#1F2328`,\"--shiki-dark\":`#D4D4D4`},children:` memories (`})]}),`\n`,_jsxs(_components.span,{className:`line`,children:[_jsx(_components.span,{style:{color:`#1F2328`,\"--shiki-dark\":`#D4D4D4`},children:` id `}),_jsx(_components.span,{style:{color:`#CF222E`,\"--shiki-dark\":`#569CD6`},children:`TEXT`}),_jsx(_components.span,{style:{color:`#CF222E`,\"--shiki-dark\":`#569CD6`},children:` PRIMARY KEY`}),_jsx(_components.span,{style:{color:`#1F2328`,\"--shiki-dark\":`#D4D4D4`},children:`,`})]}),`\n`,_jsxs(_components.span,{className:`line`,children:[_jsx(_components.span,{style:{color:`#1F2328`,\"--shiki-dark\":`#D4D4D4`},children:` content `}),_jsx(_components.span,{style:{color:`#CF222E`,\"--shiki-dark\":`#569CD6`},children:`TEXT`}),_jsx(_components.span,{style:{color:`#CF222E`,\"--shiki-dark\":`#569CD6`},children:` NOT NULL`}),_jsx(_components.span,{style:{color:`#1F2328`,\"--shiki-dark\":`#D4D4D4`},children:`,`})]}),`\n`,_jsxs(_components.span,{className:`line`,children:[_jsx(_components.span,{style:{color:`#1F2328`,\"--shiki-dark\":`#D4D4D4`},children:` embedding F8_BLOB(`}),_jsx(_components.span,{style:{color:`#0550AE`,\"--shiki-dark\":`#B5CEA8`},children:`384`}),_jsx(_components.span,{style:{color:`#1F2328`,\"--shiki-dark\":`#D4D4D4`},children:`),`})]}),`\n`,_jsxs(_components.span,{className:`line`,children:[_jsx(_components.span,{style:{color:`#1F2328`,\"--shiki-dark\":`#D4D4D4`},children:` category `}),_jsx(_components.span,{style:{color:`#CF222E`,\"--shiki-dark\":`#569CD6`},children:`TEXT`}),_jsx(_components.span,{style:{color:`#CF222E`,\"--shiki-dark\":`#569CD6`},children:` NOT NULL`}),_jsx(_components.span,{style:{color:`#1F2328`,\"--shiki-dark\":`#D4D4D4`},children:`,`})]}),`\n`,_jsxs(_components.span,{className:`line`,children:[_jsx(_components.span,{style:{color:`#1F2328`,\"--shiki-dark\":`#D4D4D4`},children:` created_at `}),_jsx(_components.span,{style:{color:`#CF222E`,\"--shiki-dark\":`#569CD6`},children:`INTEGER`}),_jsx(_components.span,{style:{color:`#CF222E`,\"--shiki-dark\":`#569CD6`},children:` NOT NULL`}),_jsx(_components.span,{style:{color:`#1F2328`,\"--shiki-dark\":`#D4D4D4`},children:`,`})]}),`\n`,_jsxs(_components.span,{className:`line`,children:[_jsx(_components.span,{style:{color:`#1F2328`,\"--shiki-dark\":`#D4D4D4`},children:` last_retrieved `}),_jsx(_components.span,{style:{color:`#CF222E`,\"--shiki-dark\":`#569CD6`},children:`INTEGER`}),_jsx(_components.span,{style:{color:`#1F2328`,\"--shiki-dark\":`#D4D4D4`},children:`,`})]}),`\n`,_jsxs(_components.span,{className:`line`,children:[_jsx(_components.span,{style:{color:`#1F2328`,\"--shiki-dark\":`#D4D4D4`},children:` retrieval_count `}),_jsx(_components.span,{style:{color:`#CF222E`,\"--shiki-dark\":`#569CD6`},children:`INTEGER`}),_jsx(_components.span,{style:{color:`#CF222E`,\"--shiki-dark\":`#569CD6`},children:` DEFAULT`}),_jsx(_components.span,{style:{color:`#0550AE`,\"--shiki-dark\":`#B5CEA8`},children:` 0`}),_jsx(_components.span,{style:{color:`#1F2328`,\"--shiki-dark\":`#D4D4D4`},children:`,`})]}),`\n`,_jsxs(_components.span,{className:`line`,children:[_jsx(_components.span,{style:{color:`#1F2328`,\"--shiki-dark\":`#D4D4D4`},children:` source_task `}),_jsx(_components.span,{style:{color:`#CF222E`,\"--shiki-dark\":`#569CD6`},children:`TEXT`})]}),`\n`,_jsx(_components.span,{className:`line`,children:_jsx(_components.span,{style:{color:`#1F2328`,\"--shiki-dark\":`#D4D4D4`},children:`);`})}),`\n`,_jsx(_components.span,{className:`line`}),`\n`,_jsxs(_components.span,{className:`line`,children:[_jsx(_components.span,{style:{color:`#CF222E`,\"--shiki-dark\":`#569CD6`},children:`CREATE`}),_jsx(_components.span,{style:{color:`#CF222E`,\"--shiki-dark\":`#569CD6`},children:` TABLE`}),_jsx(_components.span,{style:{color:`#8250DF`,\"--shiki-dark\":`#DCDCAA`},children:` IF`}),_jsx(_components.span,{style:{color:`#CF222E`,\"--shiki-dark\":`#569CD6`},children:` NOT`}),_jsx(_components.span,{style:{color:`#CF222E`,\"--shiki-dark\":`#569CD6`},children:` EXISTS`}),_jsx(_components.span,{style:{color:`#1F2328`,\"--shiki-dark\":`#D4D4D4`},children:` tasks (`})]}),`\n`,_jsxs(_components.span,{className:`line`,children:[_jsx(_components.span,{style:{color:`#1F2328`,\"--shiki-dark\":`#D4D4D4`},children:` id `}),_jsx(_components.span,{style:{color:`#CF222E`,\"--shiki-dark\":`#569CD6`},children:`TEXT`}),_jsx(_components.span,{style:{color:`#CF222E`,\"--shiki-dark\":`#569CD6`},children:` PRIMARY KEY`}),_jsx(_components.span,{style:{color:`#1F2328`,\"--shiki-dark\":`#D4D4D4`},children:`,`})]}),`\n`,_jsxs(_components.span,{className:`line`,children:[_jsx(_components.span,{style:{color:`#CF222E`,\"--shiki-dark\":`#569CD6`},children:` description`}),_jsx(_components.span,{style:{color:`#CF222E`,\"--shiki-dark\":`#569CD6`},children:` TEXT`}),_jsx(_components.span,{style:{color:`#1F2328`,\"--shiki-dark\":`#D4D4D4`},children:`,`})]}),`\n`,_jsxs(_components.span,{className:`line`,children:[_jsx(_components.span,{style:{color:`#1F2328`,\"--shiki-dark\":`#D4D4D4`},children:` embedding F8_BLOB(`}),_jsx(_components.span,{style:{color:`#0550AE`,\"--shiki-dark\":`#B5CEA8`},children:`384`}),_jsx(_components.span,{style:{color:`#1F2328`,\"--shiki-dark\":`#D4D4D4`},children:`),`})]}),`\n`,_jsxs(_components.span,{className:`line`,children:[_jsx(_components.span,{style:{color:`#1F2328`,\"--shiki-dark\":`#D4D4D4`},children:` started_at `}),_jsx(_components.span,{style:{color:`#CF222E`,\"--shiki-dark\":`#569CD6`},children:`INTEGER`}),_jsx(_components.span,{style:{color:`#1F2328`,\"--shiki-dark\":`#D4D4D4`},children:`,`})]}),`\n`,_jsxs(_components.span,{className:`line`,children:[_jsx(_components.span,{style:{color:`#1F2328`,\"--shiki-dark\":`#D4D4D4`},children:` finished_at `}),_jsx(_components.span,{style:{color:`#CF222E`,\"--shiki-dark\":`#569CD6`},children:`INTEGER`})]}),`\n`,_jsx(_components.span,{className:`line`,children:_jsx(_components.span,{style:{color:`#1F2328`,\"--shiki-dark\":`#D4D4D4`},children:`);`})}),`\n`]})})}),`\n`,_jsxs(_components.ul,{children:[`\n`,_jsxs(_components.li,{children:[_jsx(_components.strong,{children:_jsx(_components.code,{children:`memories`})}),` stores each lesson with its vector embedding and a category (e.g. `,_jsx(_components.code,{children:`correction`}),`, `,_jsx(_components.code,{children:`insight`}),`, `,_jsx(_components.code,{children:`user`}),`, `,_jsx(_components.code,{children:`discovery`}),`). The embedding is a 384-dimensional int8-quantized vector (`,_jsx(_components.code,{children:`F8_BLOB(384)`}),`).`]}),`\n`,_jsxs(_components.li,{children:[_jsx(_components.strong,{children:_jsx(_components.code,{children:`tasks`})}),` records each task the agent worked on, linking task descriptions to their embeddings for retrieval.`]}),`\n`]}),`\n`,_jsx(Heading,{level:`2`,id:`connecting`,children:`Connecting`}),`\n`,_jsx(CodeBlock,{filename:``,numberOfLines:`4`,language:`javascript`,children:_jsx(_components.pre,{className:`shiki shiki-themes github-light-default dark-plus`,style:{backgroundColor:`#ffffff`,\"--shiki-dark-bg\":`#0B0C0E`,color:`#1f2328`,\"--shiki-dark\":`#D4D4D4`},language:`javascript`,children:_jsxs(_components.code,{language:`javascript`,numberOfLines:`4`,children:[_jsxs(_components.span,{className:`line`,children:[_jsx(_components.span,{style:{color:`#CF222E`,\"--shiki-dark\":`#C586C0`},children:`import`}),_jsx(_components.span,{style:{color:`#1F2328`,\"--shiki-dark\":`#D4D4D4`},children:` { connect } `}),_jsx(_components.span,{style:{color:`#CF222E`,\"--shiki-dark\":`#C586C0`},children:`from`}),_jsx(_components.span,{style:{color:`#0A3069`,\"--shiki-dark\":`#CE9178`},children:` \"@tursodatabase/database\"`}),_jsx(_components.span,{style:{color:`#1F2328`,\"--shiki-dark\":`#D4D4D4`},children:`;`})]}),`\n`,_jsx(_components.span,{className:`line`}),`\n`,_jsxs(_components.span,{className:`line`,children:[_jsx(_components.span,{style:{color:`#CF222E`,\"--shiki-dark\":`#C586C0`},children:`const`}),_jsx(_components.span,{style:{color:`#1F2328`,\"--shiki-dark\":`#D4D4D4`},children:` db = `}),_jsx(_components.span,{style:{color:`#CF222E`,\"--shiki-dark\":`#C586C0`},children:`await`}),_jsx(_components.span,{style:{color:`#1F2328`,\"--shiki-dark\":`#D4D4D4`},children:` connect(`}),_jsx(_components.span,{style:{color:`#0A3069`,\"--shiki-dark\":`#CE9178`},children:`\".memory/agent.db\"`}),_jsx(_components.span,{style:{color:`#1F2328`,\"--shiki-dark\":`#D4D4D4`},children:`);`})]}),`\n`,_jsxs(_components.span,{className:`line`,children:[_jsx(_components.span,{style:{color:`#CF222E`,\"--shiki-dark\":`#C586C0`},children:`await`}),_jsx(_components.span,{style:{color:`#1F2328`,\"--shiki-dark\":`#D4D4D4`},children:` db.exec(SCHEMA);`})]}),`\n`]})})}),`\n`,_jsx(Heading,{level:`2`,id:`storing-a-memory`,children:`Storing a memory`}),`\n`,_jsxs(_components.p,{children:[`When the agent learns something — a correction, a user preference, an insight — store it with an embedding. Use `,_jsx(_components.code,{children:`vector8()`}),` to convert a JSON float array into an int8-quantized vector:`]}),`\n`,_jsx(CodeBlock,{filename:``,numberOfLines:`2`,language:`sql`,children:_jsx(_components.pre,{className:`shiki shiki-themes github-light-default dark-plus`,style:{backgroundColor:`#ffffff`,\"--shiki-dark-bg\":`#0B0C0E`,color:`#1f2328`,\"--shiki-dark\":`#D4D4D4`},language:`sql`,children:_jsxs(_components.code,{language:`sql`,numberOfLines:`2`,children:[_jsxs(_components.span,{className:`line`,children:[_jsx(_components.span,{style:{color:`#CF222E`,\"--shiki-dark\":`#569CD6`},children:`INSERT INTO`}),_jsx(_components.span,{style:{color:`#1F2328`,\"--shiki-dark\":`#D4D4D4`},children:` memories (id, content, embedding, category, created_at, source_task)`})]}),`\n`,_jsxs(_components.span,{className:`line`,children:[_jsx(_components.span,{style:{color:`#CF222E`,\"--shiki-dark\":`#569CD6`},children:`VALUES`}),_jsx(_components.span,{style:{color:`#1F2328`,\"--shiki-dark\":`#D4D4D4`},children:` (?, ?, vector8(?), ?, ?, ?);`})]}),`\n`]})})}),`\n`,_jsxs(_components.p,{children:[`The parameter for `,_jsx(_components.code,{children:`vector8()`}),` is a JSON-stringified float array (e.g. 384 dimensions from a model like `,_jsx(_components.code,{children:`all-MiniLM-L6-v2`}),`). Turso handles the quantization internally:`]}),`\n`,_jsx(CodeBlock,{filename:``,numberOfLines:`4`,language:`javascript`,children:_jsx(_components.pre,{className:`shiki shiki-themes github-light-default dark-plus`,style:{backgroundColor:`#ffffff`,\"--shiki-dark-bg\":`#0B0C0E`,color:`#1f2328`,\"--shiki-dark\":`#D4D4D4`},language:`javascript`,children:_jsxs(_components.code,{language:`javascript`,numberOfLines:`4`,children:[_jsxs(_components.span,{className:`line`,children:[_jsx(_components.span,{style:{color:`#1F2328`,\"--shiki-dark\":`#D4D4D4`},children:`db.prepare(`}),_jsx(_components.span,{style:{color:`#0A3069`,\"--shiki-dark\":`#CE9178`},children:\"`\"})]}),`\n`,_jsx(_components.span,{className:`line`,children:_jsx(_components.span,{style:{color:`#0A3069`,\"--shiki-dark\":`#CE9178`},children:` INSERT INTO memories (id, content, embedding, category, created_at)`})}),`\n`,_jsx(_components.span,{className:`line`,children:_jsx(_components.span,{style:{color:`#0A3069`,\"--shiki-dark\":`#CE9178`},children:` VALUES (?, ?, vector8(?), ?, ?)`})}),`\n`,_jsxs(_components.span,{className:`line`,children:[_jsx(_components.span,{style:{color:`#0A3069`,\"--shiki-dark\":`#CE9178`},children:\"`\"}),_jsx(_components.span,{style:{color:`#1F2328`,\"--shiki-dark\":`#D4D4D4`},children:`).run(id, content, JSON.stringify(Array.`}),_jsx(_components.span,{style:{color:`#CF222E`,\"--shiki-dark\":`#C586C0`},children:`from`}),_jsx(_components.span,{style:{color:`#1F2328`,\"--shiki-dark\":`#D4D4D4`},children:`(embedding)), `}),_jsx(_components.span,{style:{color:`#0A3069`,\"--shiki-dark\":`#CE9178`},children:`\"correction\"`}),_jsx(_components.span,{style:{color:`#1F2328`,\"--shiki-dark\":`#D4D4D4`},children:`, Date.now());`})]}),`\n`]})})}),`\n`,_jsx(Heading,{level:`2`,id:`retrieving-relevant-memories`,children:`Retrieving relevant memories`}),`\n`,_jsxs(_components.p,{children:[`When a new task starts, find the most relevant memories using Turso’s `,_jsx(_components.code,{children:`vector_distance_cos()`}),` function:`]}),`\n`,_jsx(CodeBlock,{filename:``,numberOfLines:`6`,language:`sql`,children:_jsx(_components.pre,{className:`shiki shiki-themes github-light-default dark-plus`,style:{backgroundColor:`#ffffff`,\"--shiki-dark-bg\":`#0B0C0E`,color:`#1f2328`,\"--shiki-dark\":`#D4D4D4`},language:`sql`,children:_jsxs(_components.code,{language:`sql`,numberOfLines:`6`,children:[_jsxs(_components.span,{className:`line`,children:[_jsx(_components.span,{style:{color:`#CF222E`,\"--shiki-dark\":`#569CD6`},children:`SELECT`}),_jsx(_components.span,{style:{color:`#1F2328`,\"--shiki-dark\":`#D4D4D4`},children:` id, content, category, created_at, retrieval_count,`})]}),`\n`,_jsxs(_components.span,{className:`line`,children:[_jsx(_components.span,{style:{color:`#1F2328`,\"--shiki-dark\":`#D4D4D4`},children:` vector_distance_cos(embedding, vector8(?)) `}),_jsx(_components.span,{style:{color:`#CF222E`,\"--shiki-dark\":`#569CD6`},children:`AS`}),_jsx(_components.span,{style:{color:`#1F2328`,\"--shiki-dark\":`#D4D4D4`},children:` distance`})]}),`\n`,_jsxs(_components.span,{className:`line`,children:[_jsx(_components.span,{style:{color:`#CF222E`,\"--shiki-dark\":`#569CD6`},children:`FROM`}),_jsx(_components.span,{style:{color:`#1F2328`,\"--shiki-dark\":`#D4D4D4`},children:` memories`})]}),`\n`,_jsxs(_components.span,{className:`line`,children:[_jsx(_components.span,{style:{color:`#CF222E`,\"--shiki-dark\":`#569CD6`},children:`WHERE`}),_jsx(_components.span,{style:{color:`#1F2328`,\"--shiki-dark\":`#D4D4D4`},children:` embedding `}),_jsx(_components.span,{style:{color:`#CF222E`,\"--shiki-dark\":`#569CD6`},children:`IS NOT NULL`})]}),`\n`,_jsxs(_components.span,{className:`line`,children:[_jsx(_components.span,{style:{color:`#CF222E`,\"--shiki-dark\":`#569CD6`},children:`ORDER BY`}),_jsx(_components.span,{style:{color:`#1F2328`,\"--shiki-dark\":`#D4D4D4`},children:` distance `}),_jsx(_components.span,{style:{color:`#CF222E`,\"--shiki-dark\":`#569CD6`},children:`ASC`})]}),`\n`,_jsxs(_components.span,{className:`line`,children:[_jsx(_components.span,{style:{color:`#CF222E`,\"--shiki-dark\":`#569CD6`},children:`LIMIT`}),_jsx(_components.span,{style:{color:`#1F2328`,\"--shiki-dark\":`#D4D4D4`},children:` ?;`})]}),`\n`]})})}),`\n`,_jsx(Info,{children:_jsxs(_components.p,{children:[_jsx(_components.code,{children:`vector_distance_cos()`}),` returns cosine `,_jsx(_components.strong,{children:`distance`}),` (0 = identical, 2 = opposite). Lower is better. To convert to similarity: `,_jsx(_components.code,{children:`1.0 - distance`}),`.`]})}),`\n`,_jsx(_components.p,{children:`After retrieval, update the memory’s metadata:`}),`\n`,_jsx(CodeBlock,{filename:``,numberOfLines:`2`,language:`sql`,children:_jsx(_components.pre,{className:`shiki shiki-themes github-light-default dark-plus`,style:{backgroundColor:`#ffffff`,\"--shiki-dark-bg\":`#0B0C0E`,color:`#1f2328`,\"--shiki-dark\":`#D4D4D4`},language:`sql`,children:_jsxs(_components.code,{language:`sql`,numberOfLines:`2`,children:[_jsxs(_components.span,{className:`line`,children:[_jsx(_components.span,{style:{color:`#CF222E`,\"--shiki-dark\":`#569CD6`},children:`UPDATE`}),_jsx(_components.span,{style:{color:`#1F2328`,\"--shiki-dark\":`#D4D4D4`},children:` memories `}),_jsx(_components.span,{style:{color:`#CF222E`,\"--shiki-dark\":`#569CD6`},children:`SET`}),_jsx(_components.span,{style:{color:`#1F2328`,\"--shiki-dark\":`#D4D4D4`},children:` last_retrieved `}),_jsx(_components.span,{style:{color:`#CF222E`,\"--shiki-dark\":`#D4D4D4`},children:`=`}),_jsx(_components.span,{style:{color:`#1F2328`,\"--shiki-dark\":`#D4D4D4`},children:` ?, retrieval_count `}),_jsx(_components.span,{style:{color:`#CF222E`,\"--shiki-dark\":`#D4D4D4`},children:`=`}),_jsx(_components.span,{style:{color:`#1F2328`,\"--shiki-dark\":`#D4D4D4`},children:` retrieval_count `}),_jsx(_components.span,{style:{color:`#CF222E`,\"--shiki-dark\":`#D4D4D4`},children:`+`}),_jsx(_components.span,{style:{color:`#0550AE`,\"--shiki-dark\":`#B5CEA8`},children:` 1`})]}),`\n`,_jsxs(_components.span,{className:`line`,children:[_jsx(_components.span,{style:{color:`#CF222E`,\"--shiki-dark\":`#569CD6`},children:`WHERE`}),_jsx(_components.span,{style:{color:`#1F2328`,\"--shiki-dark\":`#D4D4D4`},children:` id `}),_jsx(_components.span,{style:{color:`#CF222E`,\"--shiki-dark\":`#D4D4D4`},children:`=`}),_jsx(_components.span,{style:{color:`#1F2328`,\"--shiki-dark\":`#D4D4D4`},children:` ?;`})]}),`\n`]})})}),`\n`,_jsx(Heading,{level:`2`,id:`correcting-wrong-memories`,children:`Correcting wrong memories`}),`\n`,_jsx(_components.p,{children:`When a memory turns out to be wrong, delete it and store a corrected version:`}),`\n`,_jsx(CodeBlock,{filename:``,numberOfLines:`4`,language:`sql`,children:_jsx(_components.pre,{className:`shiki shiki-themes github-light-default dark-plus`,style:{backgroundColor:`#ffffff`,\"--shiki-dark-bg\":`#0B0C0E`,color:`#1f2328`,\"--shiki-dark\":`#D4D4D4`},language:`sql`,children:_jsxs(_components.code,{language:`sql`,numberOfLines:`4`,children:[_jsxs(_components.span,{className:`line`,children:[_jsx(_components.span,{style:{color:`#CF222E`,\"--shiki-dark\":`#569CD6`},children:`DELETE`}),_jsx(_components.span,{style:{color:`#CF222E`,\"--shiki-dark\":`#569CD6`},children:` FROM`}),_jsx(_components.span,{style:{color:`#1F2328`,\"--shiki-dark\":`#D4D4D4`},children:` memories `}),_jsx(_components.span,{style:{color:`#CF222E`,\"--shiki-dark\":`#569CD6`},children:`WHERE`}),_jsx(_components.span,{style:{color:`#1F2328`,\"--shiki-dark\":`#D4D4D4`},children:` id `}),_jsx(_components.span,{style:{color:`#CF222E`,\"--shiki-dark\":`#D4D4D4`},children:`=`}),_jsx(_components.span,{style:{color:`#1F2328`,\"--shiki-dark\":`#D4D4D4`},children:` ?;`})]}),`\n`,_jsx(_components.span,{className:`line`}),`\n`,_jsxs(_components.span,{className:`line`,children:[_jsx(_components.span,{style:{color:`#CF222E`,\"--shiki-dark\":`#569CD6`},children:`INSERT INTO`}),_jsx(_components.span,{style:{color:`#1F2328`,\"--shiki-dark\":`#D4D4D4`},children:` memories (id, content, embedding, category, created_at)`})]}),`\n`,_jsxs(_components.span,{className:`line`,children:[_jsx(_components.span,{style:{color:`#CF222E`,\"--shiki-dark\":`#569CD6`},children:`VALUES`}),_jsx(_components.span,{style:{color:`#1F2328`,\"--shiki-dark\":`#D4D4D4`},children:` (?, ?, vector8(?), `}),_jsx(_components.span,{style:{color:`#0A3069`,\"--shiki-dark\":`#CE9178`},children:`'correction'`}),_jsx(_components.span,{style:{color:`#1F2328`,\"--shiki-dark\":`#D4D4D4`},children:`, ?);`})]}),`\n`]})})}),`\n`,_jsx(Heading,{level:`2`,id:`task-tracking`,children:`Task tracking`}),`\n`,_jsx(_components.p,{children:`Record tasks to understand what the agent has been working on:`}),`\n`,_jsx(CodeBlock,{filename:``,numberOfLines:`6`,language:`sql`,children:_jsx(_components.pre,{className:`shiki shiki-themes github-light-default dark-plus`,style:{backgroundColor:`#ffffff`,\"--shiki-dark-bg\":`#0B0C0E`,color:`#1f2328`,\"--shiki-dark\":`#D4D4D4`},language:`sql`,children:_jsxs(_components.code,{language:`sql`,numberOfLines:`6`,children:[_jsx(_components.span,{className:`line`,children:_jsx(_components.span,{style:{color:`#6E7781`,\"--shiki-dark\":`#6A9955`},children:`-- Start a task`})}),`\n`,_jsxs(_components.span,{className:`line`,children:[_jsx(_components.span,{style:{color:`#CF222E`,\"--shiki-dark\":`#569CD6`},children:`INSERT INTO`}),_jsx(_components.span,{style:{color:`#1F2328`,\"--shiki-dark\":`#D4D4D4`},children:` tasks (id, `}),_jsx(_components.span,{style:{color:`#CF222E`,\"--shiki-dark\":`#569CD6`},children:`description`}),_jsx(_components.span,{style:{color:`#1F2328`,\"--shiki-dark\":`#D4D4D4`},children:`, embedding, started_at)`})]}),`\n`,_jsxs(_components.span,{className:`line`,children:[_jsx(_components.span,{style:{color:`#CF222E`,\"--shiki-dark\":`#569CD6`},children:`VALUES`}),_jsx(_components.span,{style:{color:`#1F2328`,\"--shiki-dark\":`#D4D4D4`},children:` (?, ?, vector8(?), ?);`})]}),`\n`,_jsx(_components.span,{className:`line`}),`\n`,_jsx(_components.span,{className:`line`,children:_jsx(_components.span,{style:{color:`#6E7781`,\"--shiki-dark\":`#6A9955`},children:`-- Complete a task`})}),`\n`,_jsxs(_components.span,{className:`line`,children:[_jsx(_components.span,{style:{color:`#CF222E`,\"--shiki-dark\":`#569CD6`},children:`UPDATE`}),_jsx(_components.span,{style:{color:`#1F2328`,\"--shiki-dark\":`#D4D4D4`},children:` tasks `}),_jsx(_components.span,{style:{color:`#CF222E`,\"--shiki-dark\":`#569CD6`},children:`SET`}),_jsx(_components.span,{style:{color:`#1F2328`,\"--shiki-dark\":`#D4D4D4`},children:` finished_at `}),_jsx(_components.span,{style:{color:`#CF222E`,\"--shiki-dark\":`#D4D4D4`},children:`=`}),_jsx(_components.span,{style:{color:`#1F2328`,\"--shiki-dark\":`#D4D4D4`},children:` ? `}),_jsx(_components.span,{style:{color:`#CF222E`,\"--shiki-dark\":`#569CD6`},children:`WHERE`}),_jsx(_components.span,{style:{color:`#1F2328`,\"--shiki-dark\":`#D4D4D4`},children:` id `}),_jsx(_components.span,{style:{color:`#CF222E`,\"--shiki-dark\":`#D4D4D4`},children:`=`}),_jsx(_components.span,{style:{color:`#1F2328`,\"--shiki-dark\":`#D4D4D4`},children:` ?;`})]}),`\n`]})})}),`\n`,_jsx(Heading,{level:`2`,id:`purging-old-memories`,children:`Purging old memories`}),`\n`,_jsx(_components.p,{children:`Remove memories that have been around a long time but are never retrieved:`}),`\n`,_jsx(CodeBlock,{filename:``,numberOfLines:`2`,language:`sql`,children:_jsx(_components.pre,{className:`shiki shiki-themes github-light-default dark-plus`,style:{backgroundColor:`#ffffff`,\"--shiki-dark-bg\":`#0B0C0E`,color:`#1f2328`,\"--shiki-dark\":`#D4D4D4`},language:`sql`,children:_jsxs(_components.code,{language:`sql`,numberOfLines:`2`,children:[_jsxs(_components.span,{className:`line`,children:[_jsx(_components.span,{style:{color:`#CF222E`,\"--shiki-dark\":`#569CD6`},children:`DELETE`}),_jsx(_components.span,{style:{color:`#CF222E`,\"--shiki-dark\":`#569CD6`},children:` FROM`}),_jsx(_components.span,{style:{color:`#1F2328`,\"--shiki-dark\":`#D4D4D4`},children:` memories`})]}),`\n`,_jsxs(_components.span,{className:`line`,children:[_jsx(_components.span,{style:{color:`#CF222E`,\"--shiki-dark\":`#569CD6`},children:`WHERE`}),_jsx(_components.span,{style:{color:`#1F2328`,\"--shiki-dark\":`#D4D4D4`},children:` retrieval_count `}),_jsx(_components.span,{style:{color:`#CF222E`,\"--shiki-dark\":`#D4D4D4`},children:`=`}),_jsx(_components.span,{style:{color:`#0550AE`,\"--shiki-dark\":`#B5CEA8`},children:` 0`}),_jsx(_components.span,{style:{color:`#CF222E`,\"--shiki-dark\":`#569CD6`},children:` AND`}),_jsx(_components.span,{style:{color:`#1F2328`,\"--shiki-dark\":`#D4D4D4`},children:` created_at `}),_jsx(_components.span,{style:{color:`#CF222E`,\"--shiki-dark\":`#D4D4D4`},children:`\u003c`}),_jsx(_components.span,{style:{color:`#1F2328`,\"--shiki-dark\":`#D4D4D4`},children:` ?;`})]}),`\n`]})})}),`\n`,_jsx(Heading,{level:`2`,id:`key-design-points`,children:`Key design points`}),`\n`,_jsxs(_components.ul,{children:[`\n`,_jsxs(_components.li,{children:[_jsx(_components.strong,{children:`Vector search`}),` uses Turso’s `,_jsx(_components.code,{children:`vector8()`}),` and `,_jsx(_components.code,{children:`vector_distance_cos()`}),` for cosine similarity directly in SQL. Int8 quantization reduces storage by 75% compared to float32 with minimal impact on search quality.`]}),`\n`,_jsxs(_components.li,{children:[`For small per-project datasets, a full table scan with `,_jsx(_components.code,{children:`ORDER BY distance LIMIT k`}),` is fast enough without a vector index.`]}),`\n`,_jsx(_components.li,{children:`Everything runs in a single embedded database file with no external services.`}),`\n`]}),`\n`,_jsx(Heading,{level:`2`,id:`going-further`,children:`Going further`}),`\n`,_jsx(_components.p,{children:`The schema above is deliberately minimal. In practice you will want to experiment with strategies for keeping the memory store useful as it grows. Some ideas:`}),`\n`,_jsxs(_components.ul,{children:[`\n`,_jsxs(_components.li,{children:[_jsx(_components.strong,{children:`Weighted memories`}),` — add a `,_jsx(_components.code,{children:`weight REAL`}),` column and boost or penalize memories based on whether the agent found them useful after retrieval. Use an exponential moving average to update weights over time.`]}),`\n`,_jsxs(_components.li,{children:[_jsx(_components.strong,{children:`Time decay`}),` — multiply a recency factor into the retrieval ranking so that stale memories rank lower. For example: `,_jsx(_components.code,{children:`(1.0 - distance) * POWER(0.95, days_since_last_retrieved)`}),`.`]}),`\n`,_jsxs(_components.li,{children:[_jsx(_components.strong,{children:`Garbage collection`}),` — periodically remove memories with low weight and high retrieval count (retrieved often, never useful), or memories that have never been retrieved after a threshold period.`]}),`\n`,_jsxs(_components.li,{children:[_jsx(_components.strong,{children:`Task scoring`}),` — track outcome metrics (tokens used, errors, corrections) per task and use them to compute a credit signal for the memories that were retrieved during that task.`]}),`\n`,_jsxs(_components.li,{children:[_jsx(_components.strong,{children:`Retrieval attribution`}),` — add a join table `,_jsx(_components.code,{children:`(memory_id, task_id, similarity, credit)`}),` to record exactly which memories were used in each task and how they were rated.`]}),`\n`]}),`\n`,_jsx(Warning,{children:_jsx(_components.p,{children:`There is no single best approach — the right strategy depends on your agent’s workload, how often memories are created, and how aggressively you want to prune. Start simple and iterate.`})}),`\n`,_jsx(Heading,{level:`2`,id:`example`,children:`Example`}),`\n`,_jsxs(_components.p,{children:[_jsx(_components.a,{href:`https://github.com/glommer/memelord`,children:`memelord`}),` is an MCP server and hooks system that implements this pattern with reinforcement learning and weight decay as a persistent memory layer for AI coding agents.`]})]})}function MDXContent(props={}){const{wrapper:MDXLayout}={..._provideComponents(),...props.components};return MDXLayout?_jsx(MDXLayout,{...props,children:_jsx(_createMdxContent,{...props})}):_createMdxContent(props)}return{default:MDXContent};function _missingMdxReference(id,component){throw new Error(`Expected `+(component?`component`:`object`)+\" `\"+id+\"` to be defined: you likely forgot to import, pass, or provide it.\")}","frontmatter":{},"scope":{"config":{},"pageMetadata":{"title":"AI Memory","description":null,"href":"/guides/ai-memory"}}},"mdxSourceWithNoJs":null},"mdxExtracts":{"tableOfContents":[{"title":"Schema","slug":"schema","depth":2,"children":[]},{"title":"Connecting","slug":"connecting","depth":2,"children":[]},{"title":"Storing a memory","slug":"storing-a-memory","depth":2,"children":[]},{"title":"Retrieving relevant memories","slug":"retrieving-relevant-memories","depth":2,"children":[]},{"title":"Correcting wrong memories","slug":"correcting-wrong-memories","depth":2,"children":[]},{"title":"Task tracking","slug":"task-tracking","depth":2,"children":[]},{"title":"Purging old memories","slug":"purging-old-memories","depth":2,"children":[]},{"title":"Key design points","slug":"key-design-points","depth":2,"children":[]},{"title":"Going further","slug":"going-further","depth":2,"children":[]},{"title":"Example","slug":"example","depth":2,"children":[]}],"codeExamples":{}},"jsLinks":["/_mintlify/custom/turso/37ed2bd0c4d50890/prefetch-viz-v3.js","/_mintlify/custom/turso/30fa7bada8cea66f/segment-viz-v2.js"],"subdomain":"turso","siteName":"Turso","title":"AI Memory - Turso","navMaps":{"firstHrefInVersion":{},"firstHrefInLanguage":{}},"lastModified":"2026-10-27T15:01:37.884Z","originalFileLocation":"guides/ai-memory.mdx"},"headHtml":"\u003cscript data-mint-head>(function(){var d=document.documentElement,p=null;if(!p){try{p=localStorage.getItem(\"isDarkMode\")}catch(e){}if(p==='true')p='dark';else if(p==='false')p='light';if(p!=='light'\u0026\u0026p!=='dark'\u0026\u0026p!=='system')p=\"dark\"}var r=p==='system'?(matchMedia(\"(prefers-color-scheme: dark)\").matches?'dark':'light'):p;d.classList.remove(r==='dark'?'light':'dark');d.classList.add(r);d.setAttribute(\"data-theme-preference\",p)})();\u003c/script>\u003cmeta data-mint-head name=\"application-name\" content=\"Turso\">\u003cmeta data-mint-head name=\"generator\" content=\"Mintlify\">\u003clink data-mint-head rel=\"icon\" type=\"image/png\" sizes=\"192x192\" href=\"/mintlify-assets/_mintlify/favicons/turso/Iu9g4qZd-KNb0pxr/_generated/favicon/android-chrome-192x192.png\" media=\"(prefers-color-scheme: light)\">\u003clink data-mint-head rel=\"apple-touch-icon\" type=\"image/png\" sizes=\"180x180\" href=\"/mintlify-assets/_mintlify/favicons/turso/Iu9g4qZd-KNb0pxr/_generated/favicon/apple-touch-icon.png\">\u003clink data-mint-head rel=\"icon\" type=\"image/png\" sizes=\"16x16\" href=\"/mintlify-assets/_mintlify/favicons/turso/Iu9g4qZd-KNb0pxr/_generated/favicon/favicon-16x16.png\" media=\"(prefers-color-scheme: light)\">\u003clink data-mint-head rel=\"icon\" type=\"image/png\" sizes=\"32x32\" href=\"/mintlify-assets/_mintlify/favicons/turso/Iu9g4qZd-KNb0pxr/_generated/favicon/favicon-32x32.png\" media=\"(prefers-color-scheme: light)\">\u003clink data-mint-head rel=\"shortcut icon\" type=\"image/x-icon\" href=\"/mintlify-assets/_mintlify/favicons/turso/Iu9g4qZd-KNb0pxr/_generated/favicon/favicon.ico\" media=\"(prefers-color-scheme: light)\">\u003clink data-mint-head rel=\"icon\" type=\"image/png\" sizes=\"192x192\" href=\"/mintlify-assets/_mintlify/favicons/turso/Iu9g4qZd-KNb0pxr/_generated/favicon-dark/android-chrome-192x192.png\" media=\"(prefers-color-scheme: dark)\">\u003clink data-mint-head rel=\"icon\" type=\"image/png\" sizes=\"16x16\" href=\"/mintlify-assets/_mintlify/favicons/turso/Iu9g4qZd-KNb0pxr/_generated/favicon-dark/favicon-16x16.png\" media=\"(prefers-color-scheme: dark)\">\u003clink data-mint-head rel=\"icon\" type=\"image/png\" sizes=\"32x32\" href=\"/mintlify-assets/_mintlify/favicons/turso/Iu9g4qZd-KNb0pxr/_generated/favicon-dark/favicon-32x32.png\" media=\"(prefers-color-scheme: dark)\">\u003clink data-mint-head rel=\"shortcut icon\" type=\"image/x-icon\" href=\"/mintlify-assets/_mintlify/favicons/turso/Iu9g4qZd-KNb0pxr/_generated/favicon-dark/favicon.ico\" media=\"(prefers-color-scheme: dark)\">\u003cmeta data-mint-head property=\"og:title\" content=\"AI Memory - Turso\">\u003cmeta data-mint-head property=\"og:site_name\" content=\"Turso\">\u003cmeta data-mint-head property=\"og:url\" content=\"https://docs.turso.tech/guides/ai-memory\">\u003cmeta data-mint-head property=\"og:type\" content=\"website\">\u003cmeta data-mint-head property=\"og:image\" content=\"https://turso.mintlify.site/_mintlify/api/og/v2?division=AI+and+Agents\u0026amp;title=AI+Memory\u0026amp;theme=799b9dd861205471af889b1f\u0026amp;signature=c7d75ecdd126d314d448da95182b159d28164ce909ac7d94e2552139c3b20ad8\">\u003cmeta data-mint-head property=\"og:image:width\" content=\"1200\">\u003cmeta data-mint-head property=\"og:image:height\" content=\"630\">\u003cmeta data-mint-head name=\"twitter:card\" content=\"summary_large_image\">\u003cmeta data-mint-head name=\"twitter:title\" content=\"AI Memory - Turso\">\u003cmeta data-mint-head name=\"twitter:image\" content=\"https://turso.mintlify.site/_mintlify/api/og/v2?division=AI+and+Agents\u0026amp;title=AI+Memory\u0026amp;theme=799b9dd861205471af889b1f\u0026amp;signature=c7d75ecdd126d314d448da95182b159d28164ce909ac7d94e2552139c3b20ad8\">\u003cmeta data-mint-head name=\"twitter:image:width\" content=\"1200\">\u003cmeta data-mint-head name=\"twitter:image:height\" content=\"630\">\u003cmeta data-mint-head name=\"msapplication-config\" content=\"/mintlify-assets/_mintlify/favicons/turso/Iu9g4qZd-KNb0pxr/_generated/favicon/browserconfig.xml\">\u003cmeta data-mint-head name=\"apple-mobile-web-app-title\" content=\"Turso\">\u003cmeta data-mint-head name=\"msapplication-TileColor\" content=\"#1EBCA1\">\u003clink data-mint-head rel=\"canonical\" href=\"https://docs.turso.tech/guides/ai-memory\">\u003clink data-mint-head rel=\"alternate\" type=\"application/xml\" href=\"/sitemap.xml\">\u003clink data-mint-head rel=\"alternate\" type=\"text/markdown\" href=\"/guides/ai-memory.md\">\u003cscript data-mint-head type=\"application/ld+json\">{\"@context\":\"https://schema.org\",\"@graph\":[{\"@type\":\"Organization\",\"@id\":\"https://docs.turso.tech/#organization\",\"name\":\"Turso\",\"url\":\"https://docs.turso.tech\",\"logo\":{\"@type\":\"ImageObject\",\"url\":\"https://mintcdn.com/turso/0gN7SecUGL1uVO9v/logo/turso-light.svg?fit=max\u0026auto=format\u0026n=0gN7SecUGL1uVO9v\u0026q=85\u0026s=089d09358d8f5a3e3815861a7419a616\"}},{\"@type\":\"WebSite\",\"@id\":\"https://docs.turso.tech#website\",\"name\":\"Turso\",\"url\":\"https://docs.turso.tech\",\"publisher\":{\"@id\":\"https://docs.turso.tech/#organization\"}},{\"@type\":\"WebPage\",\"@id\":\"https://docs.turso.tech/guides/ai-memory#webpage\",\"url\":\"https://docs.turso.tech/guides/ai-memory\",\"name\":\"AI Memory\",\"dateModified\":\"2026-10-27T15:01:37.884Z\",\"isPartOf\":{\"@id\":\"https://docs.turso.tech#website\"},\"breadcrumb\":{\"@id\":\"https://docs.turso.tech/guides/ai-memory#breadcrumb\"}},{\"@type\":\"BreadcrumbList\",\"@id\":\"https://docs.turso.tech/guides/ai-memory#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"AI and Agents\",\"item\":\"https://docs.turso.tech/guides/agent-databases\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"AI Memory\",\"item\":\"https://docs.turso.tech/guides/ai-memory\"}]},{\"@type\":[\"Article\",\"TechArticle\"],\"@id\":\"https://docs.turso.tech/guides/ai-memory#article\",\"headline\":\"AI Memory\",\"name\":\"AI Memory\",\"url\":\"https://docs.turso.tech/guides/ai-memory\",\"mainEntityOfPage\":{\"@id\":\"https://docs.turso.tech/guides/ai-memory#webpage\"},\"dateModified\":\"2026-10-27T15:01:37.884Z\",\"publisher\":{\"@id\":\"https://docs.turso.tech/#organization\"},\"isPartOf\":{\"@id\":\"https://docs.turso.tech#website\"}}]}\u003c/script>\u003cscript data-mint-head type=\"application/ld+json\">{\"@context\":\"https://schema.org\",\"@type\":\"WebSite\",\"name\":\"Turso\",\"publisher\":{\"@type\":\"Organization\",\"name\":\"Turso\"},\"provider\":{\"@type\":\"Organization\",\"name\":\"Mintlify\",\"url\":\"https://mintlify.com\"}}\u003c/script>","agentPointerHref":"/guides/ai-memory.md","siteName":"Turso","nav":{"global":{"anchors":[{"anchor":"Turso Homepage","icon":"house","href":"https://turso.tech"},{"anchor":"Discord","icon":"discord","href":"https://tur.so/discord"},{"anchor":"GitHub","icon":"github","href":"https://github.com/tursodatabase/turso"}]},"tabs":[{"tab":"Turso Database","groups":[{"group":"Getting Started","pages":[{"title":"Welcome to Turso","sidebarTitle":"Introduction","href":"/introduction"},{"title":"Quickstart","href":"/tursodb/quickstart"},{"group":"Connect","pages":[{"title":"Connect to Turso using JavaScript","sidebarTitle":"JavaScript","href":"/connect/javascript"},{"title":"Connect to Turso using Python","sidebarTitle":"Python","href":"/connect/python"},{"title":"Connect to Turso using Go","sidebarTitle":"Go","href":"/connect/go"},{"title":"Connect to Turso using Rust","sidebarTitle":"Rust","href":"/connect/rust"},{"title":"Connect to Turso using Dart","sidebarTitle":"Dart","href":"/connect/dart"},{"title":"Connect to Turso using Java","sidebarTitle":"Java","href":"/connect/java"}]}]},{"group":"Fundamentals","pages":[{"title":"Change Data Capture","href":"/tursodb/cdc"},{"title":"Concurrent Writes","href":"/tursodb/concurrent-writes"},{"title":"Encryption","href":"/tursodb/encryption"},{"title":"Vector Search","href":"/guides/vector-search"},{"group":"Sync","pages":[{"title":"Usage","href":"/sync/usage"},{"title":"Local Sync Server","href":"/sync/local-sync-server"},{"title":"Partial sync","href":"/sync/partial"},{"title":"Conflict Resolution","href":"/sync/conflict-resolution"},{"title":"Checkpoint","href":"/sync/checkpoint"}]}]},{"group":"AI and Agents","pages":[{"title":"Agent Databases","description":null,"href":"/guides/agent-databases"},{"title":"AI Memory","href":"/guides/ai-memory"},{"title":"Code Indexing","description":null,"href":"/guides/code-indexing"},{"title":"Content Caching","href":"/guides/content-caching"},{"title":"Secrets Vault","href":"/guides/secrets-vault"}]},{"group":"CLI Reference","pages":[{"title":"Getting Started","sidebarTitle":"Getting Started","href":"/sql-reference/cli/getting-started"},{"title":"Command-Line Options","sidebarTitle":"Command-Line Options","href":"/sql-reference/cli/command-line-options"},{"title":"Shell Commands","sidebarTitle":"Shell Commands","href":"/sql-reference/cli/shell-commands"}]},{"group":"SQL Reference","pages":[{"title":"SQLite Compatibility","sidebarTitle":"Compatibility","href":"/sql-reference/compatibility"},{"title":"Data Types","sidebarTitle":"Data Types","href":"/sql-reference/data-types"},{"title":"Expressions","sidebarTitle":"Expressions","href":"/sql-reference/expressions"},{"group":"Statements","pages":[{"title":"CREATE TABLE","sidebarTitle":"CREATE TABLE","href":"/sql-reference/statements/create-table"},{"title":"ALTER TABLE","sidebarTitle":"ALTER TABLE","href":"/sql-reference/statements/alter-table"},{"title":"CREATE INDEX","sidebarTitle":"CREATE INDEX","href":"/sql-reference/statements/create-index"},{"title":"CREATE VIEW","sidebarTitle":"CREATE VIEW","href":"/sql-reference/statements/create-view"},{"title":"CREATE MATERIALIZED VIEW","sidebarTitle":"CREATE MATERIALIZED VIEW","href":"/sql-reference/statements/create-materialized-view"},{"title":"CREATE TRIGGER","sidebarTitle":"CREATE TRIGGER","href":"/sql-reference/statements/create-trigger"},{"title":"CREATE TYPE","sidebarTitle":"CREATE TYPE","href":"/sql-reference/statements/create-type"},{"title":"CREATE DOMAIN","sidebarTitle":"CREATE DOMAIN","href":"/sql-reference/statements/create-domain"},{"title":"CREATE SEQUENCE","sidebarTitle":"CREATE SEQUENCE","href":"/sql-reference/statements/create-sequence"},{"title":"CREATE VIRTUAL TABLE","sidebarTitle":"CREATE VIRTUAL TABLE","href":"/sql-reference/statements/create-virtual-table"},{"title":"DROP TABLE","sidebarTitle":"DROP TABLE","href":"/sql-reference/statements/drop-table"},{"title":"DROP INDEX","sidebarTitle":"DROP INDEX","href":"/sql-reference/statements/drop-index"},{"title":"DROP VIEW","sidebarTitle":"DROP VIEW","href":"/sql-reference/statements/drop-view"},{"title":"DROP TRIGGER","sidebarTitle":"DROP TRIGGER","href":"/sql-reference/statements/drop-trigger"},{"title":"DROP TYPE","sidebarTitle":"DROP TYPE","href":"/sql-reference/statements/drop-type"},{"title":"DROP DOMAIN","sidebarTitle":"DROP DOMAIN","href":"/sql-reference/statements/drop-domain"},{"title":"DROP SEQUENCE","sidebarTitle":"DROP SEQUENCE","href":"/sql-reference/statements/drop-sequence"},{"title":"SELECT","sidebarTitle":"SELECT","href":"/sql-reference/statements/select"},{"title":"INSERT","sidebarTitle":"INSERT","href":"/sql-reference/statements/insert"},{"title":"UPDATE","sidebarTitle":"UPDATE","href":"/sql-reference/statements/update"},{"title":"DELETE","sidebarTitle":"DELETE","href":"/sql-reference/statements/delete"},{"title":"REPLACE","sidebarTitle":"REPLACE","href":"/sql-reference/statements/replace"},{"title":"INSERT ... ON CONFLICT (Upsert)","sidebarTitle":"Upsert","href":"/sql-reference/statements/upsert"},{"title":"EXPLAIN","sidebarTitle":"EXPLAIN","href":"/sql-reference/statements/explain"},{"title":"Transactions","sidebarTitle":"Transactions","href":"/sql-reference/statements/transactions"},{"title":"ATTACH DATABASE","sidebarTitle":"ATTACH DATABASE","href":"/sql-reference/statements/attach-database"},{"title":"DETACH DATABASE","sidebarTitle":"DETACH DATABASE","href":"/sql-reference/statements/detach-database"},{"title":"ANALYZE","sidebarTitle":"ANALYZE","href":"/sql-reference/statements/analyze"},{"title":"REINDEX","sidebarTitle":"REINDEX","href":"/sql-reference/statements/reindex"},{"title":"VACUUM","sidebarTitle":"VACUUM","href":"/sql-reference/statements/vacuum"}]},{"group":"Functions","pages":[{"title":"Scalar Functions","sidebarTitle":"Scalar Functions","href":"/sql-reference/functions/scalar"},{"title":"Aggregate Functions","sidebarTitle":"Aggregate Functions","href":"/sql-reference/functions/aggregate"},{"title":"Window Functions","sidebarTitle":"Window","href":"/sql-reference/functions/window"},{"title":"Math Functions","sidebarTitle":"Math","href":"/sql-reference/functions/math"},{"title":"Date and Time Functions","sidebarTitle":"Date \u0026 Time","href":"/sql-reference/functions/date-time"},{"title":"JSON Functions","sidebarTitle":"JSON","href":"/sql-reference/functions/json"},{"title":"Vector Functions","sidebarTitle":"Vector","href":"/sql-reference/functions/vector"},{"title":"Array Functions","sidebarTitle":"Array","href":"/sql-reference/functions/array"},{"title":"Full-Text Search","sidebarTitle":"Full-Text Search","href":"/sql-reference/functions/fts"}]},{"title":"Multi-Process Access","sidebarTitle":"Multi-Process Access","href":"/sql-reference/multiprocess-access"},{"title":"Extensions","sidebarTitle":"Extensions","href":"/sql-reference/extensions"},{"title":"PRAGMA Statements","sidebarTitle":"PRAGMAs","href":"/sql-reference/pragmas"},{"title":"Experimental Features","sidebarTitle":"Experimental Features","href":"/sql-reference/experimental-features"}]},{"group":"Changelog","pages":[{"title":"Changelog","url":"https://github.com/tursodatabase/turso/blob/main/CHANGELOG.md","href":"/tursodb/changelog"}]}]},{"tab":"Turso Cloud","pages":[{"title":"Turso Cloud","href":"/turso-cloud"}]},{"tab":"AgentFS (beta)","pages":[{"title":"AgentFS (beta)","href":"/agentfs/introduction"}]}]},"basePath":"","title":"AI Memory - Turso","buildId":"dpl_Fyv1NbzwP68g2bEH2Roy98uY1zmE","deploymentId":"dpl_Fyv1NbzwP68g2bEH2Roy98uY1zmE","dynamics":[],"chunks":[]}