Recipes
Persistent memory across sessions
A chat that remembers yesterday's conversation. SQLite-backed. Works in 5 lines.
A chat that picks up where you left off β across processes, deploys, machines.
#Install
npm install @agentskit/runtime @agentskit/adapters @agentskit/memory#The chat
import { createRuntime } from '@agentskit/runtime'
import { openai } from '@agentskit/adapters'
import { sqliteChatMemory } from '@agentskit/memory'
const runtime = createRuntime({
adapter: openai({ apiKey: KEY, model: 'gpt-4o-mini' }),
memory: sqliteChatMemory({ path: './sessions/user-42.db' }),
})
const reply = await runtime.run(process.argv.slice(2).join(' '))
console.log(reply.content)#Run it
npx tsx chat.ts "My favorite framework is AgentsKit. Remember that."
# > Got it.
# Later, in a different process:
npx tsx chat.ts "What's my favorite framework?"
# > AgentsKit.#What's happening
sqliteChatMemory({ path })returns aChatMemory(per ADR 0003)- Runtime calls
memory.load()at the start ofrun()β conversation rehydrates - After
run()succeeds, runtime callsmemory.save(messages)with the full updated history - Failed or aborted runs do NOT save β atomicity is built into the contract
#Per-user, per-channel, per-X
A new memory instance per scope:
function runtimeFor(userId: string) {
return createRuntime({
adapter,
memory: sqliteChatMemory({ path: `./sessions/${userId}.db` }),
})
}Cheap because createRuntime is config-only (RT1).
#Migrate to Redis when you outgrow SQLite
The contract is identical:
import { redisChatMemory } from '@agentskit/memory'
memory: redisChatMemory({
url: process.env.REDIS_URL!,
key: `session:${userId}`,
}),No code in your runtime changes. That's the plug-and-play promise (Manifesto principle 2).
#Tighten the recipe
- Hash long histories β wrap
memorywith a proxy that summarizes when it grows beyond N tokens (Phase-2 work, #153) - TTL β use Redis
EXPIREfor ephemeral sessions - Encrypt at rest β wrap with an encrypting proxy; the contract doesn't change
#Related
- Concepts: Memory β split contracts, replace-all save
- Recipe: Discord bot β same pattern, per-channel
Explore nearby
- PeerRecipes
Copy-paste solutions grouped by theme. Every recipe end-to-end, runs as written.
- PeerCustom adapter
Wrap any LLM API as an AgentsKit adapter. Plug-and-play with the rest of the kit in 30 lines.
- PeerAdapter contract tests
Verify any adapter against the ADR 0001 invariants A1βA10 with the shared test harness.